<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LinuxByte &#187; 重定向</title>
	<atom:link href="http://www.linuxbyte.org/tag/%e9%87%8d%e5%ae%9a%e5%90%91/feed" rel="self" type="application/rss+xml" />
	<link>http://www.linuxbyte.org</link>
	<description>一个Linux Blog</description>
	<lastBuildDate>Thu, 26 Jan 2012 08:13:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Bash的输入输出重定向</title>
		<link>http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html</link>
		<comments>http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html#comments</comments>
		<pubDate>Mon, 03 Mar 2008 09:29:41 +0000</pubDate>
		<dc:creator>xiao H</dc:creator>
				<category><![CDATA[shell]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[重定向]]></category>

		<guid isPermaLink="false">http://www.linuxbyte.org/bash-quick-reference</guid>
		<description><![CDATA[使用Bash可以方便的用&#60;和&#62;实现输出输入的重定向，本文讨论重定向的一些细节和技巧。本文介绍部分是对Bash Quick Reference相关内容的翻译。 基础知识 文件描述符(File Descriptor)，用一个数字... ]]></description>
			<content:encoded><![CDATA[<p>使用Bash可以方便的用&lt;和&gt;实现输出输入的重定向，本文讨论重定向的一些细节和技巧。本文介绍部分是对Bash Quick Reference相关内容的翻译。</p>
<p><strong>  基础知识</strong></p>
<p>文件描述符(File Descriptor)，用一个数字（通常为0-9<span id="more-58"></span>）来表示一个文件。常用的文件描述符如下：<br />
文件描述符 	名称 	常用缩写 	默认值<br />
0 	标准输入 	stdin 	键盘<br />
1 	标准输出 	stdout 	屏幕<br />
2 	标准错误输出 	stderr 	屏幕</p>
<p>我们在简单地用&lt;或&gt;时，相当于使用 0&lt; 或 1&gt;（下面会详细介绍）。</p>
<p>管道“|”(pipe line)，把上一个命令的 stdout 接到下一个命令的 stdin;</p>
<p>tee 命令的作用是在不影响原本 I/O 的情况下，将 stdout 复制一份到档案去;<br />
<strong> 简单重定向</strong></p>
<p>* cmd &gt; file</p>
<p>把cmd命令的输出重定向到文件file中。如果file已经存在，则清空原有文件，使用bash的noclobber选项可以防止复盖原有文件。</p>
<p>* cmd &gt;&gt; file</p>
<p>把cmd命令的输出重定向到文件file中，如果file已经存在，则把信息加在原有文件後面。</p>
<p>* cmd &lt; file</p>
<p>使cmd命令从file读入</p>
<p>* cmd &lt;&lt; text</p>
<p>从命令行读取输入，直到一个与text相同的行结束。除非使用引号把输入括起来，此模式将对输入内容进行shell变量替换。如果使用 &lt;&lt;- ，则会忽略接下来输入行首的tab，结束行也可以是一堆tab再加上一个与text相同的内容，可以参考後面的例子。</p>
<p>* cmd &lt;&lt;&lt; word</p>
<p>把word（而不是文件word）和後面的换行作为输入提供给cmd。</p>
<p>* cmd &lt;&gt; file</p>
<p>以读写模式把文件file重定向到输入，文件file不会被破坏。仅当应用程序利用了这一特性时，它才是有意义的。</p>
<p>* cmd &gt;| file</p>
<p>功能同&gt;，但即便在设置了noclobber时也会复盖file文件，注意用的是|而非一些书中说的!，目前仅在csh中仍沿用&gt;!实现这一功能。</p>
<p><strong>使用文件描述符的重定向</strong></p>
<p>使用文件描述符的重定向都使用了&amp;符号。<br />
cmd &gt;&amp;n 	把输出送到文件描述符n<br />
cmd m&gt;&amp;n 	把输出 到文件符m的信息重定向到文件描述符n<br />
cmd &gt;&amp;- 	关闭标准输出<br />
cmd &lt;&amp;n 	输入来自文件描述符n<br />
cmd m&lt;&amp;n 	m来自文件描述各个n<br />
cmd &lt;&amp;- 	关闭标准输入<br />
cmd &lt;&amp;n- 	移动输入文件描述符n而非复制它。（需要解释）<br />
cmd &gt;&amp;n- 	移动输出文件描述符 n而非复制它。（需要解释）<br />
<font color="#333399"> 注意： &gt;&amp;实际上复制了文件描述符，这使得ls &gt; dirlist 2&gt;&amp;1与ls 2&gt;&amp;1 &gt; dirlist的效果不一样。man bash的Redirection节中提及了这段内容。</font><br />
<strong> 重定向的组合应用</strong><br />
cmd 2&gt;file 	把文件描述符2重定向到file，即把错误输出存到file中。<br />
cmd &gt; file 2&gt;&amp;1 	把标准错误重定向到标准输出，再重定向到file，即stderr和stdout都被输出到file中<br />
cmd &amp;&gt; file 	功能与上一个相同，更为简便的写法。<br />
cmd &gt;&amp; file 	功能仍与上一个相同。<br />
cmd &gt; f1 2&gt;f2 	把stdout重定向到f1，而把stderr重定向到f2<br />
tee files 	把stdout原样输出的同时，复制一份到files中。<br />
tee files 	把stderr和stdout都输出到files中，同时输出到屏幕。</p>
<h2  class="related_post_title">与本文关系暧昧的文字</h2><ul class="related_post"><li><a href="http://www.linuxbyte.org/zui-shou-huan-ying-de-10ge-linux-dan-xing-ming-ling.html" title="最受欢迎的10个 Linux 单行命令">最受欢迎的10个 Linux 单行命令</a></li><li><a href="http://www.linuxbyte.org/bash-shell-kuai-jie-jian.html" title="Bash Shell 快捷键">Bash Shell 快捷键</a></li><li><a href="http://www.linuxbyte.org/bash-remove-operating-system.html" title="dell 的 Remove Operating System 代码">dell 的 Remove Operating System 代码</a></li><li><a href="http://www.linuxbyte.org/bash-tips.html" title="我常用的小shell">我常用的小shell</a></li><li><a href="http://www.linuxbyte.org/fun-bash-shell.html" title="几个有用的bash shell组合">几个有用的bash shell组合</a></li></ul><hr />
<p><small>© xiao H for <a href="http://www.linuxbyte.org">LinuxByte</a>, 2008<br />
Source: <a href="http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html">Bash的输入输出重定向</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/bash" rel="tag">Bash</a>, <a href="http://www.linuxbyte.org/tag/%e9%87%8d%e5%ae%9a%e5%90%91" rel="tag">重定向</a><br/>
</small></p>
	<p style="margin:3px 0 0 0;">标签：<a href="http://www.linuxbyte.org/tag/bash" title="Bash" rel="tag">Bash</a>, <a href="http://www.linuxbyte.org/tag/%e9%87%8d%e5%ae%9a%e5%90%91" title="重定向" rel="tag">重定向</a></p>

<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

