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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>I/O Redirection</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="Advanced Topics"
HREF="part5.html"><LINK
REL="PREVIOUS"
TITLE="Here Documents"
HREF="here-docs.html"><LINK
REL="NEXT"
TITLE="Redirecting Code Blocks"
HREF="redircb.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="CHAPTER"
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="here-docs.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="redircb.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="IO-REDIRECTION"
></A
>Chapter 20. I/O Redirection</H1
><P
><A
NAME="IOREDIRREF"
></A
></P
><P
>There are always three default <I
CLASS="FIRSTTERM"
>files</I
>
<A
NAME="AEN17884"
HREF="#FTN.AEN17884"
>[1]</A
>
open, <TT
CLASS="FILENAME"
>stdin</TT
> (the keyboard),
<TT
CLASS="FILENAME"
>stdout</TT
> (the screen), and
<TT
CLASS="FILENAME"
>stderr</TT
> (error messages output to the
screen). These, and any other open files, can be redirected.
Redirection simply means capturing output from a file, command,
program, script, or even code block within a script (see <A
HREF="special-chars.html#EX8"
>Example 3-1</A
> and <A
HREF="special-chars.html#RPMCHECK"
>Example 3-2</A
>) and sending it as
input to another file, command, program, or script.</P
><P
><A
NAME="FDREF"
></A
>Each open file gets assigned a file descriptor.
<A
NAME="AEN17894"
HREF="#FTN.AEN17894"
>[2]</A
>
The file descriptors for <TT
CLASS="FILENAME"
>stdin</TT
>,
<TT
CLASS="FILENAME"
>stdout</TT
>, and <TT
CLASS="FILENAME"
>stderr</TT
> are
0, 1, and 2, respectively. For opening additional files, there
remain descriptors 3 to 9. It is sometimes useful to assign one of
these additional file descriptors to <TT
CLASS="FILENAME"
>stdin</TT
>,
<TT
CLASS="FILENAME"
>stdout</TT
>, or <TT
CLASS="FILENAME"
>stderr</TT
>
as a temporary duplicate link.
<A
NAME="AEN17906"
HREF="#FTN.AEN17906"
>[3]</A
>
This simplifies restoration to normal after complex redirection
and reshuffling (see <A
HREF="io-redirection.html#REDIR1"
>Example 20-1</A
>).</P
><P
><A
NAME="IOREDIRECTIONREF"
></A
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 COMMAND_OUTPUT >
2 # Redirect stdout to a file.
3 # Creates the file if not present, otherwise overwrites it.
4
5 ls -lR > dir-tree.list
6 # Creates a file containing a listing of the directory tree.
7
8 : > filename
9 # The > truncates file "filename" to zero length.
10 # If file not present, creates zero-length file (same effect as 'touch').
11 # The : serves as a dummy placeholder, producing no output.
12
13 > filename
14 # The > truncates file "filename" to zero length.
15 # If file not present, creates zero-length file (same effect as 'touch').
16 # (Same result as ": >", above, but this does not work with some shells.)
17
18 COMMAND_OUTPUT >>
19 # Redirect stdout to a file.
20 # Creates the file if not present, otherwise appends to it.
21
22
23 # Single-line redirection commands (affect only the line they are on):
24 # --------------------------------------------------------------------
25
26 1>filename
27 # Redirect stdout to file "filename."
28 1>>filename
29 # Redirect and append stdout to file "filename."
30 2>filename
31 # Redirect stderr to file "filename."
32 2>>filename
33 # Redirect and append stderr to file "filename."
34 &>filename
35 # Redirect both stdout and stderr to file "filename."
36 # This operator is now functional, as of Bash 4, final release.
37
38 M>N
39 # "M" is a file descriptor, which defaults to 1, if not explicitly set.
40 # "N" is a filename.
41 # File descriptor "M" is redirect to file "N."
42 M>&N
43 # "M" is a file descriptor, which defaults to 1, if not set.
44 # "N" is another file descriptor.
45
46 #==============================================================================
47
48 # Redirecting stdout, one line at a time.
49 LOGFILE=script.log
50
51 echo "This statement is sent to the log file, \"$LOGFILE\"." 1>$LOGFILE
52 echo "This statement is appended to \"$LOGFILE\"." 1>>$LOGFILE
53 echo "This statement is also appended to \"$LOGFILE\"." 1>>$LOGFILE
54 echo "This statement is echoed to stdout, and will not appear in \"$LOGFILE\"."
55 # These redirection commands automatically "reset" after each line.
56
57
58
59 # Redirecting stderr, one line at a time.
60 ERRORFILE=script.errors
61
62 bad_command1 2>$ERRORFILE # Error message sent to $ERRORFILE.
63 bad_command2 2>>$ERRORFILE # Error message appended to $ERRORFILE.
64 bad_command3 # Error message echoed to stderr,
65 #+ and does not appear in $ERRORFILE.
66 # These redirection commands also automatically "reset" after each line.
67 #=======================================================================</PRE
></TD
></TR
></TABLE
><P
><A
NAME="IOREDIRECTIONREF1"
></A
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 2>&1
2 # Redirects stderr to stdout.
3 # Error messages get sent to same place as standard output.
4 >>filename 2>&1
5 bad_command >>filename 2>&1
6 # Appends both stdout and stderr to the file "filename" ...
7 2>&1 | [command(s)]
8 bad_command 2>&1 | awk '{print $5}' # found
9 # Sends stderr through a pipe.
10 # |& was added to Bash 4 as an abbreviation for 2>&1 |.
11
12 i>&j
13 # Redirects file descriptor <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>i</I
></SPAN
> to <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>j</I
></SPAN
>.
14 # All output of file pointed to by <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>i</I
></SPAN
> gets sent to file pointed to by <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>j</I
></SPAN
>.
15
16 >&j
17 # Redirects, by default, file descriptor <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>1</I
></SPAN
> (stdout) to <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>j</I
></SPAN
>.
18 # All stdout gets sent to file pointed to by <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>j</I
></SPAN
>.</PRE
></TD
></TR
></TABLE
><P
><A
NAME="IOREDIRECTIONREF2"
></A
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 0< FILENAME
2 < FILENAME
3 # Accept input from a file.
4 # Companion command to <SPAN
CLASS="QUOTE"
>">"</SPAN
>, and often used in combination with it.
5 #
6 # grep search-word <filename
7
8
9 [j]<>filename
10 # Open file "filename" for reading and writing,
11 #+ and assign file descriptor "j" to it.
12 # If "filename" does not exist, create it.
13 # If file descriptor "j" is not specified, default to fd 0, stdin.
14 #
15 # An application of this is writing at a specified place in a file.
16 echo 1234567890 > File # Write string to "File".
17 exec 3<> File # Open "File" and assign fd 3 to it.
18 read -n 4 <&3 # Read only 4 characters.
19 echo -n . >&3 # Write a decimal point there.
20 exec 3>&- # Close fd 3.
21 cat File # ==> 1234.67890
22 # Random access, by golly.
23
24
25
26 |
27 # Pipe.
28 # General purpose process and command chaining tool.
29 # Similar to <SPAN
CLASS="QUOTE"
>">"</SPAN
>, but more general in effect.
30 # Useful for chaining commands, scripts, files, and programs together.
31 cat *.txt | sort | uniq > result-file
32 # Sorts the output of all the .txt files and deletes duplicate lines,
33 # finally saves results to <SPAN
CLASS="QUOTE"
>"result-file"</SPAN
>.</PRE
></TD
></TR
></TABLE
><P
>Multiple instances of input and output redirection
and/or pipes can be combined in a single command
line.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 command < input-file > output-file
2 # Or the equivalent:
3 < input-file command > output-file # Although this is non-standard.
4
5 command1 | command2 | command3 > output-file</PRE
></TD
></TR
></TABLE
>
See <A
HREF="filearchiv.html#DERPM"
>Example 16-31</A
> and <A
HREF="contributed-scripts.html#FIFO"
>Example A-14</A
>.</P
><P
>Multiple output streams may be redirected to one file.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 ls -yz >> command.log 2>&1
2 # Capture result of illegal options "yz" in file "command.log."
3 # Because stderr is redirected to the file,
4 #+ any error messages will also be there.
5
6 # Note, however, that the following does *not* give the same result.
7 ls -yz 2>&1 >> command.log
8 # Outputs an error message, but does not write to file.
9 # More precisely, the command output (in this case, null)
10 #+ writes to the file, but the error message goes only to stdout.
11
12 # If redirecting both stdout and stderr,
13 #+ the order of the commands makes a difference.</PRE
></TD
></TR
></TABLE
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="CFD"
></A
>Closing File Descriptors</B
></P
><DL
><DT
><SPAN
CLASS="TOKEN"
>n<&-</SPAN
></DT
><DD
><P
>Close input file descriptor
<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>.</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>0<&-</SPAN
>, <SPAN
CLASS="TOKEN"
><&-</SPAN
></DT
><DD
><P
>Close <TT
CLASS="FILENAME"
>stdin</TT
>.</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>n>&-</SPAN
></DT
><DD
><P
>Close output file descriptor <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>.</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>1>&-</SPAN
>, <SPAN
CLASS="TOKEN"
>>&-</SPAN
></DT
><DD
><P
>Close <TT
CLASS="FILENAME"
>stdout</TT
>.</P
></DD
></DL
></DIV
><P
><A
NAME="FDREF2"
></A
></P
><P
>Child processes inherit open file descriptors. This is
why pipes work. To prevent an fd from being inherited, close it.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Redirecting only stderr to a pipe.
2
3 exec 3>&1 # Save current "value" of stdout.
4 ls -l 2>&1 >&3 3>&- | grep bad 3>&- # Close fd 3 for 'grep' (but not 'ls').
5 # ^^^^ ^^^^
6 exec 3>&- # Now close it for the remainder of the script.
7
8 # Thanks, S.C.</PRE
></TD
></TR
></TABLE
>
</P
><P
>For a more detailed introduction to I/O redirection see
<A
HREF="ioredirintro.html"
>Appendix F</A
>.</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN17974"
></A
>20.1. Using <I
CLASS="FIRSTTERM"
>exec</I
></H1
><P
><A
NAME="USINGEXECREF"
></A
></P
><P
>An <B
CLASS="COMMAND"
>exec <filename</B
> command redirects
<TT
CLASS="FILENAME"
>stdin</TT
> to a file. From that point on, all
<TT
CLASS="FILENAME"
>stdin</TT
> comes from that file, rather than
its normal source (usually keyboard input). This provides a
method of reading a file line by line and possibly parsing
each line of input using <A
HREF="sedawk.html#SEDREF"
>sed</A
>
and/or <A
HREF="awk.html#AWKREF"
>awk</A
>.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="REDIR1"
></A
><P
><B
>Example 20-1. Redirecting <TT
CLASS="FILENAME"
>stdin</TT
> using
<I
CLASS="FIRSTTERM"
>exec</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # Redirecting stdin using 'exec'.
3
4
5 exec 6<&0 # Link file descriptor #6 with stdin.
6 # Saves stdin.
7
8 exec < data-file # stdin replaced by file "data-file"
9
10 read a1 # Reads first line of file "data-file".
11 read a2 # Reads second line of file "data-file."
12
13 echo
14 echo "Following lines read from file."
15 echo "-------------------------------"
16 echo $a1
17 echo $a2
18
19 echo; echo; echo
20
21 exec 0<&6 6<&-
22 # Now restore stdin from fd #6, where it had been saved,
23 #+ and close fd #6 ( 6<&- ) to free it for other processes to use.
24 #
25 # <&6 6<&- also works.
26
27 echo -n "Enter data "
28 read b1 # Now "read" functions as expected, reading from normal stdin.
29 echo "Input read from stdin."
30 echo "----------------------"
31 echo "b1 = $b1"
32
33 echo
34
35 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>Similarly, an <B
CLASS="COMMAND"
>exec >filename</B
>
command redirects <TT
CLASS="FILENAME"
>stdout</TT
> to a designated
file. This sends all command output that would normally go
to <TT
CLASS="FILENAME"
>stdout</TT
> to that file.</P
><DIV
CLASS="IMPORTANT"
><TABLE
CLASS="IMPORTANT"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/important.png"
HSPACE="5"
ALT="Important"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
> <B
CLASS="COMMAND"
>exec N > filename</B
> affects the entire
script or <I
CLASS="FIRSTTERM"
>current shell</I
>. Redirection in
the <A
HREF="special-chars.html#PROCESSIDREF"
>PID</A
> of the script or shell
from that point on has changed. However . . .
</P
><P
> <B
CLASS="COMMAND"
>N > filename</B
> affects only the newly-forked process,
not the entire script or shell.
</P
><P
>Thank you, Ahmed Darwish, for pointing this out.</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="REASSIGNSTDOUT"
></A
><P
><B
>Example 20-2. Redirecting <TT
CLASS="FILENAME"
>stdout</TT
> using
<I
CLASS="FIRSTTERM"
>exec</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # reassign-stdout.sh
3
4 LOGFILE=logfile.txt
5
6 exec 6>&1 # Link file descriptor #6 with stdout.
7 # Saves stdout.
8
9 exec > $LOGFILE # stdout replaced with file "logfile.txt".
10
11 # ----------------------------------------------------------- #
12 # All output from commands in this block sent to file $LOGFILE.
13
14 echo -n "Logfile: "
15 date
16 echo "-------------------------------------"
17 echo
18
19 echo "Output of \"ls -al\" command"
20 echo
21 ls -al
22 echo; echo
23 echo "Output of \"df\" command"
24 echo
25 df
26
27 # ----------------------------------------------------------- #
28
29 exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
30
31 echo
32 echo "== stdout now restored to default == "
33 echo
34 ls -al
35 echo
36
37 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="UPPERCONV"
></A
><P
><B
>Example 20-3. Redirecting both <TT
CLASS="FILENAME"
>stdin</TT
> and
<TT
CLASS="FILENAME"
>stdout</TT
> in the same script with
<I
CLASS="FIRSTTERM"
>exec</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # upperconv.sh
3 # Converts a specified input file to uppercase.
4
5 E_FILE_ACCESS=70
6 E_WRONG_ARGS=71
7
8 if [ ! -r "$1" ] # Is specified input file readable?
9 then
10 echo "Can't read from input file!"
11 echo "Usage: $0 input-file output-file"
12 exit $E_FILE_ACCESS
13 fi # Will exit with same error
14 #+ even if input file ($1) not specified (why?).
15
16 if [ -z "$2" ]
17 then
18 echo "Need to specify output file."
19 echo "Usage: $0 input-file output-file"
20 exit $E_WRONG_ARGS
21 fi
22
23
24 exec 4<&0
25 exec < $1 # Will read from input file.
26
27 exec 7>&1
28 exec > $2 # Will write to output file.
29 # Assumes output file writable (add check?).
30
31 # -----------------------------------------------
32 cat - | tr a-z A-Z # Uppercase conversion.
33 # ^^^^^ # Reads from stdin.
34 # ^^^^^^^^^^ # Writes to stdout.
35 # However, both stdin and stdout were redirected.
36 # Note that the 'cat' can be omitted.
37 # -----------------------------------------------
38
39 exec 1>&7 7>&- # Restore stout.
40 exec 0<&4 4<&- # Restore stdin.
41
42 # After restoration, the following line prints to stdout as expected.
43 echo "File \"$1\" written to \"$2\" as uppercase conversion."
44
45 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>I/O redirection is a clever way of avoiding the dreaded <A
HREF="subshells.html#PARVIS"
>inaccessible variables within a subshell</A
>
problem.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="AVOIDSUBSHELL"
></A
><P
><B
>Example 20-4. Avoiding a subshell</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # avoid-subshell.sh
3 # Suggested by Matthew Walker.
4
5 Lines=0
6
7 echo
8
9 cat myfile.txt | while read line;
10 do {
11 echo $line
12 (( Lines++ )); # Incremented values of this variable
13 #+ inaccessible outside loop.
14 # Subshell problem.
15 }
16 done
17
18 echo "Number of lines read = $Lines" # 0
19 # Wrong!
20
21 echo "------------------------"
22
23
24 exec 3<> myfile.txt
25 while read line <&3
26 do {
27 echo "$line"
28 (( Lines++ )); # Incremented values of this variable
29 #+ accessible outside loop.
30 # No subshell, no problem.
31 }
32 done
33 exec 3>&-
34
35 echo "Number of lines read = $Lines" # 8
36
37 echo
38
39 exit 0
40
41 # Lines below not seen by script.
42
43 $ cat myfile.txt
44
45 Line 1.
46 Line 2.
47 Line 3.
48 Line 4.
49 Line 5.
50 Line 6.
51 Line 7.
52 Line 8.</PRE
></TD
></TR
></TABLE
><HR></DIV
></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.AEN17884"
HREF="io-redirection.html#AEN17884"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>By convention in UNIX and Linux, data streams
and peripherals (<A
HREF="devproc.html#DEVFILEREF"
>device files</A
>)
are treated as files, in a fashion analogous to ordinary
files.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN17894"
HREF="io-redirection.html#AEN17894"
>[2]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
><A
NAME="FDREF1"
></A
>A <I
CLASS="FIRSTTERM"
>file
descriptor</I
> is simply a number that
the operating system assigns to an open file
to keep track of it. Consider it a simplified
type of file pointer. It is analogous
to a <I
CLASS="FIRSTTERM"
>file handle</I
> in
<B
CLASS="COMMAND"
>C</B
>.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN17906"
HREF="io-redirection.html#AEN17906"
>[3]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Using <TT
CLASS="REPLACEABLE"
><I
>file
descriptor 5</I
></TT
> might cause problems.
When Bash creates a child process, as with <A
HREF="internal.html#EXECREF"
>exec</A
>, the child inherits
fd 5 (see Chet Ramey's archived e-mail, <A
HREF="http://groups.google.com/group/gnu.bash.bug/browse_thread/thread/13955daafded3b5c/18c17050087f9f37"
TARGET="_top"
> SUBJECT: RE: File descriptor 5 is held open</A
>).
Best leave this particular fd alone.</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="here-docs.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="redircb.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Here Documents</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Redirecting Code Blocks</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|