Articles

Shell script to find greatest of three numbers

Shell script to find greatest of three numbers


In this chapter of shell script programs, we will learn:

  • find greatest of 3 numbers in shell script

Shell script to find greatest of three numbers

In this example, we will find greatest of 3 numbers in shell script. To solve this problem, we have to know few topics-

  1. ‘echo’ statement
  2. ‘read’ command
  3. ‘if-else’ statement
  4. How to check condition in shell script
  5. 'gt', 'ge' commands

ALGORITHM:

Below algorithm shows steps of how to find greatest of three numbers in linux:

  • STEP 1: START THE PROBLEM
  • STEP 2: TAKE THREE INPUTS FROM THE USER
  • STEP 3: IN IF-ELSE CONDITION, CHECK WHICH IS THE GREATEST
  • STEP 4: ALSO CHECK WITH THE THIRD NUMBER
  • STEP 5: FIND THE RESULT
  • STEP 6: PRNT THE RESULT
  • STEP 7: STOP THE PROGRAM

CODE:

Shell script to find the largest of three numbers using command line arguments:

clear
echo "---FIND THE GREATEST AMONG THREE NUMBERS---"
echo "Enter 1st number:"
read first_num
echo "Enter 2nd number:"
read second_num
echo "Enter 3rd number:"
read third_num
if test $first_num -gt $second_num && test $first_num -gt $third_num
then
	echo $first_num is the greatest number.
elif test $second_num -gt $third_num
then
	echo $second_num is the greaatest number.
else
	echo $third_num is the greatest number.
fi

CODE IMAGE IN CYGWIN:

how to find greatest of three numbers in linux

OUTPUT:

---FIND THE GREATEST AMONG THREE NUMBERS---

Enter 1st number:
45
Enter 2nd number:
343
Enter 3rd number:
23
343 is the greaatest number.
 
find greatest of three numbers in linux

 


Shell Script Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Suraj Maity
Page Views :    Published Date : Feb 19,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!