
用shell编程,输入任意几个数,然后输出最大数?
2个回答
展开全部
[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]#
#!/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]#
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询