I have to write a script where i need to show the sum of entered numbers and the biggest & smallest number among them. I am using array. I have solved this with normal way. but when i am using array i am getting sum, biggest number but can get the lowest number.I understand its because of my if logic for smallest number.
my script:
#!/bin/bash
sum=0
small=0
big=0
echo "Please enter the number"
while(( n != -99 ));
do
read -a n
arr=${#n[@]}
for((i=0;i<$arr;i++))do
if [ ${n[$i]} -eq -99 ]; then
break
elif [ ${n[$i]} -ne -99 ]; then
sum=$((sum + n[$i]))
if [ ${n[$i]} -gt $big ]; then
big=${n[i]}
elif [ ${n[$i]} -le $small ]; then
small=${n[i]}
fi
fi
done
done
echo "Sum: $sum"
echo "Highest: $big"
echo "Lowest: $small"
output:
Please enter the number
12
13
14
-99
Sum: 39
Highest: 14
Lowest: 0
$before some of yourivariables. Also get into the habit of always quoting your variables even if you think they don't need it.