≡ Menu

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

#!/bin/bash
# Shell script to read 3 numbers and find the greaters of the three
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project 
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
echo -n "Please enter three numbers (separate number by space) : "
read a b c
# compare a with b and c. Note -a is logical and operator
if [ $a -gt $b -a $a -gt $c ]
then
   big=$a
elif  [ $b -gt $a -a $b -gt $c ] # compare b with a and c
then
   big=$b
elif  [ $c -gt $a -a $c -gt $b ] # compare c with a and b
then
   big=$c
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
then
   big="All three numbers are same (equal)"
else # something must be wrong if we are here, like one of number is character such as 'A'
   big="Can not guess greaters of three numbers"
fi
# display result
echo "Result : $big"
{ 8 comments… add one }
  • hamza May 24, 2012, 8:33 pm

    i love it, cos am just learning shell

  • sameer mishra April 9, 2012, 1:51 am

    superb logic

  • mahendra bohra March 25, 2012, 12:20 pm

    this is good because all steps are defined

  • Muthu Lakshmanan October 14, 2011, 4:43 pm

    I Like thisssssssssssssssss

  • trilok September 27, 2011, 9:53 am

    thanx for help us….

  • sanjay kundu November 20, 2009, 10:25 am

    the program length is very simple

    • mayur August 11, 2010, 6:01 am

      really simple…

  • SABAREESAN July 3, 2008, 1:21 pm

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

Security: Are you a robot or human?

Leave a Comment