Bash shell script to find out the largest value from given command line arguments
Write a shell script to find out the largest value from the given number of command-line arguments.
Example:

Special variables in bash:
$@- All arguments. $#- Number of arguments. $0- Filename. $1, $2, $3, $4 ... - Specific arguments.
Approach
- If the number of arguments is 0, end the program.
- If not zero, then
- Initialize a variable maxEle with first argument.
- Loop over all the arguments. Compare each argument with maxEle and update it if the argument is greater.
#Check if the number of arguments passed is zero if [ "$#" = 0 ] then #Script exits if no #arguments passed echo "No arguments passed." exit 1 fi #Initialize maxEle with #the first argument maxEle=$1 #Loop that compares maxEle with the #passed arguments and updates it for arg in "$@"do if [ "$arg" -gt "$maxEle" ] then maxEle=$arg fidoneecho "Largest value among the arguments passed is: $maxEle" |
chevron_right
filter_none
Recommended Posts:
- How to find time taken by a command/program on Linux Shell?
- mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
- Netstat command in Linux
- Introduction to Linux Shell and Shell Scripting
- Practical applications of 'ls' command in Linux
- Write a bash script to print a particular line from a file
- 'dd' command in Linux
- 'IPCS' command in Linux with examples
- Bash program to check if the Number is a Prime or not
- Difference between grep and fgrep command
- select command in Linux with examples
- grep command in Unix/Linux
- Sed Command in Linux/Unix with examples
- Making your own Linux Shell in C
- ZIP command in Linux with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



