Shell 编程基础之 Break, Continue 练习

一、语法

break:用来跳出循环。break N 可以跳出多层循环

break
break N

continue:用来跳过循环。continue N 可以跳过多层循环

continue
continue N

二、详细与区别

  1. break
    for var1 in 1 2 3
    do
      for var2 in 0 5
      do
        if [ "$var1" -eq "2" -a "$var2" -eq "0" ]; then
          break 2
        else
          echo "$var1 $var2"
        fi
      done
    done
    user@ae01:~$ ./test.sh
    1 0
    1 5
  2. continue
    for var1 in 1 2 3
    do
      for var2 in 0 5
      do
        if [ "$var1" -eq "2" -a "$var2" -eq "0" ]; then
          continue 2
        else
          echo "$var1 $var2"
        fi
      done
    done
    user@ae01:~$ ./test.sh
    1 0
    1 5
    3 0
    3 5
posted @ 2014-10-29 10:15  TannerBG  阅读(241)  评论(0编辑  收藏  举报