shell脚本,通过传入的参数来计算最大值和最小值以及平均值。

[root@localhost zuoye]# cat quansges.sh 
#!/bin/bash 

 > file
[ ! $#  -ge 3  ] &&  echo "please input three number or more number!" && exit 2 || echo $* >file

for i in $*
do
  expr $i + 1  &> /dev/null 
 [  $?  -ne  0  ] && echo "input is error" && exit 
done

      a=`cat file |xargs -n1|sort -n|head -1`
      b=`cat file |xargs -n1|sort -n|tail -1`
      c=`cat file |xargs -n1|sort -n|wc -l`
      
     d=`cat file |xargs -n1|awk '{a+=$1}'END'{print a}'`
     
             
#[ $b -ne 0 ] && 
echo "max number:"$b 
#[ $a -ne 0 ] &&

 echo "min number:"$a

echo "average is:" `awk 'BEGIN{printf "%.2f
", '$d'/'$c'}'`

[root@localhost zuoye]# bash quansges.sh 1 d 1
input is error
[root@localhost zuoye]# bash quansges.sh d d 1
input is error
[root@localhost zuoye]# bash quansges.sh d d d
input is error
[root@localhost zuoye]# bash quansges.sh d 1 1 
input is error
[root@localhost zuoye]# bash quansges.sh 1 2 3 4 5 6 7 8 9 0
max number:9
min number:0
average is: 4.50
[root@localhost zuoye]# bash quansges.sh 1 2 3 4 5 6 7 8 9 10
max number:10
min number:1
average is: 5.50
[root@localhost zuoye]# 
原文地址:https://www.cnblogs.com/wangyuebo/p/5875635.html