| 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 COMMAND_OUTPUT > |
| 195 | 2 # Redirect stdout to a file. |
| 196 | 3 # Creates the file if not present, otherwise overwrites it. |
| 197 | 4 |
| 198 | 5 ls -lR > dir-tree.list |
| 199 | 6 # Creates a file containing a listing of the directory tree. |
| 200 | 7 |
| 201 | 8 : > filename |
| 202 | 9 # The > truncates file "filename" to zero length. |
| 203 | 10 # If file not present, creates zero-length file (same effect as 'touch'). |
| 204 | 11 # The : serves as a dummy placeholder, producing no output. |
| 205 | 12 |
| 206 | 13 > filename |
| 207 | 14 # The > truncates file "filename" to zero length. |
| 208 | 15 # If file not present, creates zero-length file (same effect as 'touch'). |
| 209 | 16 # (Same result as ": >", above, but this does not work with some shells.) |
| 210 | 17 |
| 211 | 18 COMMAND_OUTPUT >> |
| 212 | 19 # Redirect stdout to a file. |
| 213 | 20 # Creates the file if not present, otherwise appends to it. |
| 214 | 21 |
| 215 | 22 |
| 216 | 23 # Single-line redirection commands (affect only the line they are on): |
| 217 | 24 # -------------------------------------------------------------------- |
| 218 | 25 |
| 219 | 26 1>filename |
| 220 | 27 # Redirect stdout to file "filename." |
| 221 | 28 1>>filename |
| 222 | 29 # Redirect and append stdout to file "filename." |
| 223 | 30 2>filename |
| 224 | 31 # Redirect stderr to file "filename." |
| 225 | 32 2>>filename |
| 226 | 33 # Redirect and append stderr to file "filename." |
| 227 | 34 &>filename |
| 228 | 35 # Redirect both stdout and stderr to file "filename." |
| 229 | 36 # This operator is now functional, as of Bash 4, final release. |
| 230 | 37 |
| 231 | 38 M>N |
| 232 | 39 # "M" is a file descriptor, which defaults to 1, if not explicitly set. |
| 233 | 40 # "N" is a filename. |
| 234 | 41 # File descriptor "M" is redirect to file "N." |
| 235 | 42 M>&N |
| 236 | 43 # "M" is a file descriptor, which defaults to 1, if not set. |
| 237 | 44 # "N" is another file descriptor. |
| 238 | 45 |
| 239 | 46 #============================================================================== |
| 240 | 47 |
| 241 | 48 # Redirecting stdout, one line at a time. |
| 242 | 49 LOGFILE=script.log |
| 243 | 50 |
| 244 | 51 echo "This statement is sent to the log file, \"$LOGFILE\"." 1>$LOGFILE |
| 245 | 52 echo "This statement is appended to \"$LOGFILE\"." 1>>$LOGFILE |
| 246 | 53 echo "This statement is also appended to \"$LOGFILE\"." 1>>$LOGFILE |
| 247 | 54 echo "This statement is echoed to stdout, and will not appear in \"$LOGFILE\"." |
| 248 | 55 # These redirection commands automatically "reset" after each line. |
| 249 | 56 |
| 250 | 57 |
| 251 | 58 |
| 252 | 59 # Redirecting stderr, one line at a time. |
| 253 | 60 ERRORFILE=script.errors |
| 254 | 61 |
| 255 | 62 bad_command1 2>$ERRORFILE # Error message sent to $ERRORFILE. |
| 256 | 63 bad_command2 2>>$ERRORFILE # Error message appended to $ERRORFILE. |
| 257 | 64 bad_command3 # Error message echoed to stderr, |
| 258 | 65 #+ and does not appear in $ERRORFILE. |
| 259 | 66 # These redirection commands also automatically "reset" after each line. |
| 260 | 67 #=======================================================================</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 2>&1 |
| 278 | 2 # Redirects stderr to stdout. |
| 279 | 3 # Error messages get sent to same place as standard output. |
| 280 | 4 >>filename 2>&1 |
| 281 | 5 bad_command >>filename 2>&1 |
| 282 | 6 # Appends both stdout and stderr to the file "filename" ... |
| 283 | 7 2>&1 | [command(s)] |
| 284 | 8 bad_command 2>&1 | awk '{print $5}' # found |
| 285 | 9 # Sends stderr through a pipe. |
| 286 | 10 # |& was added to Bash 4 as an abbreviation for 2>&1 |. |
| 287 | 11 |
| 288 | 12 i>&j |
| 289 | 13 # 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 # 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 |
| 316 | 16 >&j |
| 317 | 17 # 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 # 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 0< FILENAME |
| 354 | 2 < FILENAME |
| 355 | 3 # Accept input from a file. |
| 356 | 4 # Companion command to <SPAN |
| 357 | CLASS="QUOTE" |
| 358 | >">"</SPAN |
| 359 | >, and often used in combination with it. |
| 360 | 5 # |
| 361 | 6 # grep search-word <filename |
| 362 | 7 |
| 363 | 8 |
| 364 | 9 [j]<>filename |
| 365 | 10 # Open file "filename" for reading and writing, |
| 366 | 11 #+ and assign file descriptor "j" to it. |
| 367 | 12 # If "filename" does not exist, create it. |
| 368 | 13 # If file descriptor "j" is not specified, default to fd 0, stdin. |
| 369 | 14 # |
| 370 | 15 # An application of this is writing at a specified place in a file. |
| 371 | 16 echo 1234567890 > File # Write string to "File". |
| 372 | 17 exec 3<> File # Open "File" and assign fd 3 to it. |
| 373 | 18 read -n 4 <&3 # Read only 4 characters. |
| 374 | 19 echo -n . >&3 # Write a decimal point there. |
| 375 | 20 exec 3>&- # Close fd 3. |
| 376 | 21 cat File # ==> 1234.67890 |
| 377 | 22 # Random access, by golly. |
| 378 | 23 |
| 379 | 24 |
| 380 | 25 |
| 381 | 26 | |
| 382 | 27 # Pipe. |
| 383 | 28 # General purpose process and command chaining tool. |
| 384 | 29 # Similar to <SPAN |
| 385 | CLASS="QUOTE" |
| 386 | >">"</SPAN |
| 387 | >, but more general in effect. |
| 388 | 30 # Useful for chaining commands, scripts, files, and programs together. |
| 389 | 31 cat *.txt | sort | uniq > result-file |
| 390 | 32 # Sorts the output of all the .txt files and deletes duplicate lines, |
| 391 | 33 # 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 command < input-file > output-file |
| 412 | 2 # Or the equivalent: |
| 413 | 3 < input-file command > output-file # Although this is non-standard. |
| 414 | 4 |
| 415 | 5 command1 | command2 | command3 > 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 ls -yz >> command.log 2>&1 |
| 439 | 2 # Capture result of illegal options "yz" in file "command.log." |
| 440 | 3 # Because stderr is redirected to the file, |
| 441 | 4 #+ any error messages will also be there. |
| 442 | 5 |
| 443 | 6 # Note, however, that the following does *not* give the same result. |
| 444 | 7 ls -yz 2>&1 >> command.log |
| 445 | 8 # Outputs an error message, but does not write to file. |
| 446 | 9 # More precisely, the command output (in this case, null) |
| 447 | 10 #+ writes to the file, but the error message goes only to stdout. |
| 448 | 11 |
| 449 | 12 # If redirecting both stdout and stderr, |
| 450 | 13 #+ 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<&-</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<&-</SPAN |
| 484 | >, <SPAN |
| 485 | CLASS="TOKEN" |
| 486 | ><&-</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>&-</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>&-</SPAN |
| 513 | >, <SPAN |
| 514 | CLASS="TOKEN" |
| 515 | >>&-</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 # Redirecting only stderr to a pipe. |
| 543 | 2 |
| 544 | 3 exec 3>&1 # Save current "value" of stdout. |
| 545 | 4 ls -l 2>&1 >&3 3>&- | grep bad 3>&- # Close fd 3 for 'grep' (but not 'ls'). |
| 546 | 5 # ^^^^ ^^^^ |
| 547 | 6 exec 3>&- # Now close it for the remainder of the script. |
| 548 | 7 |
| 549 | 8 # 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 <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 #!/bin/bash |
| 625 | 2 # Redirecting stdin using 'exec'. |
| 626 | 3 |
| 627 | 4 |
| 628 | 5 exec 6<&0 # Link file descriptor #6 with stdin. |
| 629 | 6 # Saves stdin. |
| 630 | 7 |
| 631 | 8 exec < data-file # stdin replaced by file "data-file" |
| 632 | 9 |
| 633 | 10 read a1 # Reads first line of file "data-file". |
| 634 | 11 read a2 # Reads second line of file "data-file." |
| 635 | 12 |
| 636 | 13 echo |
| 637 | 14 echo "Following lines read from file." |
| 638 | 15 echo "-------------------------------" |
| 639 | 16 echo $a1 |
| 640 | 17 echo $a2 |
| 641 | 18 |
| 642 | 19 echo; echo; echo |
| 643 | 20 |
| 644 | 21 exec 0<&6 6<&- |
| 645 | 22 # Now restore stdin from fd #6, where it had been saved, |
| 646 | 23 #+ and close fd #6 ( 6<&- ) to free it for other processes to use. |
| 647 | 24 # |
| 648 | 25 # <&6 6<&- also works. |
| 649 | 26 |
| 650 | 27 echo -n "Enter data " |
| 651 | 28 read b1 # Now "read" functions as expected, reading from normal stdin. |
| 652 | 29 echo "Input read from stdin." |
| 653 | 30 echo "----------------------" |
| 654 | 31 echo "b1 = $b1" |
| 655 | 32 |
| 656 | 33 echo |
| 657 | 34 |
| 658 | 35 exit 0</PRE |
| 659 | ></TD |
| 660 | ></TR |
| 661 | ></TABLE |
| 662 | ><HR></DIV |
| 663 | ><P |
| 664 | >Similarly, an <B |
| 665 | CLASS="COMMAND" |
| 666 | >exec >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 > 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 > 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 #!/bin/bash |
| 748 | 2 # reassign-stdout.sh |
| 749 | 3 |
| 750 | 4 LOGFILE=logfile.txt |
| 751 | 5 |
| 752 | 6 exec 6>&1 # Link file descriptor #6 with stdout. |
| 753 | 7 # Saves stdout. |
| 754 | 8 |
| 755 | 9 exec > $LOGFILE # stdout replaced with file "logfile.txt". |
| 756 | 10 |
| 757 | 11 # ----------------------------------------------------------- # |
| 758 | 12 # All output from commands in this block sent to file $LOGFILE. |
| 759 | 13 |
| 760 | 14 echo -n "Logfile: " |
| 761 | 15 date |
| 762 | 16 echo "-------------------------------------" |
| 763 | 17 echo |
| 764 | 18 |
| 765 | 19 echo "Output of \"ls -al\" command" |
| 766 | 20 echo |
| 767 | 21 ls -al |
| 768 | 22 echo; echo |
| 769 | 23 echo "Output of \"df\" command" |
| 770 | 24 echo |
| 771 | 25 df |
| 772 | 26 |
| 773 | 27 # ----------------------------------------------------------- # |
| 774 | 28 |
| 775 | 29 exec 1>&6 6>&- # Restore stdout and close file descriptor #6. |
| 776 | 30 |
| 777 | 31 echo |
| 778 | 32 echo "== stdout now restored to default == " |
| 779 | 33 echo |
| 780 | 34 ls -al |
| 781 | 35 echo |
| 782 | 36 |
| 783 | 37 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 #!/bin/bash |
| 817 | 2 # upperconv.sh |
| 818 | 3 # Converts a specified input file to uppercase. |
| 819 | 4 |
| 820 | 5 E_FILE_ACCESS=70 |
| 821 | 6 E_WRONG_ARGS=71 |
| 822 | 7 |
| 823 | 8 if [ ! -r "$1" ] # Is specified input file readable? |
| 824 | 9 then |
| 825 | 10 echo "Can't read from input file!" |
| 826 | 11 echo "Usage: $0 input-file output-file" |
| 827 | 12 exit $E_FILE_ACCESS |
| 828 | 13 fi # Will exit with same error |
| 829 | 14 #+ even if input file ($1) not specified (why?). |
| 830 | 15 |
| 831 | 16 if [ -z "$2" ] |
| 832 | 17 then |
| 833 | 18 echo "Need to specify output file." |
| 834 | 19 echo "Usage: $0 input-file output-file" |
| 835 | 20 exit $E_WRONG_ARGS |
| 836 | 21 fi |
| 837 | 22 |
| 838 | 23 |
| 839 | 24 exec 4<&0 |
| 840 | 25 exec < $1 # Will read from input file. |
| 841 | 26 |
| 842 | 27 exec 7>&1 |
| 843 | 28 exec > $2 # Will write to output file. |
| 844 | 29 # Assumes output file writable (add check?). |
| 845 | 30 |
| 846 | 31 # ----------------------------------------------- |
| 847 | 32 cat - | tr a-z A-Z # Uppercase conversion. |
| 848 | 33 # ^^^^^ # Reads from stdin. |
| 849 | 34 # ^^^^^^^^^^ # Writes to stdout. |
| 850 | 35 # However, both stdin and stdout were redirected. |
| 851 | 36 # Note that the 'cat' can be omitted. |
| 852 | 37 # ----------------------------------------------- |
| 853 | 38 |
| 854 | 39 exec 1>&7 7>&- # Restore stout. |
| 855 | 40 exec 0<&4 4<&- # Restore stdin. |
| 856 | 41 |
| 857 | 42 # After restoration, the following line prints to stdout as expected. |
| 858 | 43 echo "File \"$1\" written to \"$2\" as uppercase conversion." |
| 859 | 44 |
| 860 | 45 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 #!/bin/bash |
| 889 | 2 # avoid-subshell.sh |
| 890 | 3 # Suggested by Matthew Walker. |
| 891 | 4 |
| 892 | 5 Lines=0 |
| 893 | 6 |
| 894 | 7 echo |
| 895 | 8 |
| 896 | 9 cat myfile.txt | while read line; |
| 897 | 10 do { |
| 898 | 11 echo $line |
| 899 | 12 (( Lines++ )); # Incremented values of this variable |
| 900 | 13 #+ inaccessible outside loop. |
| 901 | 14 # Subshell problem. |
| 902 | 15 } |
| 903 | 16 done |
| 904 | 17 |
| 905 | 18 echo "Number of lines read = $Lines" # 0 |
| 906 | 19 # Wrong! |
| 907 | 20 |
| 908 | 21 echo "------------------------" |
| 909 | 22 |
| 910 | 23 |
| 911 | 24 exec 3<> myfile.txt |
| 912 | 25 while read line <&3 |
| 913 | 26 do { |
| 914 | 27 echo "$line" |
| 915 | 28 (( Lines++ )); # Incremented values of this variable |
| 916 | 29 #+ accessible outside loop. |
| 917 | 30 # No subshell, no problem. |
| 918 | 31 } |
| 919 | 32 done |
| 920 | 33 exec 3>&- |
| 921 | 34 |
| 922 | 35 echo "Number of lines read = $Lines" # 8 |
| 923 | 36 |
| 924 | 37 echo |
| 925 | 38 |
| 926 | 39 exit 0 |
| 927 | 40 |
| 928 | 41 # Lines below not seen by script. |
| 929 | 42 |
| 930 | 43 $ cat myfile.txt |
| 931 | 44 |
| 932 | 45 Line 1. |
| 933 | 46 Line 2. |
| 934 | 47 Line 3. |
| 935 | 48 Line 4. |
| 936 | 49 Line 5. |
| 937 | 50 Line 6. |
| 938 | 51 Line 7. |
| 939 | 52 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 | > |