| 1 | #!/bin/bash |
| 2 | # The "continue N" command, continuing at the Nth level loop. |
| 3 | |
| 4 | for outer in I II III IV V # outer loop |
| 5 | do |
| 6 | echo; echo -n "Group $outer: " |
| 7 | |
| 8 | # -------------------------------------------------------------------- |
| 9 | for inner in 1 2 3 4 5 6 7 8 9 10 # inner loop |
| 10 | do |
| 11 | |
| 12 | if [[ "$inner" -eq 7 && "$outer" = "III" ]] |
| 13 | then |
| 14 | continue 2 # Continue at loop on 2nd level, that is "outer loop". |
| 15 | # Replace above line with a simple "continue" |
| 16 | # to see normal loop behavior. |
| 17 | fi |
| 18 | |
| 19 | echo -n "$inner " # 7 8 9 10 will not echo on "Group III." |
| 20 | done |
| 21 | # -------------------------------------------------------------------- |
| 22 | |
| 23 | done |
| 24 | |
| 25 | echo; echo |
| 26 | |
| 27 | # Exercise: |
| 28 | # Come up with a meaningful use for "continue N" in a script. |
| 29 | |
| 30 | exit 0 |