Shell script to read 3 numbers and find the greaters of the three

by on April 4, 2008 · 8 comments

  1. #!/bin/bash
  2. # Shell script to read 3 numbers and find the greaters of the three
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # ----------------------------------------------------------------------
  10. echo -n "Please enter three numbers (separate number by space) : "
  11. read a b c
  12. # compare a with b and c. Note -a is logical and operator
  13. if [ $a -gt $b -a $a -gt $c ]
  14. then
  15. big=$a
  16. elif [ $b -gt $a -a $b -gt $c ] # compare b with a and c
  17. then
  18. big=$b
  19. elif [ $c -gt $a -a $c -gt $b ] # compare c with a and b
  20. then
  21. big=$c
  22. elif [ $a -eq $b -a $a -eq $c -a $b -eq $c -a $c -eq $b ] # see if all of them are equal or not
  23. then
  24. big="All three numbers are same (equal)"
  25. else # something must be wrong if we are here, like one of number is character such as 'A'
  26. big="Can not guess greaters of three numbers"
  27. fi
  28. # display result
  29. echo "Result : $big"


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

Click here to subscribe via email.

  • SABAREESAN

    thanks !!! helps 4 learning unix 4 begineers like me

  • sanjay kundu

    the program length is very simple

  • mayur

    really simple…

  • trilok

    thanx for help us….

  • Muthu Lakshmanan

    I Like thisssssssssssssssss

  • mahendra bohra

    this is good because all steps are defined

  • sameer mishra

    superb logic

  • hamza

    i love it, cos am just learning shell

Previous Script:

Next Script: