用shell编程,输入任意几个数,然后输出最大数?

2024-12-04 17:59:18
推荐回答(2个)
回答1:

[root@liuxiting testdir]# cat maxMumber.sh
#!/bin/bash
read -p "input num : " num
if [ $num -eq 0 ]
then
echo no number to compare!
elif [ $num -eq 1 ]
then
read res
elif [ $num -eq 2 ]
then
read a
read b
if [ $a -gt $b ]
then
res=$a
else
res=$b
fi
else
read res
for (( i=1; i< $num ; i++ ));
do
read a
if [ $a -gt $res ]
then
res=$a;
fi
done
fi
echo max number is $res

使用方法:先输入总个数,在依次输出要比较的数,如:
[root@liuxiting testdir]# ./maxMumber.sh
input num : 4
2
3
41
23
max number is 41
[root@liuxiting testdir]#

回答2:

弄个简单的管道组合,看能否帮助你:
$ cat | sort -n | tail -1