Debian Codegraph @

HTML/redircb.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Redirecting Code Blocks</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="I/O Redirection"
15 HREF="io-redirection.html"><LINK
16 REL="PREVIOUS"
17 TITLE="I/O Redirection"
18 HREF="io-redirection.html"><LINK
19 REL="NEXT"
20 TITLE="Applications"
21 HREF="redirapps.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="io-redirection.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 20. I/O Redirection</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="redirapps.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="REDIRCB"
96 ></A
97 >20.2. Redirecting Code Blocks</H1
98 ><P
99 ><A
100 NAME="REDIRREF"
101 ></A
102 >Blocks of code, such as <A
103 HREF="loops.html#WHILELOOPREF"
104 >while</A
105 >, <A
106 HREF="loops.html#UNTILLOOPREF"
107 >until</A
108 >, and <A
109 HREF="loops.html#FORLOOPREF1"
110 >for</A
111 > loops, even <A
112 HREF="tests.html#IFTHEN"
113 >if/then</A
114 > test blocks can also incorporate
115 redirection of <TT
116 CLASS="FILENAME"
117 >stdin</TT
118 >. Even a function may
119 use this form of redirection (see <A
120 HREF="functions.html#REALNAME"
121 >Example 24-11</A
122 >).
123 The <SPAN
124 CLASS="TOKEN"
125 >&#60;</SPAN
126 > operator at the end of the code block
127 accomplishes this.</P
128 ><DIV
129 CLASS="EXAMPLE"
130 ><HR><A
131 NAME="REDIR2"
132 ></A
133 ><P
134 ><B
135 >Example 20-5. Redirected <I
136 CLASS="FIRSTTERM"
137 >while</I
138 > loop</B
139 ></P
140 ><TABLE
141 BORDER="0"
142 BGCOLOR="#E0E0E0"
143 WIDTH="100%"
144 ><TR
145 ><TD
146 ><PRE
147 CLASS="PROGRAMLISTING"
148 > 1&nbsp;#!/bin/bash
149 2&nbsp;# redir2.sh
150 3&nbsp;
151 4&nbsp;if [ -z "$1" ]
152 5&nbsp;then
153 6&nbsp; Filename=names.data # Default, if no filename specified.
154 7&nbsp;else
155 8&nbsp; Filename=$1
156 9&nbsp;fi
157 10&nbsp;#+ Filename=${1:-names.data}
158 11&nbsp;# can replace the above test (parameter substitution).
159 12&nbsp;
160 13&nbsp;count=0
161 14&nbsp;
162 15&nbsp;echo
163 16&nbsp;
164 17&nbsp;while [ "$name" != Smith ] # Why is variable $name in quotes?
165 18&nbsp;do
166 19&nbsp; read name # Reads from $Filename, rather than stdin.
167 20&nbsp; echo $name
168 21&nbsp; let "count += 1"
169 22&nbsp;done &#60;"$Filename" # Redirects stdin to file $Filename.
170 23&nbsp;# ^^^^^^^^^^^^
171 24&nbsp;
172 25&nbsp;echo; echo "$count names read"; echo
173 26&nbsp;
174 27&nbsp;exit 0
175 28&nbsp;
176 29&nbsp;# Note that in some older shell scripting languages,
177 30&nbsp;#+ the redirected loop would run as a subshell.
178 31&nbsp;# Therefore, $count would return 0, the initialized value outside the loop.
179 32&nbsp;# Bash and ksh avoid starting a subshell *whenever possible*,
180 33&nbsp;#+ so that this script, for example, runs correctly.
181 34&nbsp;# (Thanks to Heiner Steven for pointing this out.)
182 35&nbsp;
183 36&nbsp;# However . . .
184 37&nbsp;# Bash *can* sometimes start a subshell in a PIPED "while-read" loop,
185 38&nbsp;#+ as distinct from a REDIRECTED "while" loop.
186 39&nbsp;
187 40&nbsp;abc=hi
188 41&nbsp;echo -e "1\n2\n3" | while read l
189 42&nbsp; do abc="$l"
190 43&nbsp; echo $abc
191 44&nbsp; done
192 45&nbsp;echo $abc
193 46&nbsp;
194 47&nbsp;# Thanks, Bruno de Oliveira Schneider, for demonstrating this
195 48&nbsp;#+ with the above snippet of code.
196 49&nbsp;# And, thanks, Brian Onn, for correcting an annotation error.</PRE
197 ></TD
198 ></TR
199 ></TABLE
200 ><HR></DIV
201 ><DIV
202 CLASS="EXAMPLE"
203 ><HR><A
204 NAME="REDIR2A"
205 ></A
206 ><P
207 ><B
208 >Example 20-6. Alternate form of redirected <I
209 CLASS="FIRSTTERM"
210 >while</I
211 > loop</B
212 ></P
213 ><TABLE
214 BORDER="0"
215 BGCOLOR="#E0E0E0"
216 WIDTH="100%"
217 ><TR
218 ><TD
219 ><PRE
220 CLASS="PROGRAMLISTING"
221 > 1&nbsp;#!/bin/bash
222 2&nbsp;
223 3&nbsp;# This is an alternate form of the preceding script.
224 4&nbsp;
225 5&nbsp;# Suggested by Heiner Steven
226 6&nbsp;#+ as a workaround in those situations when a redirect loop
227 7&nbsp;#+ runs as a subshell, and therefore variables inside the loop
228 8&nbsp;# +do not keep their values upon loop termination.
229 9&nbsp;
230 10&nbsp;
231 11&nbsp;if [ -z "$1" ]
232 12&nbsp;then
233 13&nbsp; Filename=names.data # Default, if no filename specified.
234 14&nbsp;else
235 15&nbsp; Filename=$1
236 16&nbsp;fi
237 17&nbsp;
238 18&nbsp;
239 19&nbsp;exec 3&#60;&#38;0 # Save stdin to file descriptor 3.
240 20&nbsp;exec 0&#60;"$Filename" # Redirect standard input.
241 21&nbsp;
242 22&nbsp;count=0
243 23&nbsp;echo
244 24&nbsp;
245 25&nbsp;
246 26&nbsp;while [ "$name" != Smith ]
247 27&nbsp;do
248 28&nbsp; read name # Reads from redirected stdin ($Filename).
249 29&nbsp; echo $name
250 30&nbsp; let "count += 1"
251 31&nbsp;done # Loop reads from file $Filename
252 32&nbsp; #+ because of line 20.
253 33&nbsp;
254 34&nbsp;# The original version of this script terminated the "while" loop with
255 35&nbsp;#+ done &#60;"$Filename"
256 36&nbsp;# Exercise:
257 37&nbsp;# Why is this unnecessary?
258 38&nbsp;
259 39&nbsp;
260 40&nbsp;exec 0&#60;&#38;3 # Restore old stdin.
261 41&nbsp;exec 3&#60;&#38;- # Close temporary fd 3.
262 42&nbsp;
263 43&nbsp;echo; echo "$count names read"; echo
264 44&nbsp;
265 45&nbsp;exit 0</PRE
266 ></TD
267 ></TR
268 ></TABLE
269 ><HR></DIV
270 ><DIV
271 CLASS="EXAMPLE"
272 ><HR><A
273 NAME="REDIR3"
274 ></A
275 ><P
276 ><B
277 >Example 20-7. Redirected <I
278 CLASS="FIRSTTERM"
279 >until</I
280 > loop</B
281 ></P
282 ><TABLE
283 BORDER="0"
284 BGCOLOR="#E0E0E0"
285 WIDTH="100%"
286 ><TR
287 ><TD
288 ><PRE
289 CLASS="PROGRAMLISTING"
290 > 1&nbsp;#!/bin/bash
291 2&nbsp;# Same as previous example, but with "until" loop.
292 3&nbsp;
293 4&nbsp;if [ -z "$1" ]
294 5&nbsp;then
295 6&nbsp; Filename=names.data # Default, if no filename specified.
296 7&nbsp;else
297 8&nbsp; Filename=$1
298 9&nbsp;fi
299 10&nbsp;
300 11&nbsp;# while [ "$name" != Smith ]
301 12&nbsp;until [ "$name" = Smith ] # Change != to =.
302 13&nbsp;do
303 14&nbsp; read name # Reads from $Filename, rather than stdin.
304 15&nbsp; echo $name
305 16&nbsp;done &#60;"$Filename" # Redirects stdin to file $Filename.
306 17&nbsp;# ^^^^^^^^^^^^
307 18&nbsp;
308 19&nbsp;# Same results as with "while" loop in previous example.
309 20&nbsp;
310 21&nbsp;exit 0</PRE
311 ></TD
312 ></TR
313 ></TABLE
314 ><HR></DIV
315 ><DIV
316 CLASS="EXAMPLE"
317 ><HR><A
318 NAME="REDIR4"
319 ></A
320 ><P
321 ><B
322 >Example 20-8. Redirected <I
323 CLASS="FIRSTTERM"
324 >for</I
325 > loop</B
326 ></P
327 ><TABLE
328 BORDER="0"
329 BGCOLOR="#E0E0E0"
330 WIDTH="100%"
331 ><TR
332 ><TD
333 ><PRE
334 CLASS="PROGRAMLISTING"
335 > 1&nbsp;#!/bin/bash
336 2&nbsp;
337 3&nbsp;if [ -z "$1" ]
338 4&nbsp;then
339 5&nbsp; Filename=names.data # Default, if no filename specified.
340 6&nbsp;else
341 7&nbsp; Filename=$1
342 8&nbsp;fi
343 9&nbsp;
344 10&nbsp;line_count=`wc $Filename | awk '{ print $1 }'`
345 11&nbsp;# Number of lines in target file.
346 12&nbsp;#
347 13&nbsp;# Very contrived and kludgy, nevertheless shows that
348 14&nbsp;#+ it's possible to redirect stdin within a "for" loop...
349 15&nbsp;#+ if you're clever enough.
350 16&nbsp;#
351 17&nbsp;# More concise is line_count=$(wc -l &#60; "$Filename")
352 18&nbsp;
353 19&nbsp;
354 20&nbsp;for name in `seq $line_count` # Recall that "seq" prints sequence of numbers.
355 21&nbsp;# while [ "$name" != Smith ] -- more complicated than a "while" loop --
356 22&nbsp;do
357 23&nbsp; read name # Reads from $Filename, rather than stdin.
358 24&nbsp; echo $name
359 25&nbsp; if [ "$name" = Smith ] # Need all this extra baggage here.
360 26&nbsp; then
361 27&nbsp; break
362 28&nbsp; fi
363 29&nbsp;done &#60;"$Filename" # Redirects stdin to file $Filename.
364 30&nbsp;# ^^^^^^^^^^^^
365 31&nbsp;
366 32&nbsp;exit 0</PRE
367 ></TD
368 ></TR
369 ></TABLE
370 ><HR></DIV
371 ><P
372 >We can modify the previous example to also redirect the output of
373 the loop.</P
374 ><DIV
375 CLASS="EXAMPLE"
376 ><HR><A
377 NAME="REDIR4A"
378 ></A
379 ><P
380 ><B
381 >Example 20-9. Redirected <I
382 CLASS="FIRSTTERM"
383 >for</I
384 > loop (both
385 <TT
386 CLASS="FILENAME"
387 >stdin</TT
388 > and <TT
389 CLASS="FILENAME"
390 >stdout</TT
391 >
392 redirected)</B
393 ></P
394 ><TABLE
395 BORDER="0"
396 BGCOLOR="#E0E0E0"
397 WIDTH="100%"
398 ><TR
399 ><TD
400 ><PRE
401 CLASS="PROGRAMLISTING"
402 > 1&nbsp;#!/bin/bash
403 2&nbsp;
404 3&nbsp;if [ -z "$1" ]
405 4&nbsp;then
406 5&nbsp; Filename=names.data # Default, if no filename specified.
407 6&nbsp;else
408 7&nbsp; Filename=$1
409 8&nbsp;fi
410 9&nbsp;
411 10&nbsp;Savefile=$Filename.new # Filename to save results in.
412 11&nbsp;FinalName=Jonah # Name to terminate "read" on.
413 12&nbsp;
414 13&nbsp;line_count=`wc $Filename | awk '{ print $1 }'` # Number of lines in target file.
415 14&nbsp;
416 15&nbsp;
417 16&nbsp;for name in `seq $line_count`
418 17&nbsp;do
419 18&nbsp; read name
420 19&nbsp; echo "$name"
421 20&nbsp; if [ "$name" = "$FinalName" ]
422 21&nbsp; then
423 22&nbsp; break
424 23&nbsp; fi
425 24&nbsp;done &#60; "$Filename" &#62; "$Savefile" # Redirects stdin to file $Filename,
426 25&nbsp;# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ and saves it to backup file.
427 26&nbsp;
428 27&nbsp;exit 0</PRE
429 ></TD
430 ></TR
431 ></TABLE
432 ><HR></DIV
433 ><DIV
434 CLASS="EXAMPLE"
435 ><HR><A
436 NAME="REDIR5"
437 ></A
438 ><P
439 ><B
440 >Example 20-10. Redirected <I
441 CLASS="FIRSTTERM"
442 >if/then</I
443 > test</B
444 ></P
445 ><TABLE
446 BORDER="0"
447 BGCOLOR="#E0E0E0"
448 WIDTH="100%"
449 ><TR
450 ><TD
451 ><PRE
452 CLASS="PROGRAMLISTING"
453 > 1&nbsp;#!/bin/bash
454 2&nbsp;
455 3&nbsp;if [ -z "$1" ]
456 4&nbsp;then
457 5&nbsp; Filename=names.data # Default, if no filename specified.
458 6&nbsp;else
459 7&nbsp; Filename=$1
460 8&nbsp;fi
461 9&nbsp;
462 10&nbsp;TRUE=1
463 11&nbsp;
464 12&nbsp;if [ "$TRUE" ] # if true and if : also work.
465 13&nbsp;then
466 14&nbsp; read name
467 15&nbsp; echo $name
468 16&nbsp;fi &#60;"$Filename"
469 17&nbsp;# ^^^^^^^^^^^^
470 18&nbsp;
471 19&nbsp;# Reads only first line of file.
472 20&nbsp;# An "if/then" test has no way of iterating unless embedded in a loop.
473 21&nbsp;
474 22&nbsp;exit 0</PRE
475 ></TD
476 ></TR
477 ></TABLE
478 ><HR></DIV
479 ><DIV
480 CLASS="EXAMPLE"
481 ><HR><A
482 NAME="NAMESDATA"
483 ></A
484 ><P
485 ><B
486 >Example 20-11. Data file <I
487 CLASS="FIRSTTERM"
488 >names.data</I
489 > for above
490 examples</B
491 ></P
492 ><TABLE
493 BORDER="0"
494 BGCOLOR="#E0E0E0"
495 WIDTH="100%"
496 ><TR
497 ><TD
498 ><PRE
499 CLASS="PROGRAMLISTING"
500 > 1&nbsp;Aristotle
501 2&nbsp;Arrhenius
502 3&nbsp;Belisarius
503 4&nbsp;Capablanca
504 5&nbsp;Dickens
505 6&nbsp;Euler
506 7&nbsp;Goethe
507 8&nbsp;Hegel
508 9&nbsp;Jonah
509 10&nbsp;Laplace
510 11&nbsp;Maroczy
511 12&nbsp;Purcell
512 13&nbsp;Schmidt
513 14&nbsp;Schopenhauer
514 15&nbsp;Semmelweiss
515 16&nbsp;Smith
516 17&nbsp;Steinmetz
517 18&nbsp;Tukhashevsky
518 19&nbsp;Turing
519 20&nbsp;Venn
520 21&nbsp;Warshawski
521 22&nbsp;Znosko-Borowski
522 23&nbsp;
523 24&nbsp;# This is a data file for
524 25&nbsp;#+ "redir2.sh", "redir3.sh", "redir4.sh", "redir4a.sh", "redir5.sh".</PRE
525 ></TD
526 ></TR
527 ></TABLE
528 ><HR></DIV
529 ><P
530 >Redirecting the <TT
531 CLASS="FILENAME"
532 >stdout</TT
533 > of a code
534 block has the effect of saving its output to a file. See <A
535 HREF="special-chars.html#RPMCHECK"
536 >Example 3-2</A
537 >.</P
538 ><P
539 ><A
540 HREF="here-docs.html#HEREDOCREF"
541 >Here documents</A
542 >
543 are a special case of redirected code blocks. That being the case,
544 it should be possible to feed the output of a <I
545 CLASS="FIRSTTERM"
546 >here
547 document</I
548 > into the <TT
549 CLASS="FILENAME"
550 >stdin</TT
551 > for a
552 <I
553 CLASS="FIRSTTERM"
554 >while loop</I
555 >.</P
556 ><P
557 > <TABLE
558 BORDER="0"
559 BGCOLOR="#E0E0E0"
560 WIDTH="100%"
561 ><TR
562 ><TD
563 ><PRE
564 CLASS="PROGRAMLISTING"
565 > 1&nbsp;# This example by Albert Siersema
566 2&nbsp;# Used with permission (thanks!).
567 3&nbsp;
568 4&nbsp;function doesOutput()
569 5&nbsp; # Could be an external command too, of course.
570 6&nbsp; # Here we show you can use a function as well.
571 7&nbsp;{
572 8&nbsp; ls -al *.jpg | awk '{print $5,$9}'
573 9&nbsp;}
574 10&nbsp;
575 11&nbsp;
576 12&nbsp;nr=0 # We want the while loop to be able to manipulate these and
577 13&nbsp;totalSize=0 #+ to be able to see the changes after the 'while' finished.
578 14&nbsp;
579 15&nbsp;while read fileSize fileName ; do
580 16&nbsp; echo "$fileName is $fileSize bytes"
581 17&nbsp; let nr++
582 18&nbsp; totalSize=$((totalSize+fileSize)) # Or: "let totalSize+=fileSize"
583 19&nbsp;done&#60;&#60;EOF
584 20&nbsp;$(doesOutput)
585 21&nbsp;EOF
586 22&nbsp;
587 23&nbsp;echo "$nr files totaling $totalSize bytes"</PRE
588 ></TD
589 ></TR
590 ></TABLE
591 >
592 </P
593 ></DIV
594 ><DIV
595 CLASS="NAVFOOTER"
596 ><HR
597 ALIGN="LEFT"
598 WIDTH="100%"><TABLE
599 SUMMARY="Footer navigation table"
600 WIDTH="100%"
601 BORDER="0"
602 CELLPADDING="0"
603 CELLSPACING="0"
604 ><TR
605 ><TD
606 WIDTH="33%"
607 ALIGN="left"
608 VALIGN="top"
609 ><A
610 HREF="io-redirection.html"
611 ACCESSKEY="P"
612 >Prev</A
613 ></TD
614 ><TD
615 WIDTH="34%"
616 ALIGN="center"
617 VALIGN="top"
618 ><A
619 HREF="index.html"
620 ACCESSKEY="H"
621 >Home</A
622 ></TD
623 ><TD
624 WIDTH="33%"
625 ALIGN="right"
626 VALIGN="top"
627 ><A
628 HREF="redirapps.html"
629 ACCESSKEY="N"
630 >Next</A
631 ></TD
632 ></TR
633 ><TR
634 ><TD
635 WIDTH="33%"
636 ALIGN="left"
637 VALIGN="top"
638 >I/O Redirection</TD
639 ><TD
640 WIDTH="34%"
641 ALIGN="center"
642 VALIGN="top"
643 ><A
644 HREF="io-redirection.html"
645 ACCESSKEY="U"
646 >Up</A
647 ></TD
648 ><TD
649 WIDTH="33%"
650 ALIGN="right"
651 VALIGN="top"
652 >Applications</TD
653 ></TR
654 ></TABLE
655 ></DIV
656 ></BODY
657 ></HTML
658 >