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

[[email protected] 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‘}‘`

[[email protected] zuoye]# bash quansges.sh 1 d 1
input is error
[[email protected] zuoye]# bash quansges.sh d d 1
input is error
[[email protected] zuoye]# bash quansges.sh d d d
input is error
[[email protected] zuoye]# bash quansges.sh d 1 1
input is error
[[email protected] zuoye]# bash quansges.sh 1 2 3 4 5 6 7 8 9 0
max number:9
min number:0
average is: 4.50
[[email protected] zuoye]# bash quansges.sh 1 2 3 4 5 6 7 8 9 10
max number:10
min number:1
average is: 5.50
[[email protected] zuoye]# 
时间: 2024-08-07 18:53:41

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

Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件

本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import

[Python]在python中调用shell脚本,并传入参数-02python操作shell实例

首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数  test_shell_no_para.sh 内容如下:  test_shell_2_para.sh内容如下 注意含有变量的字符串要用 双引号 括起来 直接在命令行运行 test_shell_2_para.sh 执行结果如下: [email protected]348-G4:~$ sh test_shell_2_para

Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载

Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载 这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数,需要的朋友可以参考下 $#代表了命令行的参数数量,可以看以下实例: 复制代码 代码如下: if [ $# != 1 ] ; then echo "USAGE: $0 TABNAME" echo " e.g.: $0 CDR_CALL_20040701" exit 1; f

shell脚本编程之传递参数

位置参数变量 myscript.sh  argu1 argu2 引用方式: $1,  $2, ..., ${10}, ${11}, ... 在linux上执行脚本时,将参数放置于脚本后面(myscript.sh  argu1 argu2),$n代表要执行的参数 练习:写一脚本,通过命令传递两个文本文件路径给脚本,计算其空白行数之和: #!/bin/bash # file1_lines=$(grep "^$" $1 | wc -l) file2_lines=$(grep "^$

shell脚本接收命令行参数

cd ~ && mkdir $1 && cd $1 && touch $2 && cvc-add --ticket $1 --module mfgpro_cust $2 && cvc-ci --ticket $1 --message 'ci' && cvc-report --promote-avail test $0是第一个参数,$1是第二个,以此类推 sh tmp.sh T-12 test12.p tmp.s

shell脚本的几个参数

$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号)$? 执行上一个指令的返回值 (显示最后命令的退出状态.0表示没有错误,其他任何值表明有错误)$- 显示shell使用的当前选项,与set命令功能相同[email protected] 跟$*类似,但是可以当作数组用

shell脚本获取传递的参数

1 脚本编写 #!/bin/bash echo "第一个参数" $1 echo "第二个参数" $2 2 解释 $n 表示是第几个参数 $0 表示脚本命令本身 3 执行效果 原文地址:https://www.cnblogs.com/QuestionsZhang/p/10992923.html

shell脚本之函数的参数

#! /bin/bash echo use function hello() { echo how many parameters in the function:$#; echo the name of this function is $0; echo the first parameters is :$1; echo the second parameters is :$2; } hello "hello" "world" [[email protected]

shell脚本,awk利用NF来计算文本显示的行数。

解释: 1.awk 'NF{a++;print a,$0;next}1' file4 首先判断NF是否存在值,第一行第二行第三行第四行都存在,进行执行后面的输出,输出后碰到next后,就结束了后面的操作,NF不存在的行不进行执行{}里面的操作,然后就进行默认1输出. 2.awk 'NF{a++;$0=a" "$0}1' file4 NF存在,执行{}里面的操作,操作就是将原来$0=a的值替换成$0=1 a,次数替换之后没有进行打印,然后继续向后执行操作,碰到1默认输出$0,故输出的值就