Debian Codegraph @

HTML/list-cons.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >List Constructs</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="Advanced Topics"
15 HREF="part5.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Aliases"
18 HREF="aliases.html"><LINK
19 REL="NEXT"
20 TITLE="Arrays"
21 HREF="arrays.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="CHAPTER"
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="aliases.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="arrays.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="CHAPTER"
92 ><H1
93 ><A
94 NAME="LIST-CONS"
95 ></A
96 >Chapter 26. List Constructs</H1
97 ><P
98 ><A
99 NAME="LISTCONSREF"
100 ></A
101 ></P
102 ><P
103 >The <I
104 CLASS="FIRSTTERM"
105 >and list</I
106 > and <I
107 CLASS="FIRSTTERM"
108 >or
109 list</I
110 > constructs provide a means of processing a
111 number of commands consecutively. These can effectively replace
112 complex nested <A
113 HREF="tests.html#TESTCONSTRUCTS1"
114 >if/then</A
115 >
116 or even <A
117 HREF="testbranch.html#CASEESAC1"
118 >case</A
119 > statements.</P
120 ><DIV
121 CLASS="VARIABLELIST"
122 ><P
123 ><B
124 ><A
125 NAME="LCONS1"
126 ></A
127 >Chaining together commands</B
128 ></P
129 ><DL
130 ><DT
131 >and list</DT
132 ><DD
133 ><P
134 ><TABLE
135 BORDER="0"
136 BGCOLOR="#E0E0E0"
137 WIDTH="90%"
138 ><TR
139 ><TD
140 ><PRE
141 CLASS="PROGRAMLISTING"
142 > 1&nbsp;command-1 &#38;&#38; command-2 &#38;&#38; command-3 &#38;&#38; ... command-n</PRE
143 ></TD
144 ></TR
145 ></TABLE
146 >
147 Each command executes in turn, provided that
148 the previous command has given a return value of
149 <TT
150 CLASS="REPLACEABLE"
151 ><I
152 >true</I
153 ></TT
154 > (zero). At the first
155 <TT
156 CLASS="REPLACEABLE"
157 ><I
158 >false</I
159 ></TT
160 > (non-zero) return, the
161 command chain terminates (the first command returning
162 <TT
163 CLASS="REPLACEABLE"
164 ><I
165 >false</I
166 ></TT
167 > is the last one to
168 execute).</P
169 ><P
170 >An interesting use of a two-condition <I
171 CLASS="FIRSTTERM"
172 >and
173 list</I
174 > from an early version of YongYe's <A
175 HREF="http://bash.deta.in/Tetris_Game.sh"
176 TARGET="_top"
177 >Tetris
178 game script</A
179 >:</P
180 ><P
181 > <TABLE
182 BORDER="0"
183 BGCOLOR="#E0E0E0"
184 WIDTH="90%"
185 ><TR
186 ><TD
187 ><PRE
188 CLASS="PROGRAMLISTING"
189 > 1&nbsp;equation()
190 2&nbsp;
191 3&nbsp;{ # core algorithm used for doubling and halving the coordinates
192 4&nbsp; [[ ${cdx} ]] &#38;&#38; ((y=cy+(ccy-cdy)${2}2))
193 5&nbsp; eval ${1}+=\"${x} ${y} \"
194 6&nbsp;}</PRE
195 ></TD
196 ></TR
197 ></TABLE
198 >
199 </P
200 ><DIV
201 CLASS="EXAMPLE"
202 ><HR><A
203 NAME="EX64"
204 ></A
205 ><P
206 ><B
207 >Example 26-1. Using an <I
208 CLASS="FIRSTTERM"
209 >and list</I
210 > to test
211 for command-line arguments</B
212 ></P
213 ><TABLE
214 BORDER="0"
215 BGCOLOR="#E0E0E0"
216 WIDTH="90%"
217 ><TR
218 ><TD
219 ><PRE
220 CLASS="PROGRAMLISTING"
221 > 1&nbsp;#!/bin/bash
222 2&nbsp;# and list
223 3&nbsp;
224 4&nbsp;if [ ! -z "$1" ] &#38;&#38; echo "Argument #1 = $1" &#38;&#38; [ ! -z "$2" ] &#38;&#38; \
225 5&nbsp;# ^^ ^^ ^^
226 6&nbsp;echo "Argument #2 = $2"
227 7&nbsp;then
228 8&nbsp; echo "At least 2 arguments passed to script."
229 9&nbsp; # All the chained commands return true.
230 10&nbsp;else
231 11&nbsp; echo "Fewer than 2 arguments passed to script."
232 12&nbsp; # At least one of the chained commands returns false.
233 13&nbsp;fi
234 14&nbsp;# Note that "if [ ! -z $1 ]" works, but its alleged equivalent,
235 15&nbsp;# "if [ -n $1 ]" does not.
236 16&nbsp;# However, quoting fixes this.
237 17&nbsp;# if "[ -n "$1" ]" works.
238 18&nbsp;# ^ ^ Careful!
239 19&nbsp;# It is always best to QUOTE the variables being tested.
240 20&nbsp;
241 21&nbsp;
242 22&nbsp;# This accomplishes the same thing, using "pure" if/then statements.
243 23&nbsp;if [ ! -z "$1" ]
244 24&nbsp;then
245 25&nbsp; echo "Argument #1 = $1"
246 26&nbsp;fi
247 27&nbsp;if [ ! -z "$2" ]
248 28&nbsp;then
249 29&nbsp; echo "Argument #2 = $2"
250 30&nbsp; echo "At least 2 arguments passed to script."
251 31&nbsp;else
252 32&nbsp; echo "Fewer than 2 arguments passed to script."
253 33&nbsp;fi
254 34&nbsp;# It's longer and more ponderous than using an "and list".
255 35&nbsp;
256 36&nbsp;
257 37&nbsp;exit $?</PRE
258 ></TD
259 ></TR
260 ></TABLE
261 ><HR></DIV
262 ><DIV
263 CLASS="EXAMPLE"
264 ><HR><A
265 NAME="ANDLIST2"
266 ></A
267 ><P
268 ><B
269 >Example 26-2. Another command-line arg test using an <I
270 CLASS="FIRSTTERM"
271 >and
272 list</I
273 ></B
274 ></P
275 ><TABLE
276 BORDER="0"
277 BGCOLOR="#E0E0E0"
278 WIDTH="90%"
279 ><TR
280 ><TD
281 ><PRE
282 CLASS="PROGRAMLISTING"
283 > 1&nbsp;#!/bin/bash
284 2&nbsp;
285 3&nbsp;ARGS=1 # Number of arguments expected.
286 4&nbsp;E_BADARGS=85 # Exit value if incorrect number of args passed.
287 5&nbsp;
288 6&nbsp;test $# -ne $ARGS &#38;&#38; \
289 7&nbsp;# ^^^^^^^^^^^^ condition #1
290 8&nbsp;echo "Usage: `basename $0` $ARGS argument(s)" &#38;&#38; exit $E_BADARGS
291 9&nbsp;# ^^
292 10&nbsp;# If condition #1 tests true (wrong number of args passed to script),
293 11&nbsp;#+ then the rest of the line executes, and script terminates.
294 12&nbsp;
295 13&nbsp;# Line below executes only if the above test fails.
296 14&nbsp;echo "Correct number of arguments passed to this script."
297 15&nbsp;
298 16&nbsp;exit 0
299 17&nbsp;
300 18&nbsp;# To check exit value, do a "echo $?" after script termination.</PRE
301 ></TD
302 ></TR
303 ></TABLE
304 ><HR></DIV
305 ><P
306 ><A
307 NAME="ANDDEFAULT"
308 ></A
309 ></P
310 ><P
311 > Of course, an <I
312 CLASS="FIRSTTERM"
313 >and list</I
314 > can also
315 <I
316 CLASS="FIRSTTERM"
317 >set</I
318 > variables to a default value.
319 <TABLE
320 BORDER="0"
321 BGCOLOR="#E0E0E0"
322 WIDTH="90%"
323 ><TR
324 ><TD
325 ><PRE
326 CLASS="PROGRAMLISTING"
327 > 1&nbsp;arg1=$@ &#38;&#38; [ -z "$arg1" ] &#38;&#38; arg1=DEFAULT
328 2&nbsp;
329 3&nbsp; # Set $arg1 to command-line arguments, if any.
330 4&nbsp; # But . . . set to DEFAULT if not specified on command-line.</PRE
331 ></TD
332 ></TR
333 ></TABLE
334 >
335 </P
336 ></DD
337 ><DT
338 ><A
339 NAME="ORLISTREF"
340 ></A
341 >or list</DT
342 ><DD
343 ><P
344 ><TABLE
345 BORDER="0"
346 BGCOLOR="#E0E0E0"
347 WIDTH="90%"
348 ><TR
349 ><TD
350 ><PRE
351 CLASS="PROGRAMLISTING"
352 > 1&nbsp;command-1 || command-2 || command-3 || ... command-n</PRE
353 ></TD
354 ></TR
355 ></TABLE
356 >
357 Each command executes in turn for as long as the previous
358 command returns <SPAN
359 CLASS="RETURNVALUE"
360 >false</SPAN
361 >. At
362 the first <SPAN
363 CLASS="RETURNVALUE"
364 >true</SPAN
365 > return, the
366 command chain terminates (the first command returning
367 <SPAN
368 CLASS="RETURNVALUE"
369 >true</SPAN
370 > is the last one to
371 execute). This is obviously the inverse of the <SPAN
372 CLASS="QUOTE"
373 >"and
374 list"</SPAN
375 >.</P
376 ><DIV
377 CLASS="EXAMPLE"
378 ><HR><A
379 NAME="EX65"
380 ></A
381 ><P
382 ><B
383 >Example 26-3. Using <I
384 CLASS="FIRSTTERM"
385 >or lists</I
386 > in combination
387 with an <I
388 CLASS="FIRSTTERM"
389 >and list</I
390 ></B
391 ></P
392 ><TABLE
393 BORDER="0"
394 BGCOLOR="#E0E0E0"
395 WIDTH="90%"
396 ><TR
397 ><TD
398 ><PRE
399 CLASS="PROGRAMLISTING"
400 > 1&nbsp;#!/bin/bash
401 2&nbsp;
402 3&nbsp;# delete.sh, a not-so-cunning file deletion utility.
403 4&nbsp;# Usage: delete filename
404 5&nbsp;
405 6&nbsp;E_BADARGS=85
406 7&nbsp;
407 8&nbsp;if [ -z "$1" ]
408 9&nbsp;then
409 10&nbsp; echo "Usage: `basename $0` filename"
410 11&nbsp; exit $E_BADARGS # No arg? Bail out.
411 12&nbsp;else
412 13&nbsp; file=$1 # Set filename.
413 14&nbsp;fi
414 15&nbsp;
415 16&nbsp;
416 17&nbsp;[ ! -f "$file" ] &#38;&#38; echo "File \"$file\" not found. \
417 18&nbsp;Cowardly refusing to delete a nonexistent file."
418 19&nbsp;# AND LIST, to give error message if file not present.
419 20&nbsp;# Note echo message continuing on to a second line after an escape.
420 21&nbsp;
421 22&nbsp;[ ! -f "$file" ] || (rm -f $file; echo "File \"$file\" deleted.")
422 23&nbsp;# OR LIST, to delete file if present.
423 24&nbsp;
424 25&nbsp;# Note logic inversion above.
425 26&nbsp;# AND LIST executes on true, OR LIST on false.
426 27&nbsp;
427 28&nbsp;exit $?</PRE
428 ></TD
429 ></TR
430 ></TABLE
431 ><HR></DIV
432 ><DIV
433 CLASS="CAUTION"
434 ><TABLE
435 CLASS="CAUTION"
436 WIDTH="90%"
437 BORDER="0"
438 ><TR
439 ><TD
440 WIDTH="25"
441 ALIGN="CENTER"
442 VALIGN="TOP"
443 ><IMG
444 SRC="common/caution.png"
445 HSPACE="5"
446 ALT="Caution"></TD
447 ><TD
448 ALIGN="LEFT"
449 VALIGN="TOP"
450 ><P
451 >If the first command in an <I
452 CLASS="FIRSTTERM"
453 >or
454 list</I
455 > returns <SPAN
456 CLASS="RETURNVALUE"
457 >true</SPAN
458 >,
459 it <TT
460 CLASS="REPLACEABLE"
461 ><I
462 >will</I
463 ></TT
464 > execute.</P
465 ></TD
466 ></TR
467 ></TABLE
468 ></DIV
469 ></DD
470 ></DL
471 ></DIV
472 ><P
473 ><TABLE
474 BORDER="0"
475 BGCOLOR="#E0E0E0"
476 WIDTH="100%"
477 ><TR
478 ><TD
479 ><PRE
480 CLASS="PROGRAMLISTING"
481 > 1&nbsp;# ==&#62; The following snippets from the /etc/rc.d/init.d/single
482 2&nbsp;#+==&#62; script by Miquel van Smoorenburg
483 3&nbsp;#+==&#62; illustrate use of "and" and "or" lists.
484 4&nbsp;# ==&#62; "Arrowed" comments added by document author.
485 5&nbsp;
486 6&nbsp;[ -x /usr/bin/clear ] &#38;&#38; /usr/bin/clear
487 7&nbsp; # ==&#62; If /usr/bin/clear exists, then invoke it.
488 8&nbsp; # ==&#62; Checking for the existence of a command before calling it
489 9&nbsp; #+==&#62; avoids error messages and other awkward consequences.
490 10&nbsp;
491 11&nbsp; # ==&#62; . . .
492 12&nbsp;
493 13&nbsp;# If they want to run something in single user mode, might as well run it...
494 14&nbsp;for i in /etc/rc1.d/S[0-9][0-9]* ; do
495 15&nbsp; # Check if the script is there.
496 16&nbsp; [ -x "$i" ] || continue
497 17&nbsp; # ==&#62; If corresponding file in $PWD *not* found,
498 18&nbsp; #+==&#62; then "continue" by jumping to the top of the loop.
499 19&nbsp;
500 20&nbsp; # Reject backup files and files generated by rpm.
501 21&nbsp; case "$1" in
502 22&nbsp; *.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig)
503 23&nbsp; continue;;
504 24&nbsp; esac
505 25&nbsp; [ "$i" = "/etc/rc1.d/S00single" ] &#38;&#38; continue
506 26&nbsp; # ==&#62; Set script name, but don't execute it yet.
507 27&nbsp; $i start
508 28&nbsp;done
509 29&nbsp;
510 30&nbsp; # ==&#62; . . .</PRE
511 ></TD
512 ></TR
513 ></TABLE
514 ></P
515 ><DIV
516 CLASS="IMPORTANT"
517 ><TABLE
518 CLASS="IMPORTANT"
519 WIDTH="100%"
520 BORDER="0"
521 ><TR
522 ><TD
523 WIDTH="25"
524 ALIGN="CENTER"
525 VALIGN="TOP"
526 ><IMG
527 SRC="common/important.png"
528 HSPACE="5"
529 ALT="Important"></TD
530 ><TD
531 ALIGN="LEFT"
532 VALIGN="TOP"
533 ><P
534 >The <A
535 HREF="exit-status.html#EXITSTATUSREF"
536 >exit
537 status</A
538 > of an <TT
539 CLASS="USERINPUT"
540 ><B
541 >and list</B
542 ></TT
543 > or an
544 <TT
545 CLASS="USERINPUT"
546 ><B
547 >or list</B
548 ></TT
549 > is the exit status of the last
550 command executed.</P
551 ></TD
552 ></TR
553 ></TABLE
554 ></DIV
555 ><P
556 >Clever combinations of <I
557 CLASS="FIRSTTERM"
558 >and</I
559 > and
560 <I
561 CLASS="FIRSTTERM"
562 >or</I
563 > lists are possible, but the logic may
564 easily become convoluted and require close attention to <A
565 HREF="opprecedence.html#OPPRECEDENCE1"
566 >operator precedence rules</A
567 >, and
568 possibly extensive debugging.</P
569 ><P
570 ><TABLE
571 BORDER="0"
572 BGCOLOR="#E0E0E0"
573 WIDTH="100%"
574 ><TR
575 ><TD
576 ><PRE
577 CLASS="PROGRAMLISTING"
578 > 1&nbsp;false &#38;&#38; true || echo false # false
579 2&nbsp;
580 3&nbsp;# Same result as
581 4&nbsp;( false &#38;&#38; true ) || echo false # false
582 5&nbsp;# But NOT
583 6&nbsp;false &#38;&#38; ( true || echo false ) # (nothing echoed)
584 7&nbsp;
585 8&nbsp;# Note left-to-right grouping and evaluation of statements.
586 9&nbsp;
587 10&nbsp;# It's usually best to avoid such complexities.
588 11&nbsp;
589 12&nbsp;# Thanks, S.C.</PRE
590 ></TD
591 ></TR
592 ></TABLE
593 >
594 </P
595 ><P
596 >See <A
597 HREF="contributed-scripts.html#DAYSBETWEEN"
598 >Example A-7</A
599 > and <A
600 HREF="fto.html#BROKENLINK"
601 >Example 7-4</A
602 > for illustrations of using <TT
603 CLASS="USERINPUT"
604 ><B
605 >and
606 / or list</B
607 ></TT
608 > constructs to test variables.</P
609 ></DIV
610 ><DIV
611 CLASS="NAVFOOTER"
612 ><HR
613 ALIGN="LEFT"
614 WIDTH="100%"><TABLE
615 SUMMARY="Footer navigation table"
616 WIDTH="100%"
617 BORDER="0"
618 CELLPADDING="0"
619 CELLSPACING="0"
620 ><TR
621 ><TD
622 WIDTH="33%"
623 ALIGN="left"
624 VALIGN="top"
625 ><A
626 HREF="aliases.html"
627 ACCESSKEY="P"
628 >Prev</A
629 ></TD
630 ><TD
631 WIDTH="34%"
632 ALIGN="center"
633 VALIGN="top"
634 ><A
635 HREF="index.html"
636 ACCESSKEY="H"
637 >Home</A
638 ></TD
639 ><TD
640 WIDTH="33%"
641 ALIGN="right"
642 VALIGN="top"
643 ><A
644 HREF="arrays.html"
645 ACCESSKEY="N"
646 >Next</A
647 ></TD
648 ></TR
649 ><TR
650 ><TD
651 WIDTH="33%"
652 ALIGN="left"
653 VALIGN="top"
654 >Aliases</TD
655 ><TD
656 WIDTH="34%"
657 ALIGN="center"
658 VALIGN="top"
659 ><A
660 HREF="part5.html"
661 ACCESSKEY="U"
662 >Up</A
663 ></TD
664 ><TD
665 WIDTH="33%"
666 ALIGN="right"
667 VALIGN="top"
668 >Arrays</TD
669 ></TR
670 ></TABLE
671 ></DIV
672 ></BODY
673 ></HTML
674 >