Tutorials

Empowering academic aid to discover, learn and succeed

Shell Script to find largest of three numbers using If Else statement


Posted by: admin

Program


#!/bin/bash
echo "Enter first number: "
read one
echo "Enter second number: "
read two
echo "Enter third number: "
read three
if [ $one -gt $two ] && [ $one -gt $three ]; then
	echo "First number is largest"
elif [ $two -gt $three ]; then
	echo "Second number is largest"
else
	echo "Third number is largest"
fi

Output 1


$ sh largest-of-three-numbers.sh 
Enter first number: 
13
Enter second number: 
8
Enter third number: 
10
First number is largest

Output 2


$ sh largest-of-three-numbers.sh 
Enter first number: 
6
Enter second number: 
18
Enter third number: 
15
Second number is largest

Output 3


$ sh largest-of-three-numbers.sh 
Enter first number: 
6
Enter second number: 
3
Enter third number: 
10
Third number is largest


Tags:
  • Number