[[email protected] ~]# cat five.sh #!/bin/bash #任意输入5个数,判断最大值,最小值,总和 s=0 read -p "please input:" num s=$(($s+$num)) max=$num min=$num for i in `seq 4` do read -p "please input:" num s=$(($s+$num)) if [ $num -le $min ];then min=$num fi if [ $num -ge $max ];then max=$num fi done echo sum:$s max:$max min:$min [[email protected] ~]# bash five.sh please input:30 please input:20 please input:40 please input:10 please input:0 sum:100 max:40 min:0 [[email protected] ~]# cat 2five.sh #!/bin/bash #任意输入3个数,判断最大值,最小值,总和 s=0 n=0 for i in `seq 3` do read -p "please input:" num expr ${num} + 0 1>/dev/null 2>&1 if [ $? -eq 0 ];then echo "${num} is a number!" else echo "${num} is not a number!" exit fi s=$(($s+$num)) [ $n -eq 0 ] && max=$num && min=$num n=$(($n+1)) if [ $num -le $min ];then min=$num fi if [ $num -ge $max ];then max=$num fi done echo sum:$s max:$max min:$min n:$n [[email protected] ~]# bash 2five.sh please input:40 40 is a number! please input:20 20 is a number! please input:10 10 is a number! sum:70 max:40 min:10 n:3 [[email protected] ~]#
时间: 2024-10-06 15:35:29