<?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; Bash</title>
	<atom:link href="http://www.linuxbyte.org/tag/bash/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>最受欢迎的10个 Linux 单行命令</title>
		<link>http://www.linuxbyte.org/zui-shou-huan-ying-de-10ge-linux-dan-xing-ming-ling.html</link>
		<comments>http://www.linuxbyte.org/zui-shou-huan-ying-de-10ge-linux-dan-xing-ming-ling.html#comments</comments>
		<pubDate>Wed, 16 Feb 2011 05:27:28 +0000</pubDate>
		<dc:creator>xiao H</dc:creator>
				<category><![CDATA[shell]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://www.linuxbyte.org/?p=608</guid>
		<description><![CDATA[下面是来自 Commandlinefu 网站由用户投票决出的 10 个最酷的 Linux 单行命令。 sudo !! 以 root 帐户执行上一条命令。 python -m SimpleHTTPServer 利用 Python 搭建一个简单的 Web 服务器，可通过 http://$HOSTNAME:8... ]]></description>
			<content:encoded><![CDATA[<p>下面是来自 <a href="http://commandlinefu.com/">Commandlinefu</a> 网站由用户投票决出的 10 个最酷的 Linux 单行命令。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">sudo !!</pre></div></div>

<p>以 root 帐户执行上一条命令。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">python -m SimpleHTTPServer</pre></div></div>

<p>利用 Python 搭建一个简单的 Web 服务器，可通过 http://$HOSTNAME:8000 访问。<br />
<span id="more-608"></span></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">:w !sudo tee %</pre></div></div>

<p>在 Vim 中无需权限保存编辑的文件。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">cd -</pre></div></div>

<p>更改到上一次访问的目录。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">^foo^bar</pre></div></div>

<p>将上一条命令中的 foo 替换为 bar，并执行。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">cp filename{,.bak}</pre></div></div>

<p>快速备份或复制文件。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">mtr google.com</pre></div></div>

<p>traceroute + ping。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">!whatever:p</pre></div></div>

<p>搜索命令历史，但不执行。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">ssh-copy-id user@host</pre></div></div>

<p>将 ssh keys 复制到 user@host 以启用无密码 SSH 登录。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg</pre></div></div>

<p>把 Linux 桌面录制为视频。</p>
<h2  class="related_post_title">与本文关系暧昧的文字</h2><ul class="related_post"><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><li><a href="http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html" title="Bash的输入输出重定向">Bash的输入输出重定向</a></li></ul><hr />
<p><small>© xiao H for <a href="http://www.linuxbyte.org">LinuxByte</a>, 2011<br />
Source: <a href="http://www.linuxbyte.org/zui-shou-huan-ying-de-10ge-linux-dan-xing-ming-ling.html">最受欢迎的10个 Linux 单行命令</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/bash" rel="tag">Bash</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></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/zui-shou-huan-ying-de-10ge-linux-dan-xing-ming-ling.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash Shell 快捷键</title>
		<link>http://www.linuxbyte.org/bash-shell-kuai-jie-jian.html</link>
		<comments>http://www.linuxbyte.org/bash-shell-kuai-jie-jian.html#comments</comments>
		<pubDate>Sun, 06 Jun 2010 06:13:46 +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/?p=563</guid>
		<description><![CDATA[CTRL 键 Ctrl + a - Jump to the start of the line Ctrl + b - Move back a char Ctrl + c - Terminate the command //用的最多了吧? Ctrl + d - Delete from under the cursor Ctrl + e - Jump to the end of the line Ctrl + f - Move forward [...... ]]></description>
			<content:encoded><![CDATA[<h4><acronym title="Control">CTRL</acronym> 键</h4>
<pre>Ctrl + a - Jump to the start of the line
Ctrl + b - Move back a char
Ctrl + c - Terminate the command  //用的最多了吧?
Ctrl + d - Delete from under the cursor
Ctrl + e - Jump to the end of the line
Ctrl + f - Move forward a char
Ctrl + k - Delete to EOL
Ctrl + l - Clear the screen  //清屏，类似 clear 命令
Ctrl + r - Search the history backwards  //查找历史命令
Ctrl + R - Search the history backwards with multi occurrence
Ctrl + u - Delete backward from cursor // 密码输入错误的时候比较有用
Ctrl + xx - Move between EOL and current cursor position
Ctrl + x @ - Show possible hostname completions
Ctrl + z - Suspend/ Stop the command <span id="more-563"></span>
补充:
Ctrl + h - 删除当前字符
Ctrl + w - 删除最后输入的单词
</pre>
<h4>ALT  键</h4>
<p>平时很少用。有些和远程登陆工具冲突。</p>
<pre>Alt + &lt; - Move to the first line in the history
Alt + &gt; - Move to the last line in the history
Alt + ? - Show current completion list
Alt + * - Insert all possible completions
Alt + / - Attempt to complete filename
Alt + . - Yank last argument to previous command
Alt + b - Move backward
Alt + c - Capitalize the word
Alt + d - Delete word
Alt + f - Move forward
Alt + l - Make word lowercase
Alt + n - Search the history forwards non-incremental
Alt + p - Search the history backwards non-incremental
Alt + r - Recall command
Alt + t - Move words around
Alt + u - Make word uppercase
Alt + back-space - Delete backward from cursor
// SecureCRT 如果没有配置好，这个就很管用了。</pre>
<h4>其他特定的键绑定:</h4>
<p>输入 bind -P 可以查看所有的键盘绑定。这一系列我觉得更为实用。</p>
<pre>Here "2T" means Press TAB twice
$ 2T - All available commands(common) //命令行补全，我认为是 Bash 最好用的一点
$ (string)2T - All available commands starting with (string)
$ /2T - Entire directory structure including Hidden one
$ ./2T - Only Sub Dirs inside including Hidden one
$ *2T - Only Sub Dirs inside without Hidden one
$ ~2T - All Present Users on system from "/etc/passwd" //第一次见到，很好用
$ $2T - All Sys variables //写Shell脚本的时候很实用
$ @2T - Entries from "/etc/hosts"  //第一次见到
$ =2T - Output like ls or dir //好像还不如 ls 快捷
补充:
Esc + T - 交换光标前面的两个单词</pre>
<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-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><li><a href="http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html" title="Bash的输入输出重定向">Bash的输入输出重定向</a></li></ul><hr />
<p><small>© xiao H for <a href="http://www.linuxbyte.org">LinuxByte</a>, 2010<br />
Source: <a href="http://www.linuxbyte.org/bash-shell-kuai-jie-jian.html">Bash Shell 快捷键</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/bash" rel="tag">Bash</a>, <a href="http://www.linuxbyte.org/tag/%e5%bf%ab%e6%8d%b7%e9%94%ae" 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/%e5%bf%ab%e6%8d%b7%e9%94%ae" 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-shell-kuai-jie-jian.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>dell 的 Remove Operating System 代码</title>
		<link>http://www.linuxbyte.org/bash-remove-operating-system.html</link>
		<comments>http://www.linuxbyte.org/bash-remove-operating-system.html#comments</comments>
		<pubDate>Thu, 01 Jan 2009 10:21:40 +0000</pubDate>
		<dc:creator>xiao H</dc:creator>
				<category><![CDATA[shell]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[dell]]></category>

		<guid isPermaLink="false">http://www.linuxbyte.org/?p=347</guid>
		<description><![CDATA[在公社看到 WeiMingzhi 贴的Dell 预装SLED10机子上提供的删除OS 脚本，觉得蛮好特留一个备份。 title Remove Operating System root &#40;hd0,2&#41; kernel /vmlinuz root=/dev/disk/by-label/root ro init=/sbin/remove-os.sh resume=/dev... ]]></description>
			<content:encoded><![CDATA[<p>在公社看到 WeiMingzhi 贴的Dell 预装SLED10机子上提供的删除OS 脚本，觉得蛮好特留一个备份。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">title Remove Operating System
    root <span style="color: #7a0874; font-weight: bold;">&#40;</span>hd0,<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    kernel <span style="color: #000000; font-weight: bold;">/</span>vmlinuz <span style="color: #007800;">root</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>disk<span style="color: #000000; font-weight: bold;">/</span>by-label<span style="color: #000000; font-weight: bold;">/</span>root ro <span style="color: #007800;">init</span>=<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>remove-os.sh <span style="color: #007800;">resume</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda5 <span style="color: #007800;">splash</span>=silent showopts
    initrd <span style="color: #000000; font-weight: bold;">/</span>initrd</pre></div></div>

<p><span id="more-347"></span><br />
<strong>remove-os.sh </strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">dell:<span style="color: #000000; font-weight: bold;">/</span>sbin <span style="color: #666666; font-style: italic;"># cat remove-os.sh</span>
<span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
abort<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;OS removal ABORTED. System will reboot in 30 seconds.&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;NO CHANGES HAVE BEEN MADE TO THE HARD DISKS.&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">30</span>
    <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>reboot
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Initializing keyboard...&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PREVLEVEL</span>=N
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">RUNLEVEL</span>=S
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>earlykbd start
&nbsp;
<span style="color: #007800;">CORRECT_ANSWER</span>=<span style="color: #ff0000;">&quot;REMOVE OPERATING SYSTEM&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'WARNING!    WARNING!   WARNING!'</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;This operation will remove the Operating System (OS) from your computer&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;All personal files on this system will be lost.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;The information will not be recoverable.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ALL DATA ON YOUR HARD DRIVES WILL BE PERMANENTLY AND IRRETRIEVABLY DESTROYED IF YOU CONTINUE.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;To proceed with the Operating System removal, please type: <span style="color: #007800;">$CORRECT_ANSWER</span>&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #660033;">-p</span>      <span style="color: #ff0000;">&quot;Type any other text to abort: &quot;</span> answer
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$answer</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CORRECT_ANSWER</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    abort
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Hard Drive erasure begins in 10 seconds. Power off system to abort (last chance)...&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">10</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Remove all parts save N1</span>
<span style="color: #007800;">DRIVE</span>=<span style="color: #000000; font-weight: bold;">`</span>fdisk <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-m1</span> <span style="color: #ff0000;">&quot;Disk&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">':'</span> -f1<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;DRIVE = &quot;</span><span style="color: #007800;">$DRIVE</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$DRIVE</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #7a0874; font-weight: bold;">break</span>; <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;0,0,0,-&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> sfdisk <span style="color: #660033;">-uS</span> <span style="color: #660033;">-N2</span> <span style="color: #660033;">--force</span> <span style="color: #660033;">--no-reread</span> <span style="color: #007800;">$DRIVE</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;0,0,0,-&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> sfdisk <span style="color: #660033;">-uS</span> <span style="color: #660033;">-N3</span> <span style="color: #660033;">--force</span> <span style="color: #660033;">--no-reread</span> <span style="color: #007800;">$DRIVE</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;0,0,0,-&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> sfdisk <span style="color: #660033;">-uS</span> <span style="color: #660033;">-N4</span> <span style="color: #660033;">--force</span> <span style="color: #660033;">--no-reread</span> <span style="color: #007800;">$DRIVE</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Operating System has been removed. The system is now unbootable. You must reinstall an operating system for the computer.&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">600000</span></pre></div></div>

<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-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><li><a href="http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html" title="Bash的输入输出重定向">Bash的输入输出重定向</a></li></ul><hr />
<p><small>© xiao H for <a href="http://www.linuxbyte.org">LinuxByte</a>, 2009<br />
Source: <a href="http://www.linuxbyte.org/bash-remove-operating-system.html">dell 的 Remove Operating System 代码</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/bash" rel="tag">Bash</a>, <a href="http://www.linuxbyte.org/tag/dell" rel="tag">dell</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/dell" title="dell" rel="tag">dell</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-remove-operating-system.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>我常用的小shell</title>
		<link>http://www.linuxbyte.org/bash-tips.html</link>
		<comments>http://www.linuxbyte.org/bash-tips.html#comments</comments>
		<pubDate>Fri, 04 Jul 2008 09:41:56 +0000</pubDate>
		<dc:creator>xiao H</dc:creator>
				<category><![CDATA[shell]]></category>
		<category><![CDATA[系统管理]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://www.linuxbyte.org/?p=173</guid>
		<description><![CDATA[下面是我平常用的一些bash shell 组合，方便我的日常管理。 清除所有arp 缓存 arp -n&#124;awk '/^[1-9]/ {print &#34;arp -d &#34;$1}'&#124;sh 显示打开连接数前10个机子的IP 和连接数 cat /proc/net/ip_conntrack &#124; cu... ]]></description>
			<content:encoded><![CDATA[<p>下面是我平常用的一些bash shell 组合，方便我的日常管理。<br />
<strong>清除所有arp 缓存</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">arp <span style="color: #660033;">-n</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'/^[1-9]/ {print &quot;arp -d &quot;$1}'</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">sh</span></pre></div></div>

<p><strong>显示打开连接数前10个机子的IP 和连接数</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>net<span style="color: #000000; font-weight: bold;">/</span>ip_conntrack <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">' '</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">10</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">'='</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">uniq</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-nr</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">10</span></pre></div></div>

<p><strong>绑定已知机子的arp 地址</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>net<span style="color: #000000; font-weight: bold;">/</span>arp <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1 &quot; &quot; $4}'</span> <span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">sort</span> -t. <span style="color: #660033;">-n</span> +<span style="color: #000000;">3</span> <span style="color: #660033;">-4</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ethers</pre></div></div>

<p><strong>批量重命名</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{printf&quot;mv %s file_%06d\n&quot;,$0,i++}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sh</span></pre></div></div>

<p><strong>Per脚本实现eth0挂起后自动重启</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #0000ff;">$fail_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$cmd_result</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$cmd_result</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">`ping -w 4 61.130.107.193 |grep packet`</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cmd_result</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/100% packet loss/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #0000ff;">$fail_count</span><span style="color: #339933;">++;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$fail_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #ff0000;">`ifdown eth0`</span><span style="color: #339933;">;</span>
<span style="color: #ff0000;">`ifup eth0`</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #0000ff;">$fail_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>自动从apnic记录中分离出网通，电信IP 段</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">FILE</span>=<span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>ip.txt
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$FILE</span>
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>ftp.apnic.net<span style="color: #000000; font-weight: bold;">/</span>apnic<span style="color: #000000; font-weight: bold;">/</span>stats<span style="color: #000000; font-weight: bold;">/</span>apnic<span style="color: #000000; font-weight: bold;">/</span>delegated-apnic-latest <span style="color: #660033;">-O</span> <span style="color: #007800;">$FILE</span>
<span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">'apnic|CN|ipv4|'</span> <span style="color: #007800;">$FILE</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">4</span>,<span style="color: #000000;">5</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">'|'</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/|/ /g'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> ip cnt
<span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$ip</span>:<span style="color: #007800;">$cnt</span>
<span style="color: #007800;">mask</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> EOF <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bc</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-1</span>
<span style="color: #007800;">pow</span>=<span style="color: #000000;">32</span>;
define log2<span style="color: #7a0874; font-weight: bold;">&#40;</span>x<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>x<span style="color: #000000; font-weight: bold;">&lt;</span>=<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>pow<span style="color: #7a0874; font-weight: bold;">&#41;</span>;
pow--;
<span style="color: #7a0874; font-weight: bold;">return</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>log2<span style="color: #7a0874; font-weight: bold;">&#40;</span>x<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
log2<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$cnt</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
EOF<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$ip</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$mask</span><span style="color: #000000; font-weight: bold;">&gt;&gt;</span> cn.net
<span style="color: #007800;">NETNAME</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">whois</span> <span style="color: #007800;">$ip</span><span style="color: #000000; font-weight: bold;">@</span>whois.apnic.net <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'/./{H;$!d;}'</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'x;/netnum/!d'</span> <span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> ^netname <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/.*:      \(.*\)/\1/g'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/-.*//g'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$NETNAME</span> <span style="color: #000000; font-weight: bold;">in</span>
CNC<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$ip</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$mask</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> CNCGROUP
<span style="color: #000000; font-weight: bold;">;;</span>
CHINANET<span style="color: #000000; font-weight: bold;">|</span>CNCGROUP<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$ip</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$mask</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$NETNAME</span>
<span style="color: #000000; font-weight: bold;">;;</span>
CHINANET<span style="color: #000000; font-weight: bold;">|</span>CNCGROUP<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$ip</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$mask</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$NETNAME</span>
<span style="color: #000000; font-weight: bold;">;;</span>
CHINATELECOM<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$ip</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$mask</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> CHINANET
<span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$ip</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$mask</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> OTHER
<span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><strong>去掉文件中的注释和空格</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> filename <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">'^$'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">'^#'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> newfile</pre></div></div>

<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/fun-bash-shell.html" title="几个有用的bash shell组合">几个有用的bash shell组合</a></li><li><a href="http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html" title="Bash的输入输出重定向">Bash的输入输出重定向</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-tips.html">我常用的小shell</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/bash" rel="tag">Bash</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></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-tips.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>几个有用的bash shell组合</title>
		<link>http://www.linuxbyte.org/fun-bash-shell.html</link>
		<comments>http://www.linuxbyte.org/fun-bash-shell.html#comments</comments>
		<pubDate>Fri, 07 Mar 2008 14:29:59 +0000</pubDate>
		<dc:creator>xiao H</dc:creator>
				<category><![CDATA[shell]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://www.linuxbyte.org/%e5%87%a0%e4%b8%aa%e6%9c%89%e7%94%a8%e7%9a%84bash-shell%e7%bb%84%e5%90%88</guid>
		<description><![CDATA[ps aux &#124; awk '{ if ($3 &#38;gt; 8 ) print $2}' 这个是列出cpu 占有率高于 8% 的进程的PID cat /proc/net/arp &#124; awk '{print $4 $1}' &#38;gt;&#38;gt;/etc/ethers 读取当前机器arp 表并把得到的mac 地址和相应的ip 地址添... ]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ if ($3 &amp;gt; 8 ) print $2}'</span></pre></div></div>

<p>这个是列出cpu 占有率高于 8% 的进程的PID</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>net<span style="color: #000000; font-weight: bold;">/</span>arp <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $4 $1}'</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ethers</pre></div></div>

<p>读取当前机器arp 表并把得到的mac 地址和相应的ip 地址添加到/etc/ethers 文件中去</p>
<p><span id="more-50"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">dd</span> <span style="color: #007800;">if</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>hda1 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">gzip</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; mydata-hda1.gz</pre></div></div>

<p>读取hda1 分区内容并将其打包压缩为mydata-hda1.gz</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">gzip</span> <span style="color: #660033;">-dc</span> mydata-hda1.gz <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">dd</span> ō<span style="color: #007800;">f</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>hda1</pre></div></div>

<p>将压缩文件恢复写回hda1 分区<br />
这两个是一组备份和恢复命令，是直接读取写入分区的。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">:<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> :<span style="color: #000000; font-weight: bold;">|</span>:<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #7a0874; font-weight: bold;">&#125;</span>; :   </pre></div></div>

<p> <br />
这个是用来让系统死机的（不信的话可以用root 试试 _-!）分两部分，第一个部分是定义函数，第二个部分是调用这个函数<br />
:()定义函数，函数名:<br />
{ :|:&amp; }函数的主体<br />
;两个命令的分割<br />
:调用函数</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/history-ming-ling-jing-dian-yong-fa-15li.html" title="History 命令经典用法15例">History 命令经典用法15例</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/unixshellbian-cheng-di-san-ban.html" title="Unix.Shell编程(第三版)">Unix.Shell编程(第三版)</a></li><li><a href="http://www.linuxbyte.org/yong-shc-jiami-shell.html" title="用shc 加密shell">用shc 加密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/shel-fang-zhi-wu-cao-zuo.html" title="用 !$ 防止误操作">用 !$ 防止误操作</a></li><li><a href="http://www.linuxbyte.org/fun-unix-shell.html" title="一些奇怪的unix指令名字的由来">一些奇怪的unix指令名字的由来</a></li><li><a href="http://www.linuxbyte.org/bash-tips.html" title="我常用的小shell">我常用的小shell</a></li><li><a href="http://www.linuxbyte.org/bash-quick-reference-bash-redirection.html" title="Bash的输入输出重定向">Bash的输入输出重定向</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/fun-bash-shell.html">几个有用的bash shell组合</a><br />
Post tags: <a href="http://www.linuxbyte.org/tag/bash" rel="tag">Bash</a>, <a href="http://www.linuxbyte.org/tag/shell" rel="tag">shell</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/shell" title="shell" rel="tag">shell</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/fun-bash-shell.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

