Shell编程上机题 参考如下程序: 编写Shell程序test,使之从命令行接受数字,输出最大值,例如,键入test 1 2 3,则屏幕显示:the largest number is : 3。 n=$# if (($#<1)) then echo "error number" exit else max=$1 while ((n>=1)) do if (($1>=max)) then max=$1 fi shift ((n=n-1)) done echo " the largest number is $max" fi 改写上述程序 1)编写Shell程序6-2-1.sh,使之从命令行接受数字,输出最小值, 例如,键入test 1 2 3,则屏幕显示:the smallest number is : 1。
时间: 2024-02-10 17:08:12 浏览: 32
可以参考以下的Shell程序:
```
#!/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
```
这个程序与原程序类似,不过将变量名改成了min,并且比较大小的符号也变成了<=。同时需要注意,最小值初值应该设为第一个输入的数字。
相关问题
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
抱歉,我无法上传文件,但是我可以为您提供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
```
编写Shell程序test,使之从命令行接受数字,输出最大值,例如,键入test 1 2 3,则屏幕显示:the largest number is : 3。 n=$# if (($#<1)) then echo "error number" exit else max=$1 while ((n>=1)) do if (($1>=max)) then max=$1 fi shift ((n=n-1)) done echo " the largest number is $max" fi 改写上述程序 1)编写Shell程序6-2-1.sh,使之从命令行接受数字,输出最小值, 例如,键入test 1 2 3,则屏幕显示:the smallest number is : 1。
以下是编写的Shell程序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
```
主要思路与原程序相同,只是将变量名和比较符修改为查找最小值的方式,并在输出语句中将“the largest number”改为“the smallest number”。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)