I don’t really understand much about arrays, but I need to know how to find and print the largest and smallest values of an array. The array is predefined by a read command, and the user will be prompted to enter n amount of integers.
How would I assign the read input to an array and find and display the largest and smallest values of the array?
Is there a way to test the array elements to see if they are all integers?
#!/bin/bash
read -a integers
biggest=${integers[0]}
smallest=${integers[0]}
for i in ${integers[@]}
do
if [[ $i -gt $biggest ]]
then
biggest="$i"
fi
if [[ $i -lt $smallest ]]
then
smallest="$i"
fi
done
echo "The largest number is $biggest"
echo "The smallest number is $smallest"