greatest of three numbers

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

Advertisements

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my Master's in Computer Science.
This entry was posted in Shell scripting and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s