5.1
分别使用while循环和for循环编写shell脚本程序,要求用户从键盘输入一个整数,然后程序计算1+2+3+……,一直加到用户输入的整数为止。
使用while循环的示例代码:
```bash
#!/bin/bash
echo "请输入一个整数:"
read num
sum=0
i=1
while [ $i -le $num ]
do
sum=$((sum+i))
i=$((i+1))
done
echo "1+2+3+...+$num=$sum"
```
使用for循环的示例代码:
```bash
#!/bin/bash
echo "请输入一个整数:"
read num
sum=0
for ((i=1; i<=num; i++))
do
sum=$((sum+i))
done
echo "1+2+3+...+$num=$sum"
```
5.2
编写shell脚本程序,从键盘输入若干个考试分数(分数为整数),计算并输出所有分数的平均值。要求如下:
(1)使用while或until循环实现整数输入,如果输入为字符q,则退出键盘输入
(2)如果输入分数不在0—100范围内,提示用户输入有误
(3)平均值计算不需要考虑小数部分,只需要得到整数部分即可
#!/bin/bash
sum=0
count=0
while true; do
read -p "请输入一个整数分数(输入q退出):" score
if [[ "$score" == "q" ]]; then
break
elif [[ ! "$score" =~ ^[0-9]+$ ]]; then
echo "输入有误,请输入一个整数"
elif (( score < 0 || score > 100 )); then
echo "分数必须在0-100之间"
else
sum=$(( sum + score ))
count=$(( count + 1 ))
fi
done
if (( count == 0 )); then
echo "没有输入任何分数"
else
avg=$(( sum / count ))
echo "所有分数的平均值为:$avg"
fi
5.3
编写shell脚本程序,初始化包含整数的数组a,找出其中的最大值,并统计该数出现的次数。例如初始化数组a=(1 3 0 9 8 2 9 4 9 7),程序应输出:最大值为9,共出现3次
#!/bin/bash
# 初始化数组
a=(1 3 0 9 8 2 9 4 9 7)
# 找出最大值
max=${a[0]}
for i in "${a[@]}"
do
if (( i > max )); then
max=$i
fi
done
# 统计最大值出现的次数
count=0
for i in "${a[@]}"
do
if (( i == max )); then
count=$(( count + 1 ))
fi
done
# 输出结果
echo "最大值为$max,共出现$count次"
5.4
保存Linux账号信息的文件/etc/passwd是以 : 来分隔,其中第一列为登录账号名称。编写shell脚本程序,将/etc/passwd的第一列账号名称取出,并分行显示如下:
The 1 account is “root”
The 2 account is “bin”
…
#!/bin/bash
file="/etc/passwd"
count=0
while IFS=":" read -r username _ ; do
count=$(( count + 1 ))
echo "The $count account is \"$username\""
done < "$file"
编写一个shell,检查文件是否存在,文件的类型,文件的权限,文件的新旧
#!/bin/bash
# 输入文件名
read -p "请输入文件名:" filename
# 检查文件是否存在
if [ ! -e $filename ]; then
echo "文件不存在"
exit 1
fi
# 判断文件类型
if [ -f $filename ]; then
echo "文件类型:普通文件"
elif [ -d $filename ]; then
echo "文件类型:目录"
fi
# 检查文件权限
if [ -r $filename ]; then
echo "文件可读"
fi
if [ -w $filename ]; then
echo "文件可写"
fi
if [ -x $filename ]; then
echo "文件可执行"
fi
# 检查文件的新旧
current_time=$(date +%s)
file_time=$(stat -c "%Y" $filename)
time_diff=$((current_time - file_time))
if [ $time_diff -lt 60 ]; then
echo "文件很新"
elif [ $time_diff -lt 3600 ]; then
echo "文件较新"
else
echo "文件较旧"
fi
shell中分别使用while,until,for实现计算1到100内所有奇数之和
#!/bin/bash
i=1 # 初始值为1
sum=0 # 初始和为0
while [ $i -le 100 ]
do
if [ $((i % 2)) -eq 1 ] # 如果是奇数
then
sum=$((sum + i)) # 累加到总和中
fi
i=$((i + 1)) # 自增1
done
echo "1到100内所有奇数之和为:$sum"
```
使用 `until` 循环计算 1 到 100 之间所有奇数之和的 Shell 脚本:
```
#!/bin/bash
i=1
sum=0
until [ $i -gt 100 ]
do
if [ $((i % 2)) -eq 1 ]
then
sum=$((sum + i))
fi
i=$((i + 1))
done
echo "1到100内所有奇数之和为:$sum"
```
使用 `for` 循环计算 1 到 100 之间所有奇数之和的 Shell 脚本:
```
#!/bin/bash
sum=0
for (( i=1; i<=100; i++ ))
do
if [ $((i % 2)) -eq 1 ]
then
sum=$((sum + i))
fi
done
echo "1到100内所有奇数之和为:$sum"
```
三者的输出结果都为:
```
1到100内所有奇数之和为:2500
```
编写一个shell,使用关系测试运算符比较数字的大小关系
#!/bin/bash
echo "请输入一个整数A:"
read num1
echo "请输入一个整数B:"
read num2
if [ $num1 -eq $num2 ]; then
echo "$num1 等于 $num2"
elif [ $num1 -gt $num2 ]; then
echo "$num1 大于 $num2"
else
echo "$num1 小于 $num2"
fi
编写一个shell,使用字符串测试运算符检查字符串是否为空,字符串是否相等
#!/bin/bash
echo "请输入一个字符串A:"
read str1
echo "请输入一个字符串B:"
read str2
if [ -z "$str1" ]; then
echo "字符串A为空"
elif [ "$str1" = "$str2" ]; then
echo "字符串A等于字符串B"
else
echo "字符串A不为空且不等于字符串B"
fi
shell中使用if-elif结构将用户输入的数字月份转换成对应的英文表示。如果数字不在1-12内,则给出错误提示
#!/bin/bash
read -p "请输入数字月份(1-12):" month
if [ $month -eq 1 ]; then
echo "January"
elif [ $month -eq 2 ]; then
echo "February"
elif [ $month -eq 3 ]; then
echo "March"
elif [ $month -eq 4 ]; then
echo "April"
elif [ $month -eq 5 ]; then
echo "May"
elif [ $month -eq 6 ]; then
echo "June"
elif [ $month -eq 7 ]; then
echo "July"
elif [ $month -eq 8 ]; then
echo "August"
elif [ $month -eq 9 ]; then
echo "September"
elif [ $month -eq 10 ]; then
echo "October"
elif [ $month -eq 11 ]; then
echo "November"
elif [ $month -eq 12 ]; then
echo "December"
else
echo "错误:请输入1-12之间的数字月份"
fi