1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Loop Control</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Loops and Branches"
HREF="loops.html"><LINK
REL="PREVIOUS"
TITLE="Nested Loops"
HREF="nestedloops.html"><LINK
REL="NEXT"
TITLE="Testing and Branching"
HREF="testbranch.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="nestedloops.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 11. Loops and Branches</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="testbranch.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="LOOPCONTROL"
></A
>11.3. Loop Control</H1
><TABLE
BORDER="0"
WIDTH="100%"
CELLSPACING="0"
CELLPADDING="0"
CLASS="EPIGRAPH"
><TR
><TD
WIDTH="45%"
> </TD
><TD
WIDTH="45%"
ALIGN="LEFT"
VALIGN="TOP"
><I
><P
><I
>Tournez cent tours, tournez mille tours,</I
></P
><P
><I
>Tournez souvent et tournez toujours . . .</I
></P
><P
><I
>--Verlaine, <SPAN
CLASS="QUOTE"
>"Chevaux de bois"</SPAN
></I
></P
></I
></TD
></TR
></TABLE
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="BRKCONT1"
></A
>Commands affecting loop behavior</B
></P
><DL
><DT
><B
CLASS="COMMAND"
>break</B
>, <B
CLASS="COMMAND"
>continue</B
></DT
><DD
><P
>The <B
CLASS="COMMAND"
>break</B
> and <B
CLASS="COMMAND"
>continue</B
>
loop control commands
<A
NAME="AEN6981"
HREF="#FTN.AEN6981"
>[1]</A
>
correspond exactly to their counterparts in other
programming languages. The <B
CLASS="COMMAND"
>break</B
>
command terminates the loop (<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>breaks</I
></SPAN
>
out of it), while <B
CLASS="COMMAND"
>continue</B
> causes a jump
to the next <A
HREF="loops.html#ITERATIONREF"
>iteration</A
>
of the loop, skipping all the remaining commands in that
particular loop cycle.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX28"
></A
><P
><B
>Example 11-21. Effects of <I
CLASS="FIRSTTERM"
>break</I
> and
<B
CLASS="COMMAND"
>continue</B
> in a loop</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 LIMIT=19 # Upper limit
4
5 echo
6 echo "Printing Numbers 1 through 20 (but not 3 and 11)."
7
8 a=0
9
10 while [ $a -le "$LIMIT" ]
11 do
12 a=$(($a+1))
13
14 if [ "$a" -eq 3 ] || [ "$a" -eq 11 ] # Excludes 3 and 11.
15 then
16 continue # Skip rest of this particular loop iteration.
17 fi
18
19 echo -n "$a " # This will not execute for 3 and 11.
20 done
21
22 # Exercise:
23 # Why does the loop print up to 20?
24
25 echo; echo
26
27 echo Printing Numbers 1 through 20, but something happens after 2.
28
29 ##################################################################
30
31 # Same loop, but substituting 'break' for 'continue'.
32
33 a=0
34
35 while [ "$a" -le "$LIMIT" ]
36 do
37 a=$(($a+1))
38
39 if [ "$a" -gt 2 ]
40 then
41 break # Skip entire rest of loop.
42 fi
43
44 echo -n "$a "
45 done
46
47 echo; echo; echo
48
49 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="BREAKPARAM"
></A
></P
><P
>The <B
CLASS="COMMAND"
>break</B
> command may optionally take a
parameter. A plain <B
CLASS="COMMAND"
>break</B
> terminates
only the innermost loop in which it is embedded,
but a <B
CLASS="COMMAND"
>break N</B
> breaks out of
<TT
CLASS="PARAMETER"
><I
>N</I
></TT
> levels of loop.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="BREAKLEVELS"
></A
><P
><B
>Example 11-22. Breaking out of multiple loop levels</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # break-levels.sh: Breaking out of loops.
3
4 # "break N" breaks out of N level loops.
5
6 for outerloop in 1 2 3 4 5
7 do
8 echo -n "Group $outerloop: "
9
10 # --------------------------------------------------------
11 for innerloop in 1 2 3 4 5
12 do
13 echo -n "$innerloop "
14
15 if [ "$innerloop" -eq 3 ]
16 then
17 break # Try break 2 to see what happens.
18 # ("Breaks" out of both inner and outer loops.)
19 fi
20 done
21 # --------------------------------------------------------
22
23 echo
24 done
25
26 echo
27
28 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>The <B
CLASS="COMMAND"
>continue</B
> command, similar to
<B
CLASS="COMMAND"
>break</B
>, optionally takes a parameter. A
plain <B
CLASS="COMMAND"
>continue</B
> cuts short the
current iteration within its loop and begins the next.
A <B
CLASS="COMMAND"
>continue N</B
> terminates all remaining
iterations at its loop level and continues with the
next iteration at the loop, <TT
CLASS="OPTION"
>N</TT
> levels
above.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="CONTINUELEVELS"
></A
><P
><B
>Example 11-23. Continuing at a higher loop level</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 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</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="CONTINUENEX"
></A
><P
><B
>Example 11-24. Using <I
CLASS="FIRSTTERM"
>continue N</I
> in an actual task</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Albert Reiner gives an example of how to use "continue N":
2 # ---------------------------------------------------------
3
4 # Suppose I have a large number of jobs that need to be run, with
5 #+ any data that is to be treated in files of a given name pattern
6 #+ in a directory. There are several machines that access
7 #+ this directory, and I want to distribute the work over these
8 #+ different boxen.
9 # Then I usually nohup something like the following on every box:
10
11 while true
12 do
13 for n in .iso.*
14 do
15 [ "$n" = ".iso.opts" ] && continue
16 beta=${n#.iso.}
17 [ -r .Iso.$beta ] && continue
18 [ -r .lock.$beta ] && sleep 10 && continue
19 lockfile -r0 .lock.$beta || continue
20 echo -n "$beta: " `date`
21 run-isotherm $beta
22 date
23 ls -alF .Iso.$beta
24 [ -r .Iso.$beta ] && rm -f .lock.$beta
25 continue 2
26 done
27 break
28 done
29
30 exit 0
31
32 # The details, in particular the sleep N, are particular to my
33 #+ application, but the general pattern is:
34
35 while true
36 do
37 for job in {pattern}
38 do
39 {job already done or running} && continue
40 {mark job as running, do job, mark job as done}
41 continue 2
42 done
43 break # Or something like `sleep 600' to avoid termination.
44 done
45
46 # This way the script will stop only when there are no more jobs to do
47 #+ (including jobs that were added during runtime). Through the use
48 #+ of appropriate lockfiles it can be run on several machines
49 #+ concurrently without duplication of calculations [which run a couple
50 #+ of hours in my case, so I really want to avoid this]. Also, as search
51 #+ always starts again from the beginning, one can encode priorities in
52 #+ the file names. Of course, one could also do this without `continue 2',
53 #+ but then one would have to actually check whether or not some job
54 #+ was done (so that we should immediately look for the next job) or not
55 #+ (in which case we terminate or sleep for a long time before checking
56 #+ for a new job).</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="CAUTION"
><TABLE
CLASS="CAUTION"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/caution.png"
HSPACE="5"
ALT="Caution"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The <B
CLASS="COMMAND"
>continue N</B
> construct is
difficult to understand and tricky to use in any meaningful
context. It is probably best avoided.</P
></TD
></TR
></TABLE
></DIV
></DD
></DL
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN6981"
HREF="loopcontrol.html#AEN6981"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>These are shell <A
HREF="internal.html#BUILTINREF"
>builtins</A
>,
whereas other loop commands, such as <A
HREF="loops.html#WHILELOOPREF"
>while</A
> and <A
HREF="testbranch.html#CASEESAC1"
>case</A
>, are <A
HREF="internal.html#KEYWORDREF"
>keywords</A
>.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="nestedloops.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="testbranch.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Nested Loops</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="loops.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Testing and Branching</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|