Shell编程上机题参考如下程序:编写Shell程序test,使之从命令行接受数字,输出最大值,例如,键入test 1 2 3,则屏幕显示:the largest number is : 3。n=$# if (($#<1))then echo "error number" exitelsemax=$1 while ((n>=1)) do if (($1>=max)) then max=$1 fishift ((n=n-1)) doneecho " the largest number is $max" fi改写上述程序1)编写Shell程序6-2-1.sh,使之从命令行接受数字,输出最小值,例如,键入test 1 2 3,则屏幕显示:the smallest number is : 1。2) 编写Shell程序6-2-2.sh,使之从命令行接受数字,输出次最大值,例如,键入test 1 2 3,则屏幕显示:the second largest number is : 2。上传6-2-1.sh和6-2-2.sh
时间: 2024-02-10 19:08:12 浏览: 32
抱歉,我无法上传文件,但是我可以为您提供6-2-1.sh和6-2-2.sh的代码:
6-2-1.sh:
```
#!/bin/bash
n=$#
if (($#<1)); then
echo "error number"
exit
else
min=$1
while ((n>=1)); do
if (($1<=min)); then
min=$1
fi
shift
((n=n-1))
done
echo "the smallest number is $min"
fi
```
6-2-2.sh:
```
#!/bin/bash
n=$#
if (($#<2)); then
echo "error number"
exit
else
max=$1
second_max=$2
if (($1<$2)); then
max=$2
second_max=$1
fi
while ((n>=1)); do
if (($1>max)); then
second_max=$max
max=$1
elif (($1>second_max)); then
second_max=$1
fi
shift
((n=n-1))
done
echo "the second largest number is $second_max"
fi
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)