File: loopcontrol.html

package info (click to toggle)
abs-guide 10-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,960 kB
  • sloc: sh: 14,129; makefile: 81
file content (629 lines) | stat: -rw-r--r-- 12,209 bytes parent folder | download | duplicates (3)
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%"
>&nbsp;</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&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;LIMIT=19  # Upper limit
   4&nbsp;
   5&nbsp;echo
   6&nbsp;echo "Printing Numbers 1 through 20 (but not 3 and 11)."
   7&nbsp;
   8&nbsp;a=0
   9&nbsp;
  10&nbsp;while [ $a -le "$LIMIT" ]
  11&nbsp;do
  12&nbsp; a=$(($a+1))
  13&nbsp;
  14&nbsp; if [ "$a" -eq 3 ] || [ "$a" -eq 11 ]  # Excludes 3 and 11.
  15&nbsp; then
  16&nbsp;   continue      # Skip rest of this particular loop iteration.
  17&nbsp; fi
  18&nbsp;
  19&nbsp; echo -n "$a "   # This will not execute for 3 and 11.
  20&nbsp;done 
  21&nbsp;
  22&nbsp;# Exercise:
  23&nbsp;# Why does the loop print up to 20?
  24&nbsp;
  25&nbsp;echo; echo
  26&nbsp;
  27&nbsp;echo Printing Numbers 1 through 20, but something happens after 2.
  28&nbsp;
  29&nbsp;##################################################################
  30&nbsp;
  31&nbsp;# Same loop, but substituting 'break' for 'continue'.
  32&nbsp;
  33&nbsp;a=0
  34&nbsp;
  35&nbsp;while [ "$a" -le "$LIMIT" ]
  36&nbsp;do
  37&nbsp; a=$(($a+1))
  38&nbsp;
  39&nbsp; if [ "$a" -gt 2 ]
  40&nbsp; then
  41&nbsp;   break  # Skip entire rest of loop.
  42&nbsp; fi
  43&nbsp;
  44&nbsp; echo -n "$a "
  45&nbsp;done
  46&nbsp;
  47&nbsp;echo; echo; echo
  48&nbsp;
  49&nbsp;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&nbsp;#!/bin/bash
   2&nbsp;# break-levels.sh: Breaking out of loops.
   3&nbsp;
   4&nbsp;# "break N" breaks out of N level loops.
   5&nbsp;
   6&nbsp;for outerloop in 1 2 3 4 5
   7&nbsp;do
   8&nbsp;  echo -n "Group $outerloop:   "
   9&nbsp;
  10&nbsp;  # --------------------------------------------------------
  11&nbsp;  for innerloop in 1 2 3 4 5
  12&nbsp;  do
  13&nbsp;    echo -n "$innerloop "
  14&nbsp;
  15&nbsp;    if [ "$innerloop" -eq 3 ]
  16&nbsp;    then
  17&nbsp;      break  # Try   break 2   to see what happens.
  18&nbsp;             # ("Breaks" out of both inner and outer loops.)
  19&nbsp;    fi
  20&nbsp;  done
  21&nbsp;  # --------------------------------------------------------
  22&nbsp;
  23&nbsp;  echo
  24&nbsp;done  
  25&nbsp;
  26&nbsp;echo
  27&nbsp;
  28&nbsp;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&nbsp;#!/bin/bash
   2&nbsp;# The "continue N" command, continuing at the Nth level loop.
   3&nbsp;
   4&nbsp;for outer in I II III IV V           # outer loop
   5&nbsp;do
   6&nbsp;  echo; echo -n "Group $outer: "
   7&nbsp;
   8&nbsp;  # --------------------------------------------------------------------
   9&nbsp;  for inner in 1 2 3 4 5 6 7 8 9 10  # inner loop
  10&nbsp;  do
  11&nbsp;
  12&nbsp;    if [[ "$inner" -eq 7 &#38;&#38; "$outer" = "III" ]]
  13&nbsp;    then
  14&nbsp;      continue 2  # Continue at loop on 2nd level, that is "outer loop".
  15&nbsp;                  # Replace above line with a simple "continue"
  16&nbsp;                  # to see normal loop behavior.
  17&nbsp;    fi  
  18&nbsp;
  19&nbsp;    echo -n "$inner "  # 7 8 9 10 will not echo on "Group III."
  20&nbsp;  done  
  21&nbsp;  # --------------------------------------------------------------------
  22&nbsp;
  23&nbsp;done
  24&nbsp;
  25&nbsp;echo; echo
  26&nbsp;
  27&nbsp;# Exercise:
  28&nbsp;# Come up with a meaningful use for "continue N" in a script.
  29&nbsp;
  30&nbsp;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&nbsp;# Albert Reiner gives an example of how to use "continue N":
   2&nbsp;# ---------------------------------------------------------
   3&nbsp;
   4&nbsp;#  Suppose I have a large number of jobs that need to be run, with
   5&nbsp;#+ any data that is to be treated in files of a given name pattern
   6&nbsp;#+ in a directory. There are several machines that access
   7&nbsp;#+ this directory, and I want to distribute the work over these
   8&nbsp;#+ different boxen.
   9&nbsp;#  Then I usually nohup something like the following on every box:
  10&nbsp;
  11&nbsp;while true
  12&nbsp;do
  13&nbsp;  for n in .iso.*
  14&nbsp;  do
  15&nbsp;    [ "$n" = ".iso.opts" ] &#38;&#38; continue
  16&nbsp;    beta=${n#.iso.}
  17&nbsp;    [ -r .Iso.$beta ] &#38;&#38; continue
  18&nbsp;    [ -r .lock.$beta ] &#38;&#38; sleep 10 &#38;&#38; continue
  19&nbsp;    lockfile -r0 .lock.$beta || continue
  20&nbsp;    echo -n "$beta: " `date`
  21&nbsp;    run-isotherm $beta
  22&nbsp;    date
  23&nbsp;    ls -alF .Iso.$beta
  24&nbsp;    [ -r .Iso.$beta ] &#38;&#38; rm -f .lock.$beta
  25&nbsp;    continue 2
  26&nbsp;  done
  27&nbsp;  break
  28&nbsp;done
  29&nbsp;
  30&nbsp;exit 0
  31&nbsp;
  32&nbsp;#  The details, in particular the sleep N, are particular to my
  33&nbsp;#+ application, but the general pattern is:
  34&nbsp;
  35&nbsp;while true
  36&nbsp;do
  37&nbsp;  for job in {pattern}
  38&nbsp;  do
  39&nbsp;    {job already done or running} &#38;&#38; continue
  40&nbsp;    {mark job as running, do job, mark job as done}
  41&nbsp;    continue 2
  42&nbsp;  done
  43&nbsp;  break        # Or something like `sleep 600' to avoid termination.
  44&nbsp;done
  45&nbsp;
  46&nbsp;#  This way the script will stop only when there are no more jobs to do
  47&nbsp;#+ (including jobs that were added during runtime). Through the use
  48&nbsp;#+ of appropriate lockfiles it can be run on several machines
  49&nbsp;#+ concurrently without duplication of calculations [which run a couple
  50&nbsp;#+ of hours in my case, so I really want to avoid this]. Also, as search
  51&nbsp;#+ always starts again from the beginning, one can encode priorities in
  52&nbsp;#+ the file names. Of course, one could also do this without `continue 2',
  53&nbsp;#+ but then one would have to actually check whether or not some job
  54&nbsp;#+ was done (so that we should immediately look for the next job) or not
  55&nbsp;#+ (in which case we terminate or sleep for a long time before checking
  56&nbsp;#+ 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
>