首页 > 代码库 > shell脚本,计算输入给定的数,判断最大值,最小值,总和?

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

[root@localhost ~]# cat five.sh #!/bin/bash#任意输入5个数,判断最大值,最小值,总和s=0read -p "please input:" nums=$(($s+$num))max=$nummin=$numfor 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      fidoneecho sum:$s max:$max min:$min[root@localhost ~]# bash five.sh please input:30please input:20please input:40please input:10please input:0sum:100 max:40 min:0



[root@localhost ~]# cat 2five.sh #!/bin/bash#任意输入3个数,判断最大值,最小值,总和s=0n=0for 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 fidoneecho sum:$s max:$max min:$min n:$n[root@localhost ~]# bash 2five.sh please input:4040 is a number!please input:2020 is a number!please input:1010 is a number!sum:70 max:40 min:10 n:3[root@localhost ~]#

 

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