linux shell中输出0-100中连续5个数字中的最小值、最大值、求和
1、
root@PC1:/home/test2# ls test.sh root@PC1:/home/test2# cat test.sh ## script #!/bin/bash SUM=0 MIN=100 MAX=0 COUNT=0 while [ $COUNT -lt 5 ] do read -p "please input an integer of range 1-100: " INT if [[ ! $INT =~ ^[0-9]+$ ]] then echo "error! please input integer : " continue elif [ $INT -gt 100 ] || [ $INT -lt 0 ] then echo "error! please input integer 0-100: " continue fi let SUM=SUM+INT [ $INT -le $MIN ] && MIN=$INT [ $INT -ge $MAX ] && MAX=$INT let COUNT++ done echo "sum: $SUM" echo "min: $MIN" echo "max: $MAX" root@PC1:/home/test2# bash test.sh ## 执行脚本 please input an integer of range 1-100: 40 please input an integer of range 1-100: abc error! please input integer : please input an integer of range 1-100: 20 please input an integer of range 1-100: 588 error! please input integer 0-100: please input an integer of range 1-100: 0 please input an integer of range 1-100: 32 please input an integer of range 1-100: r3 error! please input integer : please input an integer of range 1-100: 70 sum: 162 ## 结果输出 min: 0 max: 70