11.3. Loop Control 笔记

370阅读 0评论2013-12-26 wuxiaobo_2009
分类:LINUX

Commands affecting loop behavior break, continue

 The break command terminates the loop (breaks out of it), while continue causes a jump to the next of the loop, skipping all the remaining commands in that particular loop cycle.  break: 结束循环; continue: 结束这次循环,进行下一次迭代。

点击(此处)折叠或打开

  1. #!/bin/bash

  2. LIMIT=19 # Upper limit

  3. echo
  4. echo "Printing Numbers 1 through 20 (but not 3 and 11)."

  5. a=0

  6. while [ $a -le "$LIMIT" ]
  7. do
  8.  a=$(($a+1))

  9.  if [ "$a" -eq 3 ] || [ "$a" -eq 11 ] # Excludes 3 and 11.
  10.  then
  11.    continue # Skip rest of this particular loop iteration.
  12.  fi

  13.  echo -n "$a " # This will not execute for 3 and 11.
  14. done

  15. # Exercise:
  16. # Why does the loop print up to 20?

  17. echo; echo

  18. echo Printing Numbers 1 through 20, but something happens after 2.

  19. ##################################################################

  20. # Same loop, but substituting 'break' for 'continue'.

  21. a=0

  22. while [ "$a" -le "$LIMIT" ]
  23. do
  24.  a=$(($a+1))

  25.  if [ "$a" -gt 2 ]
  26.  then
  27.    break # Skip entire rest of loop.
  28.  fi

  29.  echo -n "$a "
  30. done

  31. echo; echo; echo

  32. exit 0

  33. # break 退出本层循环,continue 退出本次循环。

The break command may optionally take a parameter. A plain break terminates only the innermost loop in which it is embedded, but a break N breaks out of N levels of loop.

点击(此处)折叠或打开




  1. #!/bin/bash
  2. # break-levels.sh: Breaking out of loops.

  3. # "break N" breaks out of N level loops.

  4. for outerloop in 1 2 3 4 5
  5. do
  6.   echo -n "Group $outerloop: "

  7.   # --------------------------------------------------------
  8.   for innerloop in 1 2 3 4 5
  9.   do
  10.     echo -n "$innerloop "

  11.     if [ "$innerloop" -eq 3 ]
  12.     then
  13.       break # Try break 2 to see what happens.
  14.              # ("Breaks" out of both inner and outer loops.)
  15.     fi
  16.   done
  17.   # --------------------------------------------------------

  18.   echo
  19. done

  20. echo

  21. exit 0


点击(此处)折叠或打开

  1. #!/bin/bash
  2. # The "continue N" command, continuing at the Nth level loop.

  3. for outer in I II III IV V # outer loop
  4. do
  5.   echo; echo -n "Group $outer: "

  6.   # --------------------------------------------------------------------
  7.   for inner in 1 2 3 4 5 6 7 8 9 10 # inner loop
  8.   do

  9.     if [[ "$inner" -eq 7 && "$outer" = "III" ]]
  10.     then
  11.       continue 2 # Continue at loop on 2nd level, that is "outer loop".
  12.                   # Replace above line with a simple "continue"
  13.                   # to see normal loop behavior.
  14.     fi

  15.     echo -n "$inner " # 7 8 9 10 will not echo on "Group III."
  16.   done
  17.   # --------------------------------------------------------------------

  18. done

  19. echo; echo

  20. # Exercise:
  21. # Come up with a meaningful use for "continue N" in a script.

  22. exit 0

continue N : 可以直接在内层对外层循环控制。进行一下

点击(此处)折叠或打开

  1. # Albert Reiner gives an example of how to use "continue N":
  2. # ---------------------------------------------------------

  3. # Suppose I have a large number of jobs that need to be run, with
  4. #+ any data that is to be treated in files of a given name pattern
  5. #+ in a directory. There are several machines that access
  6. #+ this directory, and I want to distribute the work over these
  7. #+ different boxen.
  8. # Then I usually nohup something like the following on every box:

  9. while true
  10. do
  11.   for n in .iso.*
  12.   do
  13.     [ "$n" = ".iso.opts" ] && continue
  14.     beta=${n#.iso.}
  15.     [ -r .Iso.$beta ] && continue
  16.     [ -r .lock.$beta ] && sleep 10 && continue
  17.     lockfile -r0 .lock.$beta || continue
  18.     echo -n "$beta: " `date`
  19.     run-isotherm $beta
  20.     date
  21.     ls -alF .Iso.$beta
  22.     [ -r .Iso.$beta ] && rm -f .lock.$beta
  23.     continue 2
  24.   done
  25.   break
  26. done

  27. exit 0

  28. # The details, in particular the sleep N, are particular to my
  29. #+ application, but the general pattern is:

  30. while true
  31. do
  32.   for job in {pattern}
  33.   do
  34.     {job already done or running} && continue
  35.     {mark job as running, do job, mark job as done}
  36.     continue 2
  37.   done
  38.   break # Or something like `sleep 600' to avoid termination.
  39. done

  40. # This way the script will stop only when there are no more jobs to do
  41. #+ (including jobs that were added during runtime). Through the use
  42. #+ of appropriate lockfiles it can be run on several machines
  43. #+ concurrently without duplication of calculations [which run a couple
  44. #+ of hours in my case, so I really want to avoid this]. Also, as search
  45. #+ always starts again from the beginning, one can encode priorities in
  46. #+ the file names. Of course, one could also do this without `continue 2',
  47. #+ but then one would have to actually check whether or not some job
  48. #+ was done (so that we should immediately look for the next job) or not
  49. #+ (in which case we terminate or sleep for a long time before checking
  50. #+ for a new job).
这个例子保存-- 待用。。。。。。



上一篇:不积跬步,无以至千里。一天做好三件:
下一篇:bash 中的算数运算