1.5K
Shell Script that takes three numbers as input from the user and then finds the greatest of them using conditional statements.
Shell Script to find the greater of three numbers
#!/bin/bash
echo "Enter three numbers:"
read num1
read num2
read num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]
then
echo "$num1 is the greatest number"
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then
echo "$num2 is the greatest number"
else
echo "$num3 is the greatest number"
fi
Save the above script with a filename, let’s say find_greatest.sh
. Then, run the following command in your terminal.
~/Assignment$ bash main.sh
Enter three numbers:
34
6
78
78 is the greatest number
This will execute the script and prompt you to enter three numbers. After entering the numbers, the script will find and display the greatest of the three numbers.