shell 脚本案例:要求输入五个数,统计五个数的和,最小值、最大值

min=0
max=100
num=0
sum=0

while [ $num -lt 5 ]
do
        read -p 'pls enter a int number:' a
        expr $a + 1 &>/dev/null
        if [ $? -ne 0 ];then
                echo "your enter must be int"
                exit 1
        fi

        if [ $a -gt 100 ];then
                echo "pls enter number must less than 100"
                exit 1
        fi

        sum=$((a+sum))
        [ $max -gt $a ] && max=$a
        [ $min -lt $a ] && min=$a
        let num++
done
echo "sum is: $sum"
echo "min value: $max"
echo "max value :$min"

 

你可能感兴趣的:(shell脚本)