Write a Bash shell script highestTest.sh that will determine the highest value given to the script. It should take in as a command-line argument a series of numbers. This list could be of any length. Your script should start by finding the lowest value in the list of values. Then it should find how many times that value appears in total in the list. It should then output those results. In addition, I want you to detect if any fractional values have been entered and output an error message.
highestTest.sh
#!/bin/bash
# checking if arguments passed or not
if [ $# -eq 0 ]
then
printf "no arguments given \n"
printf "usage: $0 <num1> <num2> ...."
fi
low=9999999
# regex for detecting integer
re='^[0-9]+$'
# looping through number
for n in "$@"
do
# checking if number is complete or not
if ! [[ $n =~ $re ]] ; then
# if number is fractional or other printing error message and
# continue for next number
printf "Error: $n not an integer skipping this value\n"
continue
fi
# checking the lowest number
if [ "$n" -lt $low ]
then
low=$n
fi
done
count=0
# finding the count of lowest number
for i in "$@"
do
if ! [[ $i =~ $re ]] ; then
continue
fi
if [[ $i -eq $low ]]
then
count=$((count+1))
fi
done
printf "lowest number is $low\n"
printf "no of times occured: $count\n"
# OUT
please do let me know if u have any concern....
Get Answers For Free
Most questions answered within 1 hours.