shell脚本,计算输入给定的数,判断最大值,最小值,总和?

时间:2016-08-29 17:31:22   收藏:0   阅读:1258
[root@localhost ~]# 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
[root@localhost ~]# bash five.sh 
please input:30
please input:20
please input:40
please input:10
please input:0
sum:100 max:40 min:0



[root@localhost ~]# 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 [root@localhost ~]# 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 [root@localhost ~]#

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!