Debian Codegraph @

HTML/loopcontrol.html

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