Chinaunix首页 | 论坛 | 博客
  • 博客访问: 338891
  • 博文数量: 222
  • 博客积分: 9349
  • 博客等级: 中将
  • 技术积分: 2135
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 13:45
文章分类

全部博文(222)

文章存档

2010年(222)

分类: LINUX

2010-08-08 10:27:01

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
阅读(220) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~