Bash shell script to find out the largest value from given command line arguments
Last Updated :
05 Sep, 2019
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.
if [ "$#" = 0 ]
then
echo "No arguments passed."
exit 1
fi
maxEle=$1
for arg in "$@"
do
if [ "$arg" -gt "$maxEle" ]
then
maxEle=$arg
fi
done
echo "Largest value among the arguments passed is: $maxEle"
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...