1. 程式人生 > >shell指令碼之任意輸入n個數,判斷最大值,最小值,總和

shell指令碼之任意輸入n個數,判斷最大值,最小值,總和

#!/bin/bash

##任意輸入n個數,判斷最大值,最小值,總和

sum=0
n=0
read -p "please input the count of number:" count
#max=0
#min=0
for i in `seq $count`
do
    read -p "please input the $i number:" temp
    ((temp+0)) 1>/dev/null 2>&1
    if (($? == 0 ))
    then
        echo "$temp is a number"
    else
        echo "$temp is not a number,please input again"
        continue 
    fi
    sum=$((sum+temp))
    [ $n -eq 0 ] && max=${temp} && min=${temp}
    n=$((n+1))
    
    if ((max < temp))
    then
        max=${temp} 
    fi
    
    if ((min > temp))
    then
        min=${temp} 
    fi
done  

echo "sum=$sum, max=$max, min=$min"