Question: I have to write a script where i need to show the sum of entered numbers and the biggest & smallest number among them. I

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.i couldn't think anyother way.. need help.

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 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!