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

时间:2022-12-26 15:12:11
[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\n", '$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]#