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

📄 ch13_01.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 13] Redirecting Input and Output</TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:36:37Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="part02.htm"TITLE="II. Let the Computer Do the Dirty Work "><LINKREL="prev"HREF="ch12_09.htm"TITLE="12.9 Running Multiple Shell Sessions with screen "><LINKREL="next"HREF="ch13_02.htm"TITLE="13.2 One Argument with a cat Isn't Enough "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><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"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 13</FONT></B></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></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-13">13. Redirecting Input and Output</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-1023"TITLE="13.1 Using Standard Input and Output">Using Standard Input and Output</A><BR><ACLASS="sect1"HREF="ch13_02.htm"TITLE="13.2 One Argument with a cat Isn't Enough ">One Argument with a cat Isn't Enough </A><BR><ACLASS="sect1"HREF="ch13_03.htm"TITLE="13.3 Send (only) Standard Error Down a Pipe ">Send (only) Standard Error Down a Pipe </A><BR><ACLASS="sect1"HREF="ch13_04.htm"TITLE="13.4 Problems Piping to a Pager ">Problems Piping to a Pager </A><BR><ACLASS="sect1"HREF="ch13_05.htm"TITLE="13.5 Redirection in C Shell: Capture Errors, Too? ">Redirection in C Shell: Capture Errors, Too? </A><BR><ACLASS="sect1"HREF="ch13_06.htm"TITLE="13.6 Safe I/O Redirection with noclobber ">Safe I/O Redirection with noclobber </A><BR><ACLASS="sect1"HREF="ch13_07.htm"TITLE="13.7 The () Subshell Operators ">The () Subshell Operators </A><BR><ACLASS="sect1"HREF="ch13_08.htm"TITLE="13.8 Using {list} to Group Bourne Shell Commands ">Using {list} to Group Bourne Shell Commands </A><BR><ACLASS="sect1"HREF="ch13_09.htm"TITLE="13.9 Send Output Two or More Places with tee ">Send Output Two or More Places with tee </A><BR><ACLASS="sect1"HREF="ch13_10.htm"TITLE="13.10 How to tee Several Commands Into One File ">How to tee Several Commands Into One File </A><BR><ACLASS="sect1"HREF="ch13_11.htm"TITLE="13.11 tpipe-Redirecting stdout to More than One Place ">tpipe-Redirecting stdout to More than One Place </A><BR><ACLASS="sect1"HREF="ch13_12.htm"TITLE="13.12 Writing to Multiple Terminals for Demonstrations ">Writing to Multiple Terminals for Demonstrations </A><BR><ACLASS="sect1"HREF="ch13_13.htm"TITLE='13.13 The "Filename&quot; - '>The &quot;Filename&quot; - </A><BR><ACLASS="sect1"HREF="ch13_14.htm"TITLE="13.14 What Can You Do with an Empty File? ">What Can You Do with an Empty File? </A><BR><ACLASS="sect1"HREF="ch13_15.htm"TITLE="13.15 What to Do with a Full Bit Bucket :-) ">What to Do with a Full Bit Bucket <CODECLASS="literal">:-)</CODE> </A><BR><ACLASS="sect1"HREF="ch13_16.htm"TITLE="13.16 Store and Show Errors with logerrs ">Store and Show Errors with logerrs </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1023">13.1 Using Standard Input and Output</A></H2><PCLASS="para">There is absolutelyno difference between reading datafrom a file and reading data from a terminal.[1]Likewise, if aprogram's output consists entirely of alphanumeric characters andpunctuation, there is no difference between writing to a file, writingto a terminal, and writing to the input of another program (as in apipe).</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] If a program'sinput consists entirely of alphanumeric characters and punctuation (i.e., ASCII data or international (non-English) characters).</P></BLOCKQUOTE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13926"></A><ACLASS="indexterm"NAME="AUTOID-13928"></A>The <EMCLASS="emphasis">standard I/O</EM> facility provides some simple defaults formanaging Input/Output. There are three default I/O streams: standard input,standard output, and standard error. By convention, standard output(abbreviated <EMCLASS="emphasis">stdout</EM>)consists of all &quot;normal&quot; output from your program, while standard error(<EMCLASS="emphasis">stderr</EM>) consists of error messages. It is often a convenience to beable to handle error messages and standard output separately.If you don't do anything special, programs will read standard inputfrom your keyboard, and they will send standard output and standarderror to your terminal's display.</P><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1023-IX-STDIN-STANDARD-INPUT"></A><ACLASS="indexterm"NAME="UPT-ART-1023-IX-INPUT-STDIN"></A>Standard input (<EMCLASS="emphasis">stdin</EM>) normally comes from your keyboard.Many programs ignore <EMCLASS="emphasis">stdin</EM>; you name files directlyon their command line&nbsp;- for instance, the command <EMCLASS="emphasis">cat&nbsp;file1&nbsp;file2</EM>never reads its standard input; it reads the files directly.But, without filenames on the command line, UNIX commands that need inputwill usually read <EMCLASS="emphasis">stdin</EM>.Standard input normally comes from your keyboard, but the shell canredirect <EMCLASS="emphasis">stdin</EM> from a file.This is handy for UNIX commands that can't open files directly&nbsp;- for instance,<SPANCLASS="link"><EMCLASS="emphasis">mail</EM> (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>.To mail a file to <EMCLASS="emphasis">joan</EM>, use <CODECLASS="literal">&lt;&nbsp;</CODE><CODECLASS="replaceable"><I>filename</I></CODE>-to tell theshell to attach the file, instead of your keyboard, to <EMCLASS="emphasis">mail</EM>'s standardinput:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mail joan &lt; myfile</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13955"></A><ACLASS="indexterm"NAME="AUTOID-13958"></A>The real virtue of standard I/O is that it allows you to<EMCLASS="emphasis">redirect</EM> input or output away from your terminal to a file.As we said, UNIX is<SPANCLASS="link">file-based (<ACLASS="linkend"HREF="ch01_29.htm"TITLE="When Is a File Not a File? ">1.29</A>)</SPAN>.Because terminals and other I/Odevices are treated as files, a program doesn't care or even know[2]if it is sending its output to a terminal or to a file. For example, ifyou want to run the command <EMCLASS="emphasis">cat&nbsp;file1&nbsp;file2</EM>, but you want toplace the output in <EMCLASS="emphasis">file3</EM> rather than sending it to yourterminal, give the command:</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[2] A program can find out.</P></BLOCKQUOTE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat file1 file2 &gt; file3</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This is called <EMCLASS="emphasis">redirecting</EM> standard output to <EMCLASS="emphasis">file3</EM>. Ifyou give this command and look at <EMCLASS="emphasis">file3</EM> afterward,you will find the contents of <EMCLASS="emphasis">file1</EM>, followed by<EMCLASS="emphasis">file2</EM>-exactly what you would have seen on your screen if youomitted the <CODECLASS="literal">&gt;&nbsp;file3</CODE> modifier. </P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13978"></A>One of the best-known forms of redirection in UNIX is the <EMCLASS="emphasis">pipe</EM>.The shell's vertical bar (<CODECLASS="literal">|</CODE>) operator makes a pipe.For example, to send both <EMCLASS="emphasis">file1</EM> and <EMCLASS="emphasis">file2</EM> together in a mailmessage for <EMCLASS="emphasis">joan</EM>, type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat file1 file2 | mail joan</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The pipe says &quot;connect the standard output of the process at the left(<CODECLASS="literal">cat</CODE>) to the standard input of the process at the right(<CODECLASS="literal">mail</CODE>).&quot;</P><PCLASS="para">Article<ACLASS="xref"HREF="ch45_20.htm"TITLE="Overview: Open Files and File Descriptors ">45.20</A>has diagrams and more information about standard I/O and redirection.<ACLASS="xref"HREF="ch13_01.htm#UPT-ART-1023-TAB-0"TITLE="Common Standard I/O Redirections">Table 13.1</A>shows the mostcommon ways of redirecting standard I/O, for both the C shell and theBourne shell.</P><TABLECLASS="table"><CAPTIONCLASS="table"><ACLASS="title"NAME="UPT-ART-1023-TAB-0">Table 13.1: Common Standard I/O Redirections</A></CAPTION><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Function</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">csh</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">sh</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Send <EMCLASS="emphasis">stdout</EM> to <EMCLASS="emphasis">file</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog &gt; <CODECLASS="replaceable"><I>file</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog &gt; <CODECLASS="replaceable"><I>file</I></CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Send <EMCLASS="emphasis">stderr</EM> to <EMCLASS="emphasis">file</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog 2&gt; <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 <EMCLASS="emphasis">file</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog &gt;&amp; <CODECLASS="replaceable"><I>file</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">prog &gt; <CODECLASS="replaceable"><I>file</I></CODE

⌨️ 快捷键说明

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