⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 io-redirection.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>I/O Redirection</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part5.html"><LINKREL="PREVIOUS"TITLE="Here Documents"HREF="here-docs.html"><LINKREL="NEXT"TITLE="Redirecting Code Blocks"HREF="redircb.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="here-docs.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="redircb.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="IO-REDIRECTION"></A>Chapter 19. I/O Redirection</H1><P><ANAME="IOREDIRREF"></A></P><P>There are always three default <SPANCLASS="QUOTE">"files"</SPAN>	open, <TTCLASS="FILENAME">stdin</TT> (the keyboard),	<TTCLASS="FILENAME">stdout</TT> (the screen), and	<TTCLASS="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 <AHREF="special-chars.html#EX8">Example 3-1</A> and <AHREF="special-chars.html#RPMCHECK">Example 3-2</A>) and sending it as	input to another file, command, program, or script.</P><P><ANAME="FDREF"></A>Each open file gets assigned a file descriptor.	               <ANAME="AEN16482"HREF="#FTN.AEN16482">[1]</A>	The file descriptors for <TTCLASS="FILENAME">stdin</TT>,	<TTCLASS="FILENAME">stdout</TT>, and <TTCLASS="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 <TTCLASS="FILENAME">stdin</TT>,	<TTCLASS="FILENAME">stdout</TT>, or <TTCLASS="FILENAME">stderr</TT>	as a temporary duplicate link.	       <ANAME="AEN16494"HREF="#FTN.AEN16494">[2]</A>	This simplifies restoration to normal after complex redirection	and reshuffling (see <AHREF="io-redirection.html#REDIR1">Example 19-1</A>).</P><P><ANAME="IOREDIRECTIONREF"></A></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;   COMMAND_OUTPUT &#62;   2&nbsp;      # Redirect stdout to a file.   3&nbsp;      # Creates the file if not present, otherwise overwrites it.   4&nbsp;   5&nbsp;      ls -lR &#62; dir-tree.list   6&nbsp;      # Creates a file containing a listing of the directory tree.   7&nbsp;   8&nbsp;   : &#62; filename   9&nbsp;      # The &#62; truncates file "filename" to zero length.  10&nbsp;      # If file not present, creates zero-length file (same effect as 'touch').  11&nbsp;      # The : serves as a dummy placeholder, producing no output.  12&nbsp;  13&nbsp;   &#62; filename      14&nbsp;      # The &#62; truncates file "filename" to zero length.  15&nbsp;      # If file not present, creates zero-length file (same effect as 'touch').  16&nbsp;      # (Same result as ": &#62;", above, but this does not work with some shells.)  17&nbsp;  18&nbsp;   COMMAND_OUTPUT &#62;&#62;  19&nbsp;      # Redirect stdout to a file.  20&nbsp;      # Creates the file if not present, otherwise appends to it.  21&nbsp;  22&nbsp;  23&nbsp;      # Single-line redirection commands (affect only the line they are on):  24&nbsp;      # --------------------------------------------------------------------  25&nbsp;  26&nbsp;   1&#62;filename  27&nbsp;      # Redirect stdout to file "filename."  28&nbsp;   1&#62;&#62;filename  29&nbsp;      # Redirect and append stdout to file "filename."  30&nbsp;   2&#62;filename  31&nbsp;      # Redirect stderr to file "filename."  32&nbsp;   2&#62;&#62;filename  33&nbsp;      # Redirect and append stderr to file "filename."  34&nbsp;   &#38;&#62;filename  35&nbsp;      # Redirect both stdout and stderr to file "filename."  36&nbsp;      #  37&nbsp;      #  Note that   &#38;&#62;&#62;filename  38&nbsp;      #+ -- attempting to redirect and *append*  39&nbsp;      #+ stdout and stderr to file "filename" --  40&nbsp;      #+ fails with the error message,  41&nbsp;      #+ syntax error near unexpected token `&#62;'.  42&nbsp;  43&nbsp;   M&#62;N  44&nbsp;     # "M" is a file descriptor, which defaults to 1, if not explicitly set.  45&nbsp;     # "N" is a filename.  46&nbsp;     # File descriptor "M" is redirect to file "N."  47&nbsp;   M&#62;&#38;N  48&nbsp;     # "M" is a file descriptor, which defaults to 1, if not set.  49&nbsp;     # "N" is another file descriptor.  50&nbsp;  51&nbsp;      #==============================================================================  52&nbsp;  53&nbsp;      # Redirecting stdout, one line at a time.  54&nbsp;      LOGFILE=script.log  55&nbsp;  56&nbsp;      echo "This statement is sent to the log file, \"$LOGFILE\"." 1&#62;$LOGFILE  57&nbsp;      echo "This statement is appended to \"$LOGFILE\"." 1&#62;&#62;$LOGFILE  58&nbsp;      echo "This statement is also appended to \"$LOGFILE\"." 1&#62;&#62;$LOGFILE  59&nbsp;      echo "This statement is echoed to stdout, and will not appear in \"$LOGFILE\"."  60&nbsp;      # These redirection commands automatically "reset" after each line.  61&nbsp;  62&nbsp;  63&nbsp;  64&nbsp;      # Redirecting stderr, one line at a time.  65&nbsp;      ERRORFILE=script.errors  66&nbsp;  67&nbsp;      bad_command1 2&#62;$ERRORFILE       #  Error message sent to $ERRORFILE.  68&nbsp;      bad_command2 2&#62;&#62;$ERRORFILE      #  Error message appended to $ERRORFILE.  69&nbsp;      bad_command3                    #  Error message echoed to stderr,  70&nbsp;                                      #+ and does not appear in $ERRORFILE.  71&nbsp;      # These redirection commands also automatically "reset" after each line.  72&nbsp;      #=======================================================================</PRE></TD></TR></TABLE><P><ANAME="IOREDIRECTIONREF1"></A></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;   2&#62;&#38;1   2&nbsp;      # Redirects stderr to stdout.   3&nbsp;      # Error messages get sent to same place as standard output.   4&nbsp;   5&nbsp;   i&#62;&#38;j   6&nbsp;      # Redirects file descriptor <SPANCLASS="emphasis"><ICLASS="EMPHASIS">i</I></SPAN> to <SPANCLASS="emphasis"><ICLASS="EMPHASIS">j</I></SPAN>.   7&nbsp;      # All output of file pointed to by <SPANCLASS="emphasis"><ICLASS="EMPHASIS">i</I></SPAN> gets sent to file pointed to by <SPANCLASS="emphasis"><ICLASS="EMPHASIS">j</I></SPAN>.   8&nbsp;   9&nbsp;   &#62;&#38;j  10&nbsp;      # Redirects, by default, file descriptor <SPANCLASS="emphasis"><ICLASS="EMPHASIS">1</I></SPAN> (stdout) to <SPANCLASS="emphasis"><ICLASS="EMPHASIS">j</I></SPAN>.  11&nbsp;      # All stdout gets sent to file pointed to by <SPANCLASS="emphasis"><ICLASS="EMPHASIS">j</I></SPAN>.</PRE></TD></TR></TABLE><P><ANAME="IOREDIRECTIONREF2"></A></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;   0&#60; FILENAME   2&nbsp;    &#60; FILENAME   3&nbsp;      # Accept input from a file.   4&nbsp;      # Companion command to <SPANCLASS="QUOTE">"&#62;"</SPAN>, and often used in combination with it.   5&nbsp;      #   6&nbsp;      # grep search-word &#60;filename   7&nbsp;   8&nbsp;   9&nbsp;   [j]&#60;&#62;filename  10&nbsp;      #  Open file "filename" for reading and writing,  11&nbsp;      #+ and assign file descriptor "j" to it.  12&nbsp;      #  If "filename" does not exist, create it.  13&nbsp;      #  If file descriptor "j" is not specified, default to fd 0, stdin.  14&nbsp;      #  15&nbsp;      #  An application of this is writing at a specified place in a file.   16&nbsp;      echo 1234567890 &#62; File    # Write string to "File".  17&nbsp;      exec 3&#60;&#62; File             # Open "File" and assign fd 3 to it.  18&nbsp;      read -n 4 &#60;&#38;3             # Read only 4 characters.  19&nbsp;      echo -n . &#62;&#38;3             # Write a decimal point there.  20&nbsp;      exec 3&#62;&#38;-                 # Close fd 3.  21&nbsp;      cat File                  # ==&#62; 1234.67890  22&nbsp;      #  Random access, by golly.  23&nbsp;  24&nbsp;  25&nbsp;  26&nbsp;   |  27&nbsp;      # Pipe.  28&nbsp;      # General purpose process and command chaining tool.  29&nbsp;      # Similar to <SPANCLASS="QUOTE">"&#62;"</SPAN>, but more general in effect.  30&nbsp;      # Useful for chaining commands, scripts, files, and programs together.  31&nbsp;      cat *.txt | sort | uniq &#62; result-file  32&nbsp;      # Sorts the output of all the .txt files and deletes duplicate lines,  33&nbsp;      # finally saves results to <SPANCLASS="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.       <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;command &#60; input-file &#62; output-file   2&nbsp;   3&nbsp;command1 | command2 | command3 &#62; output-file</PRE></TD></TR></TABLE>        See <AHREF="filearchiv.html#DERPM">Example 15-30</A> and <AHREF="contributed-scripts.html#FIFO">Example A-15</A>.</P><P>Multiple output streams may be redirected to one file.         <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;ls -yz &#62;&#62; command.log 2&#62;&#38;1   2&nbsp;#  Capture result of illegal options "yz" in file "command.log."   3&nbsp;#  Because stderr is redirected to the file,   4&nbsp;#+ any error messages will also be there.   5&nbsp;   6&nbsp;#  Note, however, that the following does *not* give the same result.   7&nbsp;ls -yz 2&#62;&#38;1 &#62;&#62; command.log   8&nbsp;#  Outputs an error message and does not write to file.   9&nbsp;  10&nbsp;#  If redirecting both stdout and stderr,  11&nbsp;#+ the order of the commands makes a difference.</PRE></TD></TR></TABLE></P><DIVCLASS="VARIABLELIST"><P><B><ANAME="CFD"></A>Closing File Descriptors</B></P><DL><DT><SPANCLASS="TOKEN">n&#60;&#38;-</SPAN></DT><DD><P>Close input file descriptor	    <TTCLASS="REPLACEABLE"><I>n</I></TT>.</P></DD><DT><SPANCLASS="TOKEN">0&#60;&#38;-</SPAN>, <SPANCLASS="TOKEN">&#60;&#38;-</SPAN></DT><DD><P>Close <TTCLASS="FILENAME">stdin</TT>.</P></DD><DT><SPANCLASS="TOKEN">n&#62;&#38;-</SPAN></DT><DD><P>Close output file descriptor <TTCLASS="REPLACEABLE"><I>n</I></TT>.</P></DD><DT><SPANCLASS="TOKEN">1&#62;&#38;-</SPAN>, <SPANCLASS="TOKEN">&#62;&#38;-</SPAN></DT><DD><P>Close <TTCLASS="FILENAME">stdout</TT>.</P></DD></DL></DIV><P>Child processes inherit open file descriptors. This is why pipes	  work. To prevent an fd from being inherited, close it.	    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Redirecting only stderr to a pipe.   2&nbsp;   3&nbsp;exec 3&#62;&#38;1                              # Save current "value" of stdout.   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').   5&nbsp;#              ^^^^   ^^^^   6&nbsp;exec 3&#62;&#38;-                              # Now close it for the remainder of the script.   7&nbsp;   8&nbsp;# Thanks, S.C.</PRE></TD></TR></TABLE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -