6. continue 命令
continue 命令用于循环命令(for、while、until 命令) 或 select 命令的内部。他用于终止当前循环体内部,剩余部分的命令的执行,重新开始一次新的循环。
命令格式
continue [n]
语法说明
- continue 命令只能用于 for 命令、while 命令、until 命令 或 select 命令的内部。
- 参数 n 是 continue 命令外层循环嵌套的层数,默认为1。。
示例
写一个程序 test_continue.sh,此程序输入一系列整数代表学生的成绩,当输入 -1 时结束输入。统计成绩在 60 ~ 100 分的人数,打印这些数的人数。
#!/bin/bash
count=0
while read -p "请输入一个整数: " score
do
if [ "$score" -le -1 ]; then
break
fi
if [ "$score" -lt 60 ]; then
continue
fi
if [ "$score" -gt 100 ]; then
continue
fi
count=`expr $count + 1`
done
echo "成绩合格的人数: $count"
执行结果
weimingze@mzstudio:~$ bash test_continue.sh
请输入一个整数: 90
请输入一个整数: 20
请输入一个整数: 80
请输入一个整数: 30
请输入一个整数: -1
成绩合格的人数: 2
练习:
- 写一个 Shell 脚本程序
not7.sh,打印 1~100 范围内个位、十位、百位都不包含 7,且不能被 7 整除的所有整数。