📄 ch13_01.htm
字号:
> 2>&1</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Take <EMCLASS="emphasis">stdin</EM> from <EMCLASS="emphasis">file</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog < <CODECLASS="replaceable"><I>file</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog < <CODECLASS="replaceable"><I>file</I></CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Send <EMCLASS="emphasis">stdout</EM> to end of <EMCLASS="emphasis">file</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog >> <CODECLASS="replaceable"><I>file</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog >> <CODECLASS="replaceable"><I>file</I></CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Send <EMCLASS="emphasis">stderr</EM> to end of <EMCLASS="emphasis">file</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog 2>> <CODECLASS="replaceable"><I>file</I></CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Send <EMCLASS="emphasis">stdout</EM> and <EMCLASS="emphasis">stderr</EM> to end of <EMCLASS="emphasis">file</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog >>& <CODECLASS="replaceable"><I>file</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog >> <CODECLASS="replaceable"><I>file</I></CODE> 2>&1</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Read <EMCLASS="emphasis">stdin</EM> from keyboard until <EMCLASS="emphasis">c</EM> (seearticle <ACLASS="xref"HREF="ch08_18.htm"TITLE="Here Documents ">8.18</A>)</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog <<<CODECLASS="replaceable"><I>c</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog <<<CODECLASS="replaceable"><I>c</I></CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Pipe <EMCLASS="emphasis">stdout</EM> to <EMCLASS="emphasis">prog2</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog | prog2</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog | prog2</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Pipe <EMCLASS="emphasis">stdout</EM> and <EMCLASS="emphasis">stderr</EM> to <EMCLASS="emphasis">prog2</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog |& prog2</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog 2>&1 | prog2</TD></TR></TBODY></TABLE><PCLASS="para">Be aware that:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-14086"></A>While standard I/O is a basic feature of UNIX, the syntax used toredirect standard I/O depends on the shell you are using. Bourneshell syntax and C shell syntax differ, particularly when you get intothe less commonly used features. The Korn shell and <EMCLASS="emphasis">bash</EM> arethe same as the Bourne shell, but with a few twists of their own. </P></LI><LICLASS="listitem"><PCLASS="para">You can redirect standard input and standard output in the same command line.For example, to read from the file <EMCLASS="emphasis">input</EM> and write to the file<EMCLASS="emphasis">output</EM>, give the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>prog < input > output</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The Bourne shell will let you go further and write <EMCLASS="emphasis">stderr</EM> to a third file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>prog < input > output 2> errors</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-14105"></A>The C shell doesn't give you an easy way to redirectstandard output without redirecting standard error. A simple trickwill help you do this. To put standard output and standard error indifferent files, give a command like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>( prog > output ) >& errors</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">We'll discuss commands like this in articles<ACLASS="xref"HREF="ch13_03.htm"TITLE="Send (only) Standard Error Down a Pipe ">13.3</A>and<ACLASS="xref"HREF="ch13_05.htm"TITLE="Redirection in C Shell: Capture Errors, Too? ">13.5</A>.</P></LI><LICLASS="listitem"><PCLASS="para">Many implementations of both shells don't care what order theredirections appear in, or even where they appear on the command line.For example, SunOS lets you type <CODECLASS="literal"><</CODE> <CODECLASS="literal">input</CODE> <CODECLASS="literal">></CODE> <CODECLASS="literal">output</CODE> <CODECLASS="literal">prog</CODE>.However, clarity is always a virtue that computer users have neverappreciated enough. It will be easiest to understandwhat you are doing if you type the command name first - then redirect standardinput, followed by standard output, followed by standard error.</P></LI></UL><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-14122"></A><SPANCLASS="link"> There are some more complex forms of standard I/O redirection,particularly for the Bourne shell . (<ACLASS="linkend"HREF="ch45_21.htm"TITLE="n>&m: Swap Standard Output and Standard Error ">45.21</A>, <ACLASS="linkend"HREF="ch45_22.htm"TITLE="Handling Files Line-by-Line ">45.22</A>, <ACLASS="linkend"HREF="ch45_23.htm"TITLE="The Ins and Outs of Redirected I/O Loops ">45.23</A>)</SPAN></P><PCLASS="para">Of course, programs aren't restricted to standard I/O. They can openother files, define their own special-purpose pipes, and writedirectly to the terminal. But standard I/O is the glue that allowsyou to make big programs out of smaller ones, and is therefore acrucial part of the operating system. Most UNIX utilities read their data from standard input and write their output to standardoutput, allowing you to combine them easily. A program that createsits own special-purpose pipe may be very useful, but it cannot beused in combination with standard utilities.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-14128"></A>Some UNIX systems, and utilities such as<SPANCLASS="link"><EMCLASS="emphasis">gawk</EM> (<ACLASS="linkend"HREF="ch33_12.htm"TITLE="Versions of awk ">33.12</A>)</SPAN>,support special filenames like <EMCLASS="emphasis">/dev/stdin</EM>, <EMCLASS="emphasis">/dev/stdout</EM>, and<EMCLASS="emphasis">/dev/stderr</EM>.You can use these just as you'd use other files.For instance, to have any ordinary command read from the file <EMCLASS="emphasis">afile</EM>,then standard input (from the keyboard, for example), then the file<EMCLASS="emphasis">bfile</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="replaceable"><I>somecmd</I></CODE><CODECLASS="userinput"><B> afile /dev/stdin bfile</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">In the same way, a process can write to its standard output through<EMCLASS="emphasis">/dev/stdout</EM> and the standard error via <EMCLASS="emphasis">/dev/stderr</EM>.<ACLASS="indexterm"NAME="AUTOID-14144"></A><ACLASS="indexterm"NAME="AUTOID-14145"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch12_09.htm"TITLE="12.9 Running Multiple Shell Sessions with screen "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 12.9 Running Multiple Shell Sessions with screen "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch13_02.htm"TITLE="13.2 One Argument with a cat Isn't Enough "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 13.2 One Argument with a cat Isn't Enough "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">12.9 Running Multiple Shell Sessions with screen </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">13.2 One Argument with a cat Isn't Enough </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -