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

📄 ch04_02.htm

📁 unix基础教程
💻 HTM
📖 第 1 页 / 共 3 页
字号:
</td><td><p>Standard error</p></td><td><p>stderr</p></td><td><p>Terminal</p></td></tr></table><p><p>The usual input source or output destination canbe changed, as seen in the following sections.</p><a name="unut-ch-4-sect-2.5.1" /><div class="sect3"><h3 class="sect3">4.2.5.1. Simple redirection</h3><dl><dt><i><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&gt;</tt> <tt><em class="replaceable">file</em></tt></i></dt><dd>Send output of <em class="emphasis">cmd</em></tt> to <em class="emphasis">file</tt></em></tt> (overwrite).<a name="IXT-4-122697" /></p></dd><dt><i><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&gt;&gt;</tt> <tt><em class="replaceable">file</em></tt></i></dt><dd>Send output of <em class="emphasis">cmd</em></tt> to <em class="emphasis">file</tt></em></tt> (append).<a name="IXT-4-122698" /></p></dd><dt><i><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&lt;</tt> <tt><em class="replaceable">file</em></tt></i></dt><dd>Take input for <em class="emphasis">cmd</em></tt> from <em class="emphasis">file</tt></em></tt>.<a name="IXT-4-122699" /></p></dd><dt><i><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&lt;&lt;</tt> <tt><em class="replaceable">text</tt></em></tt></i></dt><dd><a name="IXT-4-122700" />The contents of the shell scriptup to a line identical to <em class="emphasis">text</em>become the standard input for <em class="emphasis">cmd</em>(<em class="emphasis">text</em> can be stored in a shell variable).This command form is sometimes called a <em class="emphasis">Here document</em>.Input is usually typed at the keyboard or in the shell program.Commands that typically use this syntaxinclude <tt class="literal">cat</tt>,<tt class="literal">ex</tt>,and <tt class="literal">sed</tt>.(If <tt class="literal">&lt;&lt;-</tt>is used, leading tabs are ignored when comparing input withthe end-of-input <em class="emphasis">text</em> marker.)If <em class="emphasis">text</em> is quoted, the input is passed through verbatim.Otherwise,the contents are processed for variable and command substitutions.The Korn shell also processes the input for arithmetic substitution.</p></dd><dt><i><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&lt;&gt;</tt> <tt><em class="replaceable">file</tt></em></tt></i></dt><dd>Korn shell only.<a name="IXT-4-122701" />Open <em class="emphasis">file</em></tt> for reading <em class="emphasis">and</em></tt>writing on the standard input.  The contents are not destroyed.<a href="#FOOTNOTE-5">[5]</a></p><blockquote class="footnote"><a name="FOOTNOTE-5" /><p> [5]With <tt class="literal">&lt;</tt>, the file is opened read-only, andwrites on the file descriptor will fail.With <tt class="literal">&lt;&gt;</tt>, the file is opened read-write;it is up to the application to actually take advantage of this.</p></blockquote></dd></dl></div><a name="unut-ch-4-sect-2.5.2" /><div class="sect3"><h3 class="sect3">4.2.5.2. Redirection using file descriptors</h3><table border="1" cellpadding="3"><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&gt;&amp;</tt><tt><em class="replaceable">n</tt></em></tt> </p></td><td><p>Send <em class="emphasis">cmd</em></tt> output to file descriptor <em class="emphasis">n</em></tt>.</p></td></tr><tr><td><p><a name="IXT-4-122702" /><a name="IXT-4-122703" /><tt><em class="replaceable"><tt>cmd</em></tt> <tt><em class="replaceable">m</em></tt><tt class="literal">&gt;&amp;</tt><tt><em class="replaceable">n</tt></em></tt></p></td><td><p>Same, except that output that would normally go to filedescriptor <em class="emphasis">m</em></tt> is sent to filedescriptor <em class="emphasis">n</em></tt> instead.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</tt></em></tt> <tt class="literal">&gt;&amp;-</tt></p></td><td><p>Close standard output.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&lt;&amp;</tt><tt><em class="replaceable">n</tt></em></tt></p></td><td><p>Take input for <em class="emphasis">cmd</em></tt> from file descriptor <em class="emphasis">n</em></tt>.<a name="IXT-4-122704" /></p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt><em class="replaceable">m</em></tt><tt class="literal">&lt;&amp;</tt><tt><em class="replaceable">n</tt></em></tt></p></td><td><p>Same, except that input that would normally come from filedescriptor <em class="emphasis">m</em></tt> comes from filedescriptor <em class="emphasis">n</em></tt> instead.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</tt></em></tt> <tt class="literal">&lt;&amp;-</tt></p></td><td><p>Close standard input.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&lt;&amp;</tt><tt><em class="replaceable">n</tt></em></tt><tt class="literal">-</tt></p></td><td><p>Move input file descriptor <em class="emphasis">n</em></tt>instead of duplicating it.<tt class="literal">ksh93</tt> only.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&gt;&amp;</tt><tt><em class="replaceable">n</tt></em></tt><tt class="literal">-</tt></p></td><td><p>Move output file descriptor <em class="emphasis">n</em></tt>instead of duplicating it.<tt class="literal">ksh93</tt> only.</p></td></tr></table><p></div><a name="unut-ch-4-sect-2.5.3" /><div class="sect3"><h3 class="sect3">4.2.5.3. Multiple redirection</h3><table border="1" cellpadding="3"><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">2&gt;</tt><tt><em class="replaceable">file</tt></em></tt></p></td><td><p>Send standard error to <em class="emphasis">file</em></tt>; standard output remains the same (e.g., the screen).</p></td></tr><tr><td><p><a name="IXT-4-122705" /><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&gt;</tt> <tt><em class="replaceable">file</tt></em></tt> <tt class="literal">2&gt;&amp;1</tt></p></td><td><p>Send both standard error and standard output to <em class="emphasis">file</em></tt>.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">&gt;</tt> <tt><em class="replaceable">f1</em></tt> <tt class="literal">2&gt;</tt><tt><em class="replaceable">f2</tt></em></tt></p></td><td><p>Send standard output to file <em class="emphasis">f1</em></tt>,standard error to file <em class="emphasis">f2</em></tt>.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">| tee</tt> <tt><em class="replaceable">files</tt></em></tt></p></td><td><p>Send output of <em class="emphasis">cmd</em></tt> to standard output(usually the terminal) and to <em class="emphasis">files</em></tt>.(See the Example in<a href="ch02_01.htm">Chapter 2</a>,under <b class="emphasis-bold">tee</b>.)</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</em></tt> <tt class="literal">2&gt;&amp;1 | tee</tt> <tt><em class="replaceable">files</tt></em></tt></p></td><td><p>Send standard output and error outputof <em class="emphasis">cmd</em></tt> to standard output(usually the terminal) and to <em class="emphasis">files</em></tt>.</p></td></tr></table><p><p>No space should appear between file descriptors and a redirection symbol;spacing is optional in the other cases.</p></div><a name="unut-ch-4-sect-2.5.4" /><div class="sect3"><h3 class="sect3">4.2.5.4. Examples</h3><blockquote><pre class="code">$ <tt class="userinput"><b>cat part1 &gt; book</b></tt>$ <tt class="userinput"><b>cat part2 part3 &gt;&gt; book</b></tt>$ <tt class="userinput"><b>mail tim &lt; report</b></tt>$ <tt class="userinput"><b>sed 's/^/XX /g' &lt;&lt; END_ARCHIVE</b></tt>&gt; <tt class="userinput"><b>This is often how a shell archive is "wrapped",</b></tt>&gt; <tt class="userinput"><b>bundling text for distribution.  You would normally</b></tt>&gt; <tt class="userinput"><b>run sed from a shell program, not from the command line.</b></tt>&gt; <tt class="userinput"><b>END_ARCHIVE</b></tt>XX This is often how a shell archive is "wrapped",XX bundling text for distribution.  You would normallyXX run sed from a shell program, not from the command line.</pre></blockquote><p>To redirect standard output to standard error:</p><blockquote><pre class="code">$ <tt class="userinput"><b>echo "Usage error:  see administrator" 1&gt;&amp;2</b></tt></pre></blockquote><p>The following commandsends output (files found) to <tt class="literal">filelist</tt> and errormessages (inaccessible files) to file <tt class="literal">no_access</tt>:</p><blockquote><pre class="code">$ <tt class="userinput"><b>find / -print &gt; filelist 2&gt;no_access</b></tt></pre></blockquote></div></div><a name="unut-ch-4-sect-2.6" /><div class="sect2"><h3 class="sect2">4.2.6. Coprocesses</h3><p><a name="IXT-4-122706" /><a name="IXT-4-122707" />Coprocesses are a feature of the Korn shell only.</p><table border="1" cellpadding="3"><tr><td><p><tt><em class="replaceable"><tt>cmd1</em></tt> <tt class="literal">|</tt> <tt><em class="replaceable">cmd2</tt></em></tt> <tt class="literal">|&amp;</tt></p></td><td><p>Coprocess; execute the pipeline in the background.The shell sets up a two-way pipe, allowing redirection ofboth standard input and standard output.</p></td></tr><tr><td><p><tt class="literal">read -p</tt> <tt><em class="replaceable"><tt>var</tt></em></tt></p></td><td><p>Read coprocess output into variable <em class="emphasis">var</em></tt>.</p></td></tr><tr><td><p><tt class="literal">print -p</tt> <tt><em class="replaceable"><tt>string</tt></em></tt></p></td><td><p>Write <em class="emphasis">string</em></tt> to the coprocess.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</tt></em></tt> <tt class="literal">&lt;&amp;p</tt></p></td><td><p>Take input for <em class="emphasis">cmd</em></tt> from the coprocess.</p></td></tr><tr><td><p><tt><em class="replaceable"><tt>cmd</tt></em></tt> <tt class="literal">&gt;&amp;p</tt></p></td><td><p>Send output of <em class="emphasis">cmd</em></tt> to the coprocess.</p></td></tr><tr><td><p><tt class="literal">exec</tt> <tt><em class="replaceable"><tt>n</tt></em></tt><tt class="literal">&lt;&amp;p</tt></p></td><td><p>Move input for coprocess to file descriptor <em class="emphasis">n</em></tt>.</p></td></tr><tr><td><p><tt class="literal">exec</tt> <tt><em class="replaceable"><tt>n</tt></em></tt><tt class="literal">&gt;&amp;p</tt></p></td><td><p>Move output from coprocess to file descriptor <em class="emphasis">n</em></tt>.</p></td></tr></table><p><p>Moving the coprocess input and output file descriptors to standardfile descriptors allows you to open multiple coprocesses.</p><a name="unut-ch-4-sect-2.6.1" /><div class="sect3"><h3 class="sect3">4.2.6.1. Examples</h3><blockquote><pre class="code">$<tt class="userinput"><b>ed - memo |&amp;</b></tt>		        <i class="lineannotation">Start coprocess</i>$ <tt class="userinput"><b>print -p /word/</b></tt>		<i class="lineannotation">Send ed command to coprocess</i>$ <tt class="userinput"><b>read -p search</b></tt>		<i class="lineannotation">Read output of ed command into variable search</i>$ <tt class="userinput"><b>print "$search"</b></tt>		<i class="lineannotation">Show the line on standard output</i>A word to the wise.</pre></blockquote></div></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch04_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">4. The Bourne Shell and Korn Shell</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">4.3. Variables</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2003</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,88,96" href="../index.htm"><area shape="rect" coords="90,0,165,96" href="../upt/index.htm"><area shape="rect" coords="168,1,253,107" href="../mac/index.htm"><area shape="rect" coords="255,0,335,97" href="../korn/index.htm"><area shape="rect" coords="337,0,415,109" href="../unixnut/index.htm"><area shape="rect" coords="417,0,512,122" href="../sedawk/index.htm"><area shape="rect" coords="514,0,605,105" href="../lunix/index.htm"><area shape="rect" coords="611,2,694,121" href="../vi/index.htm"></map></body></html>

⌨️ 快捷键说明

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