Shell script to find greatest of three numbers

Let's write a shell script to find the greatest of three numbers.




Algorithm

1. Get three numbers. Say num1, num2, num2

2. If (num1 > num2) and (num1 > num3)

     echo value of num1

3. elif(num2 > num1) and (num2 > num3)

     echo value of num2

4. Otherwise,

     echo value of num3




Shell script for finding greatest of three numbers

#shell script to find the greatest of three numbers

echo "Enter Num1"
read num1
echo "Enter Num2"
read num2
echo "Enter Num3"
read num3

if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]
then
    echo $num1
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then
    echo $num2
else
    echo $num3
fi

read function used to get the input from the user.

-gt stands for greater than.

&& represents the logical AND condition.




Output

Enter Num1

1

Enter Num2

34

Enter Num3

2

34


Enter Num1

500

Enter Num2

5

Enter Num3

6

500


Useful Resources

To learn more shell script examples, you can visit the link