Debian Codegraph @

HTML/io-redirection.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >I/O Redirection</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="Here Documents"
18 HREF="here-docs.html"><LINK
19 REL="NEXT"
20 TITLE="Redirecting Code Blocks"
21 HREF="redircb.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="here-docs.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="redircb.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="IO-REDIRECTION"
95 ></A
96 >Chapter 20. I/O Redirection</H1
97 ><P
98 ><A
99 NAME="IOREDIRREF"
100 ></A
101 ></P
102 ><P
103 >There are always three default <I
104 CLASS="FIRSTTERM"
105 >files</I
106 >
107 <A
108 NAME="AEN17884"
109 HREF="#FTN.AEN17884"
110 >[1]</A
111 >
112 open, <TT
113 CLASS="FILENAME"
114 >stdin</TT
115 > (the keyboard),
116 <TT
117 CLASS="FILENAME"
118 >stdout</TT
119 > (the screen), and
120 <TT
121 CLASS="FILENAME"
122 >stderr</TT
123 > (error messages output to the
124 screen). These, and any other open files, can be redirected.
125 Redirection simply means capturing output from a file, command,
126 program, script, or even code block within a script (see <A
127 HREF="special-chars.html#EX8"
128 >Example 3-1</A
129 > and <A
130 HREF="special-chars.html#RPMCHECK"
131 >Example 3-2</A
132 >) and sending it as
133 input to another file, command, program, or script.</P
134 ><P
135 ><A
136 NAME="FDREF"
137 ></A
138 >Each open file gets assigned a file descriptor.
139
140 <A
141 NAME="AEN17894"
142 HREF="#FTN.AEN17894"
143 >[2]</A
144 >
145
146 The file descriptors for <TT
147 CLASS="FILENAME"
148 >stdin</TT
149 >,
150 <TT
151 CLASS="FILENAME"
152 >stdout</TT
153 >, and <TT
154 CLASS="FILENAME"
155 >stderr</TT
156 > are
157 0, 1, and 2, respectively. For opening additional files, there
158 remain descriptors 3 to 9. It is sometimes useful to assign one of
159 these additional file descriptors to <TT
160 CLASS="FILENAME"
161 >stdin</TT
162 >,
163 <TT
164 CLASS="FILENAME"
165 >stdout</TT
166 >, or <TT
167 CLASS="FILENAME"
168 >stderr</TT
169 >
170 as a temporary duplicate link.
171 <A
172 NAME="AEN17906"
173 HREF="#FTN.AEN17906"
174 >[3]</A
175 >
176 This simplifies restoration to normal after complex redirection
177 and reshuffling (see <A
178 HREF="io-redirection.html#REDIR1"
179 >Example 20-1</A
180 >).</P
181 ><P
182 ><A
183 NAME="IOREDIRECTIONREF"
184 ></A
185 ></P
186 ><TABLE
187 BORDER="0"
188 BGCOLOR="#E0E0E0"
189 WIDTH="100%"
190 ><TR
191 ><TD
192 ><PRE
193 CLASS="PROGRAMLISTING"
194 > 1&nbsp; COMMAND_OUTPUT &#62;
195 2&nbsp; # Redirect stdout to a file.
196 3&nbsp; # Creates the file if not present, otherwise overwrites it.
197 4&nbsp;
198 5&nbsp; ls -lR &#62; dir-tree.list
199 6&nbsp; # Creates a file containing a listing of the directory tree.
200 7&nbsp;
201 8&nbsp; : &#62; filename
202 9&nbsp; # The &#62; truncates file "filename" to zero length.
203 10&nbsp; # If file not present, creates zero-length file (same effect as 'touch').
204 11&nbsp; # The : serves as a dummy placeholder, producing no output.
205 12&nbsp;
206 13&nbsp; &#62; filename
207 14&nbsp; # The &#62; truncates file "filename" to zero length.
208 15&nbsp; # If file not present, creates zero-length file (same effect as 'touch').
209 16&nbsp; # (Same result as ": &#62;", above, but this does not work with some shells.)
210 17&nbsp;
211 18&nbsp; COMMAND_OUTPUT &#62;&#62;
212 19&nbsp; # Redirect stdout to a file.
213 20&nbsp; # Creates the file if not present, otherwise appends to it.
214 21&nbsp;
215 22&nbsp;
216 23&nbsp; # Single-line redirection commands (affect only the line they are on):
217 24&nbsp; # --------------------------------------------------------------------
218 25&nbsp;
219 26&nbsp; 1&#62;filename
220 27&nbsp; # Redirect stdout to file "filename."
221 28&nbsp; 1&#62;&#62;filename
222 29&nbsp; # Redirect and append stdout to file "filename."
223 30&nbsp; 2&#62;filename
224 31&nbsp; # Redirect stderr to file "filename."
225 32&nbsp; 2&#62;&#62;filename
226 33&nbsp; # Redirect and append stderr to file "filename."
227 34&nbsp; &#38;&#62;filename
228 35&nbsp; # Redirect both stdout and stderr to file "filename."
229 36&nbsp; # This operator is now functional, as of Bash 4, final release.
230 37&nbsp;
231 38&nbsp; M&#62;N
232 39&nbsp; # "M" is a file descriptor, which defaults to 1, if not explicitly set.
233 40&nbsp; # "N" is a filename.
234 41&nbsp; # File descriptor "M" is redirect to file "N."
235 42&nbsp; M&#62;&#38;N
236 43&nbsp; # "M" is a file descriptor, which defaults to 1, if not set.
237 44&nbsp; # "N" is another file descriptor.
238 45&nbsp;
239 46&nbsp; #==============================================================================
240 47&nbsp;
241 48&nbsp; # Redirecting stdout, one line at a time.
242 49&nbsp; LOGFILE=script.log
243 50&nbsp;
244 51&nbsp; echo "This statement is sent to the log file, \"$LOGFILE\"." 1&#62;$LOGFILE
245 52&nbsp; echo "This statement is appended to \"$LOGFILE\"." 1&#62;&#62;$LOGFILE
246 53&nbsp; echo "This statement is also appended to \"$LOGFILE\"." 1&#62;&#62;$LOGFILE
247 54&nbsp; echo "This statement is echoed to stdout, and will not appear in \"$LOGFILE\"."
248 55&nbsp; # These redirection commands automatically "reset" after each line.
249 56&nbsp;
250 57&nbsp;
251 58&nbsp;
252 59&nbsp; # Redirecting stderr, one line at a time.
253 60&nbsp; ERRORFILE=script.errors
254 61&nbsp;
255 62&nbsp; bad_command1 2&#62;$ERRORFILE # Error message sent to $ERRORFILE.
256 63&nbsp; bad_command2 2&#62;&#62;$ERRORFILE # Error message appended to $ERRORFILE.
257 64&nbsp; bad_command3 # Error message echoed to stderr,
258 65&nbsp; #+ and does not appear in $ERRORFILE.
259 66&nbsp; # These redirection commands also automatically "reset" after each line.
260 67&nbsp; #=======================================================================</PRE
261 ></TD
262 ></TR
263 ></TABLE
264 ><P
265 ><A
266 NAME="IOREDIRECTIONREF1"
267 ></A
268 ></P
269 ><TABLE
270 BORDER="0"
271 BGCOLOR="#E0E0E0"
272 WIDTH="100%"
273 ><TR
274 ><TD
275 ><PRE
276 CLASS="PROGRAMLISTING"
277 > 1&nbsp; 2&#62;&#38;1
278 2&nbsp; # Redirects stderr to stdout.
279 3&nbsp; # Error messages get sent to same place as standard output.
280 4&nbsp; &#62;&#62;filename 2&#62;&#38;1
281 5&nbsp; bad_command &#62;&#62;filename 2&#62;&#38;1
282 6&nbsp; # Appends both stdout and stderr to the file "filename" ...
283 7&nbsp; 2&#62;&#38;1 | [command(s)]
284 8&nbsp; bad_command 2&#62;&#38;1 | awk '{print $5}' # found
285 9&nbsp; # Sends stderr through a pipe.
286 10&nbsp; # |&#38; was added to Bash 4 as an abbreviation for 2&#62;&#38;1 |.
287 11&nbsp;
288 12&nbsp; i&#62;&#38;j
289 13&nbsp; # Redirects file descriptor <SPAN
290 CLASS="emphasis"
291 ><I
292 CLASS="EMPHASIS"
293 >i</I
294 ></SPAN
295 > to <SPAN
296 CLASS="emphasis"
297 ><I
298 CLASS="EMPHASIS"
299 >j</I
300 ></SPAN
301 >.
302 14&nbsp; # All output of file pointed to by <SPAN
303 CLASS="emphasis"
304 ><I
305 CLASS="EMPHASIS"
306 >i</I
307 ></SPAN
308 > gets sent to file pointed to by <SPAN
309 CLASS="emphasis"
310 ><I
311 CLASS="EMPHASIS"
312 >j</I
313 ></SPAN
314 >.
315 15&nbsp;
316 16&nbsp; &#62;&#38;j
317 17&nbsp; # Redirects, by default, file descriptor <SPAN
318 CLASS="emphasis"
319 ><I
320 CLASS="EMPHASIS"
321 >1</I
322 ></SPAN
323 > (stdout) to <SPAN
324 CLASS="emphasis"
325 ><I
326 CLASS="EMPHASIS"
327 >j</I
328 ></SPAN
329 >.
330 18&nbsp; # All stdout gets sent to file pointed to by <SPAN
331 CLASS="emphasis"
332 ><I
333 CLASS="EMPHASIS"
334 >j</I
335 ></SPAN
336 >.</PRE
337 ></TD
338 ></TR
339 ></TABLE
340 ><P
341 ><A
342 NAME="IOREDIRECTIONREF2"
343 ></A
344 ></P
345 ><TABLE
346 BORDER="0"
347 BGCOLOR="#E0E0E0"
348 WIDTH="100%"
349 ><TR
350 ><TD
351 ><PRE
352 CLASS="PROGRAMLISTING"
353 > 1&nbsp; 0&#60; FILENAME
354 2&nbsp; &#60; FILENAME
355 3&nbsp; # Accept input from a file.
356 4&nbsp; # Companion command to <SPAN
357 CLASS="QUOTE"
358 >"&#62;"</SPAN
359 >, and often used in combination with it.
360 5&nbsp; #
361 6&nbsp; # grep search-word &#60;filename
362 7&nbsp;
363 8&nbsp;
364 9&nbsp; [j]&#60;&#62;filename
365 10&nbsp; # Open file "filename" for reading and writing,
366 11&nbsp; #+ and assign file descriptor "j" to it.
367 12&nbsp; # If "filename" does not exist, create it.
368 13&nbsp; # If file descriptor "j" is not specified, default to fd 0, stdin.
369 14&nbsp; #
370 15&nbsp; # An application of this is writing at a specified place in a file.
371 16&nbsp; echo 1234567890 &#62; File # Write string to "File".
372 17&nbsp; exec 3&#60;&#62; File # Open "File" and assign fd 3 to it.
373 18&nbsp; read -n 4 &#60;&#38;3 # Read only 4 characters.
374 19&nbsp; echo -n . &#62;&#38;3 # Write a decimal point there.
375 20&nbsp; exec 3&#62;&#38;- # Close fd 3.
376 21&nbsp; cat File # ==&#62; 1234.67890
377 22&nbsp; # Random access, by golly.
378 23&nbsp;
379 24&nbsp;
380 25&nbsp;
381 26&nbsp; |
382 27&nbsp; # Pipe.
383 28&nbsp; # General purpose process and command chaining tool.
384 29&nbsp; # Similar to <SPAN
385 CLASS="QUOTE"
386 >"&#62;"</SPAN
387 >, but more general in effect.
388 30&nbsp; # Useful for chaining commands, scripts, files, and programs together.
389 31&nbsp; cat *.txt | sort | uniq &#62; result-file
390 32&nbsp; # Sorts the output of all the .txt files and deletes duplicate lines,
391 33&nbsp; # finally saves results to <SPAN
392 CLASS="QUOTE"
393 >"result-file"</SPAN
394 >.</PRE
395 ></TD
396 ></TR
397 ></TABLE
398 ><P
399 >Multiple instances of input and output redirection
400 and/or pipes can be combined in a single command
401 line.
402
403 <TABLE
404 BORDER="0"
405 BGCOLOR="#E0E0E0"
406 WIDTH="100%"
407 ><TR
408 ><TD
409 ><PRE
410 CLASS="PROGRAMLISTING"
411 > 1&nbsp;command &#60; input-file &#62; output-file
412 2&nbsp;# Or the equivalent:
413 3&nbsp;&#60; input-file command &#62; output-file # Although this is non-standard.
414 4&nbsp;
415 5&nbsp;command1 | command2 | command3 &#62; output-file</PRE
416 ></TD
417 ></TR
418 ></TABLE
419 >
420 See <A
421 HREF="filearchiv.html#DERPM"
422 >Example 16-31</A
423 > and <A
424 HREF="contributed-scripts.html#FIFO"
425 >Example A-14</A
426 >.</P
427 ><P
428 >Multiple output streams may be redirected to one file.
429
430 <TABLE
431 BORDER="0"
432 BGCOLOR="#E0E0E0"
433 WIDTH="100%"
434 ><TR
435 ><TD
436 ><PRE
437 CLASS="PROGRAMLISTING"
438 > 1&nbsp;ls -yz &#62;&#62; command.log 2&#62;&#38;1
439 2&nbsp;# Capture result of illegal options "yz" in file "command.log."
440 3&nbsp;# Because stderr is redirected to the file,
441 4&nbsp;#+ any error messages will also be there.
442 5&nbsp;
443 6&nbsp;# Note, however, that the following does *not* give the same result.
444 7&nbsp;ls -yz 2&#62;&#38;1 &#62;&#62; command.log
445 8&nbsp;# Outputs an error message, but does not write to file.
446 9&nbsp;# More precisely, the command output (in this case, null)
447 10&nbsp;#+ writes to the file, but the error message goes only to stdout.
448 11&nbsp;
449 12&nbsp;# If redirecting both stdout and stderr,
450 13&nbsp;#+ the order of the commands makes a difference.</PRE
451 ></TD
452 ></TR
453 ></TABLE
454 ></P
455 ><DIV
456 CLASS="VARIABLELIST"
457 ><P
458 ><B
459 ><A
460 NAME="CFD"
461 ></A
462 >Closing File Descriptors</B
463 ></P
464 ><DL
465 ><DT
466 ><SPAN
467 CLASS="TOKEN"
468 >n&#60;&#38;-</SPAN
469 ></DT
470 ><DD
471 ><P
472 >Close input file descriptor
473 <TT
474 CLASS="REPLACEABLE"
475 ><I
476 >n</I
477 ></TT
478 >.</P
479 ></DD
480 ><DT
481 ><SPAN
482 CLASS="TOKEN"
483 >0&#60;&#38;-</SPAN
484 >, <SPAN
485 CLASS="TOKEN"
486 >&#60;&#38;-</SPAN
487 ></DT
488 ><DD
489 ><P
490 >Close <TT
491 CLASS="FILENAME"
492 >stdin</TT
493 >.</P
494 ></DD
495 ><DT
496 ><SPAN
497 CLASS="TOKEN"
498 >n&#62;&#38;-</SPAN
499 ></DT
500 ><DD
501 ><P
502 >Close output file descriptor <TT
503 CLASS="REPLACEABLE"
504 ><I
505 >n</I
506 ></TT
507 >.</P
508 ></DD
509 ><DT
510 ><SPAN
511 CLASS="TOKEN"
512 >1&#62;&#38;-</SPAN
513 >, <SPAN
514 CLASS="TOKEN"
515 >&#62;&#38;-</SPAN
516 ></DT
517 ><DD
518 ><P
519 >Close <TT
520 CLASS="FILENAME"
521 >stdout</TT
522 >.</P
523 ></DD
524 ></DL
525 ></DIV
526 ><P
527 ><A
528 NAME="FDREF2"
529 ></A
530 ></P
531 ><P
532 >Child processes inherit open file descriptors. This is
533 why pipes work. To prevent an fd from being inherited, close it.
534 <TABLE
535 BORDER="0"
536 BGCOLOR="#E0E0E0"
537 WIDTH="100%"
538 ><TR
539 ><TD
540 ><PRE
541 CLASS="PROGRAMLISTING"
542 > 1&nbsp;# Redirecting only stderr to a pipe.
543 2&nbsp;
544 3&nbsp;exec 3&#62;&#38;1 # Save current "value" of stdout.
545 4&nbsp;ls -l 2&#62;&#38;1 &#62;&#38;3 3&#62;&#38;- | grep bad 3&#62;&#38;- # Close fd 3 for 'grep' (but not 'ls').
546 5&nbsp;# ^^^^ ^^^^
547 6&nbsp;exec 3&#62;&#38;- # Now close it for the remainder of the script.
548 7&nbsp;
549 8&nbsp;# Thanks, S.C.</PRE
550 ></TD
551 ></TR
552 ></TABLE
553 >
554 </P
555 ><P
556 >For a more detailed introduction to I/O redirection see
557 <A
558 HREF="ioredirintro.html"
559 >Appendix F</A
560 >.</P
561 ><DIV
562 CLASS="SECT1"
563 ><H1
564 CLASS="SECT1"
565 ><A
566 NAME="AEN17974"
567 ></A
568 >20.1. Using <I
569 CLASS="FIRSTTERM"
570 >exec</I
571 ></H1
572 ><P
573 ><A
574 NAME="USINGEXECREF"
575 ></A
576 ></P
577 ><P
578 >An <B
579 CLASS="COMMAND"
580 >exec &#60;filename</B
581 > command redirects
582 <TT
583 CLASS="FILENAME"
584 >stdin</TT
585 > to a file. From that point on, all
586 <TT
587 CLASS="FILENAME"
588 >stdin</TT
589 > comes from that file, rather than
590 its normal source (usually keyboard input). This provides a
591 method of reading a file line by line and possibly parsing
592 each line of input using <A
593 HREF="sedawk.html#SEDREF"
594 >sed</A
595 >
596 and/or <A
597 HREF="awk.html#AWKREF"
598 >awk</A
599 >.</P
600 ><DIV
601 CLASS="EXAMPLE"
602 ><HR><A
603 NAME="REDIR1"
604 ></A
605 ><P
606 ><B
607 >Example 20-1. Redirecting <TT
608 CLASS="FILENAME"
609 >stdin</TT
610 > using
611 <I
612 CLASS="FIRSTTERM"
613 >exec</I
614 ></B
615 ></P
616 ><TABLE
617 BORDER="0"
618 BGCOLOR="#E0E0E0"
619 WIDTH="100%"
620 ><TR
621 ><TD
622 ><PRE
623 CLASS="PROGRAMLISTING"
624 > 1&nbsp;#!/bin/bash
625 2&nbsp;# Redirecting stdin using 'exec'.
626 3&nbsp;
627 4&nbsp;
628 5&nbsp;exec 6&#60;&#38;0 # Link file descriptor #6 with stdin.
629 6&nbsp; # Saves stdin.
630 7&nbsp;
631 8&nbsp;exec &#60; data-file # stdin replaced by file "data-file"
632 9&nbsp;
633 10&nbsp;read a1 # Reads first line of file "data-file".
634 11&nbsp;read a2 # Reads second line of file "data-file."
635 12&nbsp;
636 13&nbsp;echo
637 14&nbsp;echo "Following lines read from file."
638 15&nbsp;echo "-------------------------------"
639 16&nbsp;echo $a1
640 17&nbsp;echo $a2
641 18&nbsp;
642 19&nbsp;echo; echo; echo
643 20&nbsp;
644 21&nbsp;exec 0&#60;&#38;6 6&#60;&#38;-
645 22&nbsp;# Now restore stdin from fd #6, where it had been saved,
646 23&nbsp;#+ and close fd #6 ( 6&#60;&#38;- ) to free it for other processes to use.
647 24&nbsp;#
648 25&nbsp;# &#60;&#38;6 6&#60;&#38;- also works.
649 26&nbsp;
650 27&nbsp;echo -n "Enter data "
651 28&nbsp;read b1 # Now "read" functions as expected, reading from normal stdin.
652 29&nbsp;echo "Input read from stdin."
653 30&nbsp;echo "----------------------"
654 31&nbsp;echo "b1 = $b1"
655 32&nbsp;
656 33&nbsp;echo
657 34&nbsp;
658 35&nbsp;exit 0</PRE
659 ></TD
660 ></TR
661 ></TABLE
662 ><HR></DIV
663 ><P
664 >Similarly, an <B
665 CLASS="COMMAND"
666 >exec &#62;filename</B
667 >
668 command redirects <TT
669 CLASS="FILENAME"
670 >stdout</TT
671 > to a designated
672 file. This sends all command output that would normally go
673 to <TT
674 CLASS="FILENAME"
675 >stdout</TT
676 > to that file.</P
677 ><DIV
678 CLASS="IMPORTANT"
679 ><TABLE
680 CLASS="IMPORTANT"
681 WIDTH="100%"
682 BORDER="0"
683 ><TR
684 ><TD
685 WIDTH="25"
686 ALIGN="CENTER"
687 VALIGN="TOP"
688 ><IMG
689 SRC="common/important.png"
690 HSPACE="5"
691 ALT="Important"></TD
692 ><TD
693 ALIGN="LEFT"
694 VALIGN="TOP"
695 ><P
696 > <B
697 CLASS="COMMAND"
698 >exec N &#62; filename</B
699 > affects the entire
700 script or <I
701 CLASS="FIRSTTERM"
702 >current shell</I
703 >. Redirection in
704 the <A
705 HREF="special-chars.html#PROCESSIDREF"
706 >PID</A
707 > of the script or shell
708 from that point on has changed. However . . .
709 </P
710 ><P
711 > <B
712 CLASS="COMMAND"
713 >N &#62; filename</B
714 > affects only the newly-forked process,
715 not the entire script or shell.
716 </P
717 ><P
718 >Thank you, Ahmed Darwish, for pointing this out.</P
719 ></TD
720 ></TR
721 ></TABLE
722 ></DIV
723 ><DIV
724 CLASS="EXAMPLE"
725 ><HR><A
726 NAME="REASSIGNSTDOUT"
727 ></A
728 ><P
729 ><B
730 >Example 20-2. Redirecting <TT
731 CLASS="FILENAME"
732 >stdout</TT
733 > using
734 <I
735 CLASS="FIRSTTERM"
736 >exec</I
737 ></B
738 ></P
739 ><TABLE
740 BORDER="0"
741 BGCOLOR="#E0E0E0"
742 WIDTH="100%"
743 ><TR
744 ><TD
745 ><PRE
746 CLASS="PROGRAMLISTING"
747 > 1&nbsp;#!/bin/bash
748 2&nbsp;# reassign-stdout.sh
749 3&nbsp;
750 4&nbsp;LOGFILE=logfile.txt
751 5&nbsp;
752 6&nbsp;exec 6&#62;&#38;1 # Link file descriptor #6 with stdout.
753 7&nbsp; # Saves stdout.
754 8&nbsp;
755 9&nbsp;exec &#62; $LOGFILE # stdout replaced with file "logfile.txt".
756 10&nbsp;
757 11&nbsp;# ----------------------------------------------------------- #
758 12&nbsp;# All output from commands in this block sent to file $LOGFILE.
759 13&nbsp;
760 14&nbsp;echo -n "Logfile: "
761 15&nbsp;date
762 16&nbsp;echo "-------------------------------------"
763 17&nbsp;echo
764 18&nbsp;
765 19&nbsp;echo "Output of \"ls -al\" command"
766 20&nbsp;echo
767 21&nbsp;ls -al
768 22&nbsp;echo; echo
769 23&nbsp;echo "Output of \"df\" command"
770 24&nbsp;echo
771 25&nbsp;df
772 26&nbsp;
773 27&nbsp;# ----------------------------------------------------------- #
774 28&nbsp;
775 29&nbsp;exec 1&#62;&#38;6 6&#62;&#38;- # Restore stdout and close file descriptor #6.
776 30&nbsp;
777 31&nbsp;echo
778 32&nbsp;echo "== stdout now restored to default == "
779 33&nbsp;echo
780 34&nbsp;ls -al
781 35&nbsp;echo
782 36&nbsp;
783 37&nbsp;exit 0</PRE
784 ></TD
785 ></TR
786 ></TABLE
787 ><HR></DIV
788 ><DIV
789 CLASS="EXAMPLE"
790 ><HR><A
791 NAME="UPPERCONV"
792 ></A
793 ><P
794 ><B
795 >Example 20-3. Redirecting both <TT
796 CLASS="FILENAME"
797 >stdin</TT
798 > and
799 <TT
800 CLASS="FILENAME"
801 >stdout</TT
802 > in the same script with
803 <I
804 CLASS="FIRSTTERM"
805 >exec</I
806 ></B
807 ></P
808 ><TABLE
809 BORDER="0"
810 BGCOLOR="#E0E0E0"
811 WIDTH="100%"
812 ><TR
813 ><TD
814 ><PRE
815 CLASS="PROGRAMLISTING"
816 > 1&nbsp;#!/bin/bash
817 2&nbsp;# upperconv.sh
818 3&nbsp;# Converts a specified input file to uppercase.
819 4&nbsp;
820 5&nbsp;E_FILE_ACCESS=70
821 6&nbsp;E_WRONG_ARGS=71
822 7&nbsp;
823 8&nbsp;if [ ! -r "$1" ] # Is specified input file readable?
824 9&nbsp;then
825 10&nbsp; echo "Can't read from input file!"
826 11&nbsp; echo "Usage: $0 input-file output-file"
827 12&nbsp; exit $E_FILE_ACCESS
828 13&nbsp;fi # Will exit with same error
829 14&nbsp; #+ even if input file ($1) not specified (why?).
830 15&nbsp;
831 16&nbsp;if [ -z "$2" ]
832 17&nbsp;then
833 18&nbsp; echo "Need to specify output file."
834 19&nbsp; echo "Usage: $0 input-file output-file"
835 20&nbsp; exit $E_WRONG_ARGS
836 21&nbsp;fi
837 22&nbsp;
838 23&nbsp;
839 24&nbsp;exec 4&#60;&#38;0
840 25&nbsp;exec &#60; $1 # Will read from input file.
841 26&nbsp;
842 27&nbsp;exec 7&#62;&#38;1
843 28&nbsp;exec &#62; $2 # Will write to output file.
844 29&nbsp; # Assumes output file writable (add check?).
845 30&nbsp;
846 31&nbsp;# -----------------------------------------------
847 32&nbsp; cat - | tr a-z A-Z # Uppercase conversion.
848 33&nbsp;# ^^^^^ # Reads from stdin.
849 34&nbsp;# ^^^^^^^^^^ # Writes to stdout.
850 35&nbsp;# However, both stdin and stdout were redirected.
851 36&nbsp;# Note that the 'cat' can be omitted.
852 37&nbsp;# -----------------------------------------------
853 38&nbsp;
854 39&nbsp;exec 1&#62;&#38;7 7&#62;&#38;- # Restore stout.
855 40&nbsp;exec 0&#60;&#38;4 4&#60;&#38;- # Restore stdin.
856 41&nbsp;
857 42&nbsp;# After restoration, the following line prints to stdout as expected.
858 43&nbsp;echo "File \"$1\" written to \"$2\" as uppercase conversion."
859 44&nbsp;
860 45&nbsp;exit 0</PRE
861 ></TD
862 ></TR
863 ></TABLE
864 ><HR></DIV
865 ><P
866 >I/O redirection is a clever way of avoiding the dreaded <A
867 HREF="subshells.html#PARVIS"
868 >inaccessible variables within a subshell</A
869 >
870 problem.</P
871 ><DIV
872 CLASS="EXAMPLE"
873 ><HR><A
874 NAME="AVOIDSUBSHELL"
875 ></A
876 ><P
877 ><B
878 >Example 20-4. Avoiding a subshell</B
879 ></P
880 ><TABLE
881 BORDER="0"
882 BGCOLOR="#E0E0E0"
883 WIDTH="100%"
884 ><TR
885 ><TD
886 ><PRE
887 CLASS="PROGRAMLISTING"
888 > 1&nbsp;#!/bin/bash
889 2&nbsp;# avoid-subshell.sh
890 3&nbsp;# Suggested by Matthew Walker.
891 4&nbsp;
892 5&nbsp;Lines=0
893 6&nbsp;
894 7&nbsp;echo
895 8&nbsp;
896 9&nbsp;cat myfile.txt | while read line;
897 10&nbsp; do {
898 11&nbsp; echo $line
899 12&nbsp; (( Lines++ )); # Incremented values of this variable
900 13&nbsp; #+ inaccessible outside loop.
901 14&nbsp; # Subshell problem.
902 15&nbsp; }
903 16&nbsp; done
904 17&nbsp;
905 18&nbsp;echo "Number of lines read = $Lines" # 0
906 19&nbsp; # Wrong!
907 20&nbsp;
908 21&nbsp;echo "------------------------"
909 22&nbsp;
910 23&nbsp;
911 24&nbsp;exec 3&#60;&#62; myfile.txt
912 25&nbsp;while read line &#60;&#38;3
913 26&nbsp;do {
914 27&nbsp; echo "$line"
915 28&nbsp; (( Lines++ )); # Incremented values of this variable
916 29&nbsp; #+ accessible outside loop.
917 30&nbsp; # No subshell, no problem.
918 31&nbsp;}
919 32&nbsp;done
920 33&nbsp;exec 3&#62;&#38;-
921 34&nbsp;
922 35&nbsp;echo "Number of lines read = $Lines" # 8
923 36&nbsp;
924 37&nbsp;echo
925 38&nbsp;
926 39&nbsp;exit 0
927 40&nbsp;
928 41&nbsp;# Lines below not seen by script.
929 42&nbsp;
930 43&nbsp;$ cat myfile.txt
931 44&nbsp;
932 45&nbsp;Line 1.
933 46&nbsp;Line 2.
934 47&nbsp;Line 3.
935 48&nbsp;Line 4.
936 49&nbsp;Line 5.
937 50&nbsp;Line 6.
938 51&nbsp;Line 7.
939 52&nbsp;Line 8.</PRE
940 ></TD
941 ></TR
942 ></TABLE
943 ><HR></DIV
944 ></DIV
945 ></DIV
946 ><H3
947 CLASS="FOOTNOTES"
948 >Notes</H3
949 ><TABLE
950 BORDER="0"
951 CLASS="FOOTNOTES"
952 WIDTH="100%"
953 ><TR
954 ><TD
955 ALIGN="LEFT"
956 VALIGN="TOP"
957 WIDTH="5%"
958 ><A
959 NAME="FTN.AEN17884"
960 HREF="io-redirection.html#AEN17884"
961 >[1]</A
962 ></TD
963 ><TD
964 ALIGN="LEFT"
965 VALIGN="TOP"
966 WIDTH="95%"
967 ><P
968 >By convention in UNIX and Linux, data streams
969 and peripherals (<A
970 HREF="devproc.html#DEVFILEREF"
971 >device files</A
972 >)
973 are treated as files, in a fashion analogous to ordinary
974 files.</P
975 ></TD
976 ></TR
977 ><TR
978 ><TD
979 ALIGN="LEFT"
980 VALIGN="TOP"
981 WIDTH="5%"
982 ><A
983 NAME="FTN.AEN17894"
984 HREF="io-redirection.html#AEN17894"
985 >[2]</A
986 ></TD
987 ><TD
988 ALIGN="LEFT"
989 VALIGN="TOP"
990 WIDTH="95%"
991 ><P
992 ><A
993 NAME="FDREF1"
994 ></A
995 >A <I
996 CLASS="FIRSTTERM"
997 >file
998 descriptor</I
999 > is simply a number that
1000 the operating system assigns to an open file
1001 to keep track of it. Consider it a simplified
1002 type of file pointer. It is analogous
1003 to a <I
1004 CLASS="FIRSTTERM"
1005 >file handle</I
1006 > in
1007 <B
1008 CLASS="COMMAND"
1009 >C</B
1010 >.</P
1011 ></TD
1012 ></TR
1013 ><TR
1014 ><TD
1015 ALIGN="LEFT"
1016 VALIGN="TOP"
1017 WIDTH="5%"
1018 ><A
1019 NAME="FTN.AEN17906"
1020 HREF="io-redirection.html#AEN17906"
1021 >[3]</A
1022 ></TD
1023 ><TD
1024 ALIGN="LEFT"
1025 VALIGN="TOP"
1026 WIDTH="95%"
1027 ><P
1028 >Using <TT
1029 CLASS="REPLACEABLE"
1030 ><I
1031 >file
1032 descriptor 5</I
1033 ></TT
1034 > might cause problems.
1035 When Bash creates a child process, as with <A
1036 HREF="internal.html#EXECREF"
1037 >exec</A
1038 >, the child inherits
1039 fd 5 (see Chet Ramey's archived e-mail, <A
1040 HREF="http://groups.google.com/group/gnu.bash.bug/browse_thread/thread/13955daafded3b5c/18c17050087f9f37"
1041 TARGET="_top"
1042 > SUBJECT: RE: File descriptor 5 is held open</A
1043 >).
1044 Best leave this particular fd alone.</P
1045 ></TD
1046 ></TR
1047 ></TABLE
1048 ><DIV
1049 CLASS="NAVFOOTER"
1050 ><HR
1051 ALIGN="LEFT"
1052 WIDTH="100%"><TABLE
1053 SUMMARY="Footer navigation table"
1054 WIDTH="100%"
1055 BORDER="0"
1056 CELLPADDING="0"
1057 CELLSPACING="0"
1058 ><TR
1059 ><TD
1060 WIDTH="33%"
1061 ALIGN="left"
1062 VALIGN="top"
1063 ><A
1064 HREF="here-docs.html"
1065 ACCESSKEY="P"
1066 >Prev</A
1067 ></TD
1068 ><TD
1069 WIDTH="34%"
1070 ALIGN="center"
1071 VALIGN="top"
1072 ><A
1073 HREF="index.html"
1074 ACCESSKEY="H"
1075 >Home</A
1076 ></TD
1077 ><TD
1078 WIDTH="33%"
1079 ALIGN="right"
1080 VALIGN="top"
1081 ><A
1082 HREF="redircb.html"
1083 ACCESSKEY="N"
1084 >Next</A
1085 ></TD
1086 ></TR
1087 ><TR
1088 ><TD
1089 WIDTH="33%"
1090 ALIGN="left"
1091 VALIGN="top"
1092 >Here Documents</TD
1093 ><TD
1094 WIDTH="34%"
1095 ALIGN="center"
1096 VALIGN="top"
1097 ><A
1098 HREF="part5.html"
1099 ACCESSKEY="U"
1100 >Up</A
1101 ></TD
1102 ><TD
1103 WIDTH="33%"
1104 ALIGN="right"
1105 VALIGN="top"
1106 >Redirecting Code Blocks</TD
1107 ></TR
1108 ></TABLE
1109 ></DIV
1110 ></BODY
1111 ></HTML
1112 >