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\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]# 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM