Shell Script To Find Largest Among 3 Integers (Numbers) Given As Arguments

by on October 20, 2008 · 7 comments

  1. #!/bin/bash
  2. # Shell code to find largest integer among the 3 integers given as arguments.
  3. # --------------------------------------------------------------------
  4. # This is a free shell script under GNU GPL version 2.0 or above
  5. # Copyright (C) 2004 nixCraft project.
  6. # Feedback/comment/suggestions : http://cyberciti.biz/fb/
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # -------------------------------------------------------------------------
  11.  
  12. a=$1
  13. b=$2
  14. c=$3
  15.  
  16. if [ $# -lt 3 ]
  17. then
  18. echo "$0 n1 n2 n3"
  19. exit 1
  20. fi
  21.  
  22. if [ $a -gt $b -a $a -gt $c ]
  23. then
  24. echo "$a is largest integer"
  25. elif [ $b -gt $a -a $b -gt $c ]
  26. then
  27. echo "$b is largest integer"
  28. elif [ $c -gt $a -a $c -gt $b ];
  29. then
  30. echo "$c is largest integer"
  31. else
  32. echo "Sorry cannot guess number"
  33. fi


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • sysadmn

    Um,
    echo -e "$a\n$b\n$c" | sort -rn | head -1
    or


    big = $a;
    [ $b -gt $big ] && big=$b;
    [ $c -gt $big ] && big=$c;
    echo $big

  • jacob

    It is quite helpful to know how the argument list can be easily handled. Thank you very much !! I was totally confused to see a lot of complex scripts. But, this one really helped me.

  • loganathan

    i need ordinary program for linux progran for find biggest among 3 numbers using command line arguments… please help me…

  • poo

    write a shell script that uses any encryption method to encrypt a file

  • dick-head

    HAHA sysadmn got that one

  • Anonymous

    what is the pupose of $# in the program

  • deep

    what is meaning of elif

Previous Script:

Next Script: