break 命令可以带一个参数.一个不带参数的break 循环只能退出最内层的循环,而break N
可以退出N 层循环.
continue 命令也可以带一个参数.一个不带参数的continue 命令只去掉本次循环的剩余代码
.而continue N 将会把N 层循环剩余的代码都去掉,但是循环的次数不变.
下面是continue带参数的例子#!/bin/bash
# The "continue N" command, continuing at the Nth level loop.
for outer in I II III IV V # outer loop
do
echo; echo -n "Group $outer: "
# --------------------------------------------------------------------
for inner in 1 2 3 4 5 6 7 8 9 10 # inner loop
do
if [ "$inner" -eq 7 ]
then
continue 2 # Continue at loop on 2nd level, that is "outer loop".
# Replace above line with a simple "continue"
# to see normal loop behavior.
fi
echo -n "$inner " # 7 8 9 10 will never echo.
done
# --------------------------------------------------------------------
done
echo; echo
# Exercise:
# Come up with a meaningful use for "continue N" in a script.
exit 0
for x in 1 2 3
do
echo before $x
continue 1
echo after $x
done
The output for the preceding will be
before 1
before 2
before 3
阅读(359) | 评论(0) | 转发(0) |