In today’s post we shall write a code to find biggest of the three numbers.To write the code we need to know the syntax of nested if statements in shell scripting.The syntax of nested if statements is as follows:
if [ expr ]
then
if [ expr ]
then
//statements to be executed
else
//statements to be executed
fi
else
//statements to be executed
fi
Now the logic to find the greatest of the three numbers is, first find the biggest of any two numbers and then compare the result with the remaining number and find the biggest number.Therefore we shall write the program using the nested if statements and is as follows:
echo “Enter first value:”
read a
echo “Enter the second value:”
read b
echo “Enter the third value:”
read c
if [ $a -ge $b ]
then
if [ $a -gt $c ]
then
echo “Biggest of the given numbers is $a”
else
echo “Biggest of the given numbers is $c”
fi
else
if [ $b -gt $c ]
then
echo “Biggest of the given numbers is $b”
else
echo “Biggest of the given numbers is $c”
fi
fi
output:
Enter first value:5
Enter the second value:8
Enter the third value:2
Biggest of the given numbers is 8
You can download the script from the link:
https://docs.google.com/file/d/0BxVG8Ozr6fvhaHJNNXFxd2dOcWs/edit?pli=1