创建一个shell脚本,该脚本接收10个数,并显示最大数,最小数。求高手的linuxs,在网上查的那些都执行不了!

如题所述

一般shell只接受$0~$9十个位置参数,其中$0表示脚本名称本身,也就是说只有$1~$9共9个参数。超过9个参数的话,比如你这里要10个数,需要用shift移位来获取后面的更多参数。

#!/bin/sh
if [ $# -ne 10 ]; then
echo -e "Wrong parameters!\nYou MUST input 10 digits."
exit 1
fi
min=$1
max=$1
i=1
while [ $i -lt 10 ]
do
shift 1
let i+=1
[ $1 -lt $min ] && min=$1
[ $1 -gt $max ] && max=$1
done
echo "Min=$min"
echo "Max=$max"
exit 0
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-13
#!/bin/bash

function usage()
{
echo "$0 N1 N2 ... N10" >&2
exit 1
}

[[ $# -eq 10 ]] || usage

max=$1
min=$1
shift
for i in $*
do
[[ $min -gt $i ]] && min=$i
[[ $i -gt $max ]] && max=$i
done

echo -e "Max=$max\tMin=$min"本回答被提问者采纳
第2个回答  2012-05-12
csh脚本:
chengqi@chengqi-PC /cygdrive/d/test
$ cat ceshi.sh
#!/bin/csh
set first_num=$1
echo $1\\n$2\\n$3\\n$4\\n$5\\n$6\\n$7\\n$8\\n$9\\n$10 | awk 'BEGIN{max="'$first_num'";min="'first_num'"}{if($1>max){max=$1};if($1<min){min=$1}}END{pri
nt "the max number is "max;print "the min number is "min}'

执行结果:
chengqi@chengqi-PC /cygdrive/d/test
$ ./ceshi.sh 11 31 23 34 64 1 45 20 32 14
the max number is 64
the min number is 1追问

执行不了,大哥。在虚拟机上截个图看看。

追答

我写的都是验证过的
-_-!