<?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>极限手指</title>
	<atom:link href="http://ahei.info/feed" rel="self" type="application/rss+xml" />
	<link>http://ahei.info</link>
	<description>没有我做不到的，只有你想不到的</description>
	<lastBuildDate>Sat, 22 Sep 2012 12:45:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>轻量标记语言</title>
		<link>http://ahei.info/lml.htm</link>
		<comments>http://ahei.info/lml.htm#comments</comments>
		<pubDate>Sat, 22 Sep 2012 12:45:29 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[技术杂记]]></category>

		<guid isPermaLink="false">http://ahei.info/?p=40863</guid>
		<description><![CDATA[1 什么是 2 能做啥 3 为什么要 4 有哪些 4.1 标记语言 4.2 轻量标记语言 4.3 比较 5 reStructuredText 5.1 sphinx 5.2 用Emacs编辑 6 Emacs Org Mode 6.1 文档结构 6.2 富文本编辑 6.3 表格 6.4 GTD 6.5 导出 6.6 MobileOrg 6.7 流程图怎么办 7 参考资料 1 什么是 维基百科定义: A lightweight markup language is a markup language with a simple syntax, [...]]]></description>
			<content:encoded><![CDATA[<div id="table-of-contents">
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 什么是 </a></li>
<li><a href="#sec-2">2 能做啥 </a></li>
<li><a href="#sec-3">3 为什么要 </a></li>
<li><a href="#sec-4">4 有哪些 </a>
<ul>
<li><a href="#sec-4_1">4.1 标记语言 </a></li>
<li><a href="#sec-4_2">4.2 轻量标记语言 </a></li>
<li><a href="#sec-4_3">4.3 比较 </a></li>
</ul>
</li>
<li><a href="#sec-5">5 reStructuredText </a>
<ul>
<li><a href="#sec-5_1">5.1 sphinx </a></li>
<li><a href="#sec-5_2">5.2 用Emacs编辑 </a></li>
</ul>
</li>
<li><a href="#sec-6">6 Emacs Org Mode </a>
<ul>
<li><a href="#sec-6_1">6.1 文档结构 </a></li>
<li><a href="#sec-6_2">6.2 富文本编辑 </a></li>
<li><a href="#sec-6_3">6.3 表格 </a></li>
<li><a href="#sec-6_4">6.4 GTD </a></li>
<li><a href="#sec-6_5">6.5 导出 </a></li>
<li><a href="#sec-6_6">6.6 MobileOrg </a></li>
<li><a href="#sec-6_7">6.7 流程图怎么办 </a></li>
</ul>
</li>
<li><a href="#sec-7">7 参考资料 </a></li>
</ul>
</div>
</div>
<p><span id="more-40863"></span></p>
<div id="outline-container-1" class="outline-3">
<h3 id="sec-1"><span class="section-number-3">1</span> 什么是 </h3>
<div class="outline-text-3" id="text-1">
<p>维基百科定义:
</p>
<pre class="example">
A lightweight markup language is a markup language with a simple syntax, designed to be easy for a human to enter with a simple <a href="http://ahei.info/t/text" class="st_tag internal_tag" rel="tag" title="标签 text 下的日志">text</a> editor, and easy to read in its raw form.
</pre>
<ul>
<li>
首先轻量标记语言是标记语言, 标记语言是啥? 看看HTML(Hypertext Markup Language)就知道了.
</li>
<li>
其次易读易写, 使用世界上最简单的编辑器记事本就能搞定.
</li>
</ul>
</div>
</div>
<div id="outline-container-2" class="outline-3">
<h3 id="sec-2"><span class="section-number-3">2</span> 能做啥 </h3>
<div class="outline-text-3" id="text-2">
<p>用户只要用一些简单的语法, 就能以一种所见即所得的方式, 获得一份漂亮的文档, 这中文档能转换成各种各样的常见格式, 比如html、pdf、latex、docbook等, 主要用于:
</p>
<ul>
<li>
书写文档
</li>
<li>
内容发布 <br/><br />
比如github大量的把GFM(github flavored markdown)用于代码提交说明、提交评注、问题描述、项目的README文件、维基页面、用户主页和项目主页上, 还比如各种wiki也是轻量标记语言,</p>
<ul>
<li>
<a href="https://github.com/haxney/ezbl/">github支持org</a>
</li>
<li>
<a href="https://github.com/textmate/textmate">github支持markdown</a>
</li>
<li>
<a href="https://github.com/bigmlcom/python">github支持rst</a>
</li>
</ul>
</li>
<li>
个人主页开发 <br/></p>
<ul>
<li>
<a href="http://www.worldhello.net/gotgithub/">GotGitHub</a>
</li>
<li>
<a href="http://book.42qu.com/">42区文档中心</a>
</li>
<li>
<a href="http://emacser.com/evernote-mode.htm">Emacs中文网</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-3" class="outline-3">
<h3 id="sec-3"><span class="section-number-3">3</span> 为什么要 </h3>
<div class="outline-text-3" id="text-3">
<p>看完上面的能做啥后, 我们很容易就想到我们为什么需要轻量标记语言, 而不去使用那些字处理器了
</p>
<ol>
<li>
语法简单, 易写易读, 我们用任何的编辑器都可以编辑
</li>
<li>
格式开放, 能转换成其他各种常用的格式
</li>
<li>
文本文件, 相对于字处理器产生的二进制文件, 易于diff, 这样就可以用版本控制系统来管理
</li>
<li>
在内容发布网站发布简单的内容时, 轻量标记语言更合适
</li>
</ol>
</div>
</div>
<div id="outline-container-4" class="outline-3">
<h3 id="sec-4"><span class="section-number-3">4</span> 有哪些 </h3>
<div class="outline-text-3" id="text-4">
</div>
<div id="outline-container-4_1" class="outline-4">
<h4 id="sec-4_1"><span class="section-number-4">4.1</span> 标记语言 </h4>
<div class="outline-text-4" id="text-4_1">
<ul>
<li>
1971, troff, Bell Labs
</li>
<li>
1978, TeX, Donald Knuth
</li>
<li>
1980初期, LaTeX, Leslie Lamport
</li>
<li>
1982, PostScript, John Warnock和Charles Geschke（Adobe Systems）
</li>
<li>
1986, SGML, ISO 8879:19861
</li>
<li>
1991, HTML, Tim Berners-Lee
</li>
<li>
1991, DocBook, HAL Computer Systems和O’Reilly &amp; Associates
</li>
<li>
1998, XML 1.0, W3C
</li>
</ul>
</div>
</div>
<div id="outline-container-4_2" class="outline-4">
<h4 id="sec-4_2"><span class="section-number-4">4.2</span> 轻量标记语言 </h4>
<div class="outline-text-4" id="text-4_2">
<ul>
<li>
1998, BBCode, Ultimate Bulletin Board
</li>
<li>
2001, txt2tags, txt2tags team
</li>
<li>
2002, reStructuredText, David Goodger
</li>
<li>
2002, AsciiDoc, Stuart Rackham
</li>
<li>
2004, Markdown, John Gruber
</li>
</ul>
</div>
</div>
<div id="outline-container-4_3" class="outline-4">
<h4 id="sec-4_3"><span class="section-number-4">4.3</span> 比较 </h4>
<div class="outline-text-4" id="text-4_3">
<ul>
<li>
<a href="http://en.wikipedia.org/wiki/Lightweight_markup_language">维基百科: 轻量标记语言</a>
</li>
<li>
<a href="http://www.worldhello.net/gotgithub/appendix/markups.html">附录：轻量级标记语言</a>
</li>
<li>
<a href="http://bigclean.is-programmer.com/posts/19602.html">轻量标记语言的比较</a>
</li>
<li>
<a href="http://stdio.tumblr.com/lightdoc">轻量级文档写作</a></p>
</li>
</ul>
<p>几大流行语言比较:
</p>
<ul>
<li>
Markdown <br/></p>
<ul>
<li>
优点 <br/><br />
非常流行, 有一些大型用户, 比如Stack Overflow、Reddit、GitHub、Posterous、Tumblr（这两个已被GFW认证）. github有其自己的markdown实现<a href="http://github.github.com/github-flavored-markdown/">GitHub Flavored Markdown</a>. 虽然这个语言很简单，但也有人用这个语言写了一本书：<a href="http://progit.org/2009/07/28/the-gory-details.html">《Pro Git》</a>.
</li>
<li>
缺点 <br/><br />
标准的Markdown语法缺乏如表格等关键特性的支持，虽然不同的解析器都对其语法进行了扩展，但实现各有不同，造成一定的混乱。
</li>
</ul>
</li>
<li>
reStructuredText <br/><br />
是python的docutils的组成部分, python的官方文档工具. pythoner的最爱. 插上sphinx的翅膀之后, 建站非常方便. 功能比较强大.
</li>
<li>
<a href="http://ahei.info/c-emacs.htm" class="st_tag internal_tag" rel="tag" title="标签 Emacs 下的日志">Emacs</a> Org Mode <br/><br />
背靠着Emacs这颗大树, 它成为所有轻量标记语言中功能最为强大的语言, 它的功能主要包括文档发布和<a href="http://zh.wikipedia.org/wiki/GTD">GTD管理</a>. Org相对其他轻量标记语言语法更简单. Emacser最爱用Org来写文档. <a href="https://github.com/hsitz/VimOrganizer">vi现在也有Org的插件了</a>.
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-5" class="outline-3">
<h3 id="sec-5"><span class="section-number-3">5</span> reStructuredText </h3>
<div class="outline-text-3" id="text-5">
</div>
<div id="outline-container-5_1" class="outline-4">
<h4 id="sec-5_1"><span class="section-number-4">5.1</span> sphinx </h4>
<div class="outline-text-4" id="text-5_1">
<pre class="example">
mkdir note
cd note
sphinx-quickstart
make html
</pre>
</div>
</div>
<div id="outline-container-5_2" class="outline-4">
<h4 id="sec-5_2"><span class="section-number-4">5.2</span> 用Emacs编辑 </h4>
<div class="outline-text-4" id="text-5_2">
<p><a href="http://docutils.sourceforge.net/docs/user/emacs.html">Emacs插件</a>
</p>
</div>
</div>
</div>
<div id="outline-container-6" class="outline-3">
<h3 id="sec-6"><span class="section-number-3">6</span> Emacs Org Mode </h3>
<div class="outline-text-3" id="text-6">
</div>
<div id="outline-container-6_1" class="outline-4">
<h4 id="sec-6_1"><span class="section-number-4">6.1</span> 文档结构 </h4>
<div class="outline-text-4" id="text-6_1">
<pre class="example">
*
**
***
</pre>
<p>大纲图是别的轻量标记语言所欠缺的.
</p>
</div>
</div>
<div id="outline-container-6_2" class="outline-4">
<h4 id="sec-6_2"><span class="section-number-4">6.2</span> 富文本编辑 </h4>
<div class="outline-text-4" id="text-6_2">
<ul>
<li>
引用 <br/></p>
<pre class="example">
: xxxx
</pre>
</li>
<li>
粗体/斜体/下划线</p>
<pre class="example">
*bold*, /italic/, _underlined_, +delete+
</pre>
</li>
<li>
图片 <br/></p>
<pre class="example">
[[xxx.jpg]]
</pre>
</li>
<li>
代码 <br/></p>
<pre class="example">
#+BEGIN_SRC lua
LINK_ACTIONS = {["/artist/.*"] = processSongList}

function processSongList()
   enter("songlist")

   leave()

   extract([[&lt;div class="listen_singerintro_infroB"&gt;(?&lt;singerInfo&gt;.*?)&lt;/div&gt;]])
end
#+END_SRC
</pre>
<p>
效果:
</p>
<pre class="src src-lua">LINK_ACTIONS = <span style="color: #00ff00;">{</span><span style="color: #ffff00;">[</span><span style="color: #ff00ff;">"/artist/.*"</span><span style="color: #ffff00;">]</span> = processSongList<span style="color: #00ff00;">}</span>

<span style="color: #00ffff;">function</span> <span style="color: #ffff00; background-color: #45D463DD4FF8;">processSongList</span><span style="color: #6495ed;">()</span>
   enter<span style="color: #6495ed;">(</span><span style="color: #ff00ff;">"songlist"</span><span style="color: #6495ed;">)</span>

   leave<span style="color: #6495ed;">()</span>

   extract<span style="color: #6495ed;">(</span><span style="color: #ff00ff;">[[&lt;div class="listen_singerintro_infroB"&gt;(?&lt;singerInfo&gt;.*?)&lt;/div&gt;]]</span><span style="color: #6495ed;">)</span>
<span style="color: #00ffff;">end</span>
</pre>
</li>
<li>
内嵌html <br/></p>
<pre class="example">
#+BEGIN_HTML
xxx
#+END_HTML
</pre>
</li>
</ul>
</div>
</div>
<div id="outline-container-6_3" class="outline-4">
<h4 id="sec-6_3"><span class="section-number-4">6.3</span> 表格 </h4>
<div class="outline-text-4" id="text-6_3">
<table  border="1" cellspacing="0" cellpadding="6" rules="all" frame="all">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">命令</th>
<th scope="col" class="left">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">M-x org-table-create-or-convert-from-region</td>
<td class="left">创建表格</td>
</tr>
<tr>
<td class="left">M-&lt;left&gt;/M-&lt;right&gt;</td>
<td class="left">左右移动列</td>
</tr>
<tr>
<td class="left">M-&lt;up&gt;/M-&lt;down&gt;</td>
<td class="left">上下移动行</td>
</tr>
<tr>
<td class="left">C-c +</td>
<td class="left">累加列值</td>
</tr>
<tr>
<td class="left">C-c C-c</td>
<td class="left">调整表格</td>
</tr>
<tr>
<td class="left">&lt;digit&gt;</td>
<td class="left">设置列最大长度</td>
</tr>
<tr>
<td class="left">C-c `</td>
<td class="left">编辑列</td>
</tr>
<tr>
<td class="left">C-c ^</td>
<td class="left">对列排序</td>
</tr>
</tbody>
</table>
<p>
自动计算:
</p>
<table  border="1" cellspacing="0" cellpadding="6" rules="all" frame="all">
<caption></caption>
<colgroup>
<col class="right" /></colgroup>
<colgroup>
<col class="right" />
<col class="right" />
<col class="right" /></colgroup>
<colgroup>
<col class="right" />
</colgroup>
<thead>
<tr>
<th scope="col" class="right">N</th>
<th scope="col" class="right">N^2</th>
<th scope="col" class="right">N^3</th>
<th scope="col" class="right">N^4</th>
<th scope="col" class="right">sqrt(n)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="right">1</td>
<td class="right">1</td>
<td class="right">1</td>
<td class="right">1</td>
<td class="right">1</td>
</tr>
<tr>
<td class="right">2</td>
<td class="right">4</td>
<td class="right">8</td>
<td class="right">16</td>
<td class="right">1.4142</td>
</tr>
<tr>
<td class="right">3</td>
<td class="right">9</td>
<td class="right">27</td>
<td class="right">81</td>
<td class="right">1.7320508</td>
</tr>
<tr>
<td class="right">5</td>
<td class="right">25</td>
<td class="right">125</td>
<td class="right">625</td>
<td class="right">2.2360680</td>
</tr>
</tbody>
</table>
<p>
公式计算:
</p>
<table  border="1" cellspacing="0" cellpadding="6" rules="all" frame="all">
<caption></caption>
<colgroup>
<col class="left" />
<col class="right" />
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">Func</th>
<th scope="col" class="right">n</th>
<th scope="col" class="left">x</th>
<th scope="col" class="left">Result</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">exp(x)</td>
<td class="right">1</td>
<td class="left">x</td>
<td class="left">1 + x</td>
</tr>
<tr>
<td class="left">exp(x)</td>
<td class="right">2</td>
<td class="left">x</td>
<td class="left">1 + x + x^2 / 2</td>
</tr>
<tr>
<td class="left">exp(x)</td>
<td class="right">3</td>
<td class="left">x</td>
<td class="left">1 + x + x^2 / 2 + x^3 / 6</td>
</tr>
<tr>
<td class="left">x^2+sqrt(x)</td>
<td class="right">2</td>
<td class="left">x=0</td>
<td class="left">x*(0.5 / 0) + x^2 (2 &#8211; 0.25 / 0) / 2</td>
</tr>
<tr>
<td class="left">x^2+sqrt(x)</td>
<td class="right">2</td>
<td class="left">x=1</td>
<td class="left">2 + 2.5 x &#8211; 2.5 + 0.875 (x &#8211; 1)^2</td>
</tr>
<tr>
<td class="left">tan(x)</td>
<td class="right">3</td>
<td class="left">x</td>
<td class="left">0.0175 x + 1.77e-6 x^3</td>
</tr>
</tbody>
</table>
<ul>
<li id="sec-6_3_1">复杂的表格 <br/><br />
<table  border="1" cellspacing="0" cellpadding="6" rules="all" frame="all">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">命令</th>
<th scope="col" class="left">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">M-x table-insert</td>
<td class="left">创建复杂的表格</td>
</tr>
<tr>
<td class="left">M-x table-split-cell-vertically</td>
<td class="left">垂直切分</td>
</tr>
<tr>
<td class="left">C- -</td>
<td class="left">水平切分</td>
</tr>
</tbody>
</table>
<p><!-- This HTML table template is generated by emacs 23.4.1 --></p>
<table border="1">
<tr>
<td colspan="2" align="left" valign="top">
      &nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
<td align="center" valign="top">
      &nbsp;&nbsp;2&nbsp;&nbsp;
    </td>
<td colspan="2" align="left" valign="top">
      3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
</tr>
<tr>
<td colspan="2" align="left" valign="top">
      &nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
<td align="left" valign="top">
      5&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
<td align="left" valign="top">
      6&nbsp;&nbsp;&nbsp;
    </td>
<td align="left" valign="top">
      7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
</tr>
<tr>
<td align="left" valign="top">
      &nbsp;&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
<td align="left" valign="top">
      &nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
<td align="center" valign="top">
      &nbsp;11&nbsp;&nbsp;
    </td>
<td colspan="2" align="left" valign="top">
      &nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
</tr>
</table>
</li>
</ul>
</div>
</div>
<div id="outline-container-6_4" class="outline-4">
<h4 id="sec-6_4"><span class="section-number-4">6.4</span> GTD </h4>
<div class="outline-text-4" id="text-6_4">
<table  border="1" cellspacing="0" cellpadding="6" rules="all" frame="all">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">命令</th>
<th scope="col" class="left">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">M-S-&lt;RET&gt;</td>
<td class="left">增加TODO</td>
</tr>
<tr>
<td class="left">C-c ,</td>
<td class="left">设置优先级</td>
</tr>
<tr>
<td class="left">[%] or [/]</td>
<td class="left">设置子任务</td>
</tr>
<tr>
<td class="left">C-c !</td>
<td class="left">插入时间戳</td>
</tr>
<tr>
<td class="left">S-&lt;left&gt;/S-&lt;right&gt;</td>
<td class="left">增加/减少一天</td>
</tr>
<tr>
<td class="left">S-&lt;up&gt;/S-&lt;down&gt;</td>
<td class="left">在光标所在日期的单位上增加/减少</td>
</tr>
<tr>
<td class="left">C-c C-d</td>
<td class="left">设置deadline</td>
</tr>
<tr>
<td class="left">C-c C-s</td>
<td class="left">设置任务开始执行的时间</td>
</tr>
<tr>
<td class="left">C-c C-x C-i</td>
<td class="left">对任务开始计时</td>
</tr>
<tr>
<td class="left">C-c C-x C-o</td>
<td class="left">对任务停止计时</td>
</tr>
<tr>
<td class="left">C-c C-x C-d</td>
<td class="left">显示任务的总用时</td>
</tr>
</tbody>
</table>
<p><b>Org中的时间选择器恐怕是世界上用户体验最好的时间选择器!</b></p>
</div>
</div>
<div id="outline-container-6_5" class="outline-4">
<h4 id="sec-6_5"><span class="section-number-4">6.5</span> 导出 </h4>
<div class="outline-text-4" id="text-6_5">
<table  border="1" cellspacing="0" cellpadding="6" rules="all" frame="all">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">命令</th>
<th scope="col" class="left">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">M-x org-export-as-html</td>
<td class="left">导出成html</td>
</tr>
<tr>
<td class="left">M-x org-export-as-ascii</td>
<td class="left">导出成纯文本</td>
</tr>
</tbody>
</table>
<p>nb的以info形式导出: 在文件最开始增加:</p>
<pre class="example">
#+INFOJS_OPT: view:info toc:nil
</pre>
</div>
</div>
<div id="outline-container-6_6" class="outline-4">
<h4 id="sec-6_6"><span class="section-number-4">6.6</span> MobileOrg </h4>
<div class="outline-text-4" id="text-6_6">
<p>当今时代, 各路诸侯争先抢占无线市场, 人家Org 2009年就已经抢占市场了.
</p>
</div>
</div>
<div id="outline-container-6_7" class="outline-4">
<h4 id="sec-6_7"><span class="section-number-4">6.7</span> 流程图怎么办 </h4>
<div class="outline-text-4" id="text-6_7">
<p>见下次分享, <img src='http://ahei.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
</div>
</div>
</div>
<div id="outline-container-7" class="outline-3">
<h3 id="sec-7"><span class="section-number-3">7</span> 参考资料 </h3>
<div class="outline-text-3" id="text-7">
<ul>
<li>
<a href="http://en.wikipedia.org/wiki/Lightweight_markup_language">维基百科: 轻量标记语言</a>
</li>
<li>
<a href="http://www.worldhello.net/gotgithub/appendix/markups.html">附录：轻量级标记语言</a>
</li>
<li>
<a href="http://bigclean.is-programmer.com/posts/19602.html">轻量标记语言的比较</a>
</li>
<li>
<a href="http://stdio.tumblr.com/lightdoc">轻量级文档写作</a>
</li>
<li>
<a href="http://wstudio.web.fc2.com/others/restructuredtext.html">reStructuredText 简明教程</a>
</li>
<li>
<a href="http://book.42qu.com/rst.html">reStructuredText简明教程</a>
</li>
</ul>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/lml.htm/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Linux下文本的高效处理</title>
		<link>http://ahei.info/text-processing.htm</link>
		<comments>http://ahei.info/text-processing.htm#comments</comments>
		<pubDate>Sat, 22 Sep 2012 12:39:58 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://ahei.info/?p=40858</guid>
		<description><![CDATA[1 引言 2 关于输入 3 文本输出 3.1 整个输出 3.2 部分输出 4 文本搜索 4.1 grep (Global Regular Expression Print) 4.2 fgrep 4.3 egrep 4.4 rgrep 4.5 agrep (approximate grep) 4.6 zgrep 4.7 sgrep (structured grep) 4.8 nrgrep (Nondeterministic Reverse grep) 5 文本摘要 5.1 wc (word count) 5.2 md5sum 6 排序去重 6.1 sort 6.2 tsort (topological sort) [...]]]></description>
			<content:encoded><![CDATA[<div id="table-of-contents">
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 引言 </a></li>
<li><a href="#sec-2">2 关于输入 </a></li>
<li><a href="#sec-3">3 文本输出 </a>
<ul>
<li><a href="#sec-3_1">3.1 整个输出 </a></li>
<li><a href="#sec-3_2">3.2 部分输出 </a></li>
</ul>
</li>
<li><a href="#sec-4">4 文本搜索 </a>
<ul>
<li><a href="#sec-4_1">4.1 grep (Global Regular Expression Print) </a></li>
<li><a href="#sec-4_2">4.2 fgrep </a></li>
<li><a href="#sec-4_3">4.3 egrep </a></li>
<li><a href="#sec-4_4">4.4 rgrep </a></li>
<li><a href="#sec-4_5">4.5 agrep (approximate grep) </a></li>
<li><a href="#sec-4_6">4.6 zgrep </a></li>
<li><a href="#sec-4_7">4.7 sgrep (structured grep) </a></li>
<li><a href="#sec-4_8">4.8 nrgrep (Nondeterministic Reverse grep) </a></li>
</ul>
</li>
<li><a href="#sec-5">5 文本摘要 </a>
<ul>
<li><a href="#sec-5_1">5.1 wc (word count) </a></li>
<li><a href="#sec-5_2">5.2 md5sum </a></li>
</ul>
</li>
<li><a href="#sec-6">6 排序去重 </a>
<ul>
<li><a href="#sec-6_1">6.1 sort </a></li>
<li><a href="#sec-6_2">6.2 tsort (topological sort) </a></li>
<li><a href="#sec-6_3">6.3 uniq </a></li>
<li><a href="#sec-6_4">6.4 comm </a></li>
<li><a href="#shuf">6.5 shuf (shuffle) </a></li>
</ul>
</li>
<li><a href="#sec-7">7 操作字段 </a>
<ul>
<li><a href="#sec-7_1">7.1 cut </a></li>
<li><a href="#sec-7_2">7.2 paste </a></li>
<li><a href="#sec-7_3">7.3 join </a></li>
</ul>
</li>
<li><a href="#sec-8">8 操作字符 </a>
<ul>
<li><a href="#sec-8_1">8.1 tr (translate) </a></li>
<li><a href="#sec-8_2">8.2 expand </a></li>
<li><a href="#sec-8_3">8.3 unexpand </a></li>
<li><a href="#sec-8_4">8.4 colrm (COLumn ReMove) </a></li>
</ul>
</li>
<li><a href="#sec-9">9 文本切割 </a>
<ul>
<li><a href="#sec-9_1">9.1 split </a></li>
<li><a href="#sec-9_2">9.2 csplit (context split) </a></li>
</ul>
</li>
<li><a href="#sec-10">10 文本编码 </a>
<ul>
<li><a href="#sec-10_1">10.1 iconv </a></li>
<li><a href="#sec-10_2">10.2 enca </a></li>
</ul>
</li>
<li><a href="#sec-11">11 文本格式化 </a>
<ul>
<li><a href="#sec-11_1">11.1 column </a></li>
<li><a href="#sec-11_2">11.2 fold </a></li>
<li><a href="#sec-11_3">11.3 fmt </a></li>
</ul>
</li>
<li><a href="#sec-12">12 微语言 </a>
<ul>
<li><a href="#sec-12_1">12.1 sed (Stream EDitor) </a></li>
<li><a href="#sec-12_2">12.2 awk (Alfred V. Aho, Peter J. Weinberger, Brian W. Kernighan.) </a></li>
</ul>
</li>
<li><a href="#sec-13">13 语言 </a></li>
<li><a href="#sec-14">14 实例 </a>
<ul>
<li><a href="#sec-14_1">14.1 我的正则会数学 </a></li>
</ul>
</li>
<li><a href="#sec-15">15 参考文献 </a></li>
</ul>
</div>
</div>
<p><span id="more-40858"></span></p>
<div id="outline-container-1" class="outline-3">
<h3 id="sec-1"><span class="section-number-3">1</span> 引言 </h3>
<div class="outline-text-3" id="text-1">
<p>所谓的文本处理是指对文本进行查找、替换、删除、排序等操作, linux在文本处理方面提供了大量优秀的工具, 使得在linux下进行文本处理极其的方便. <br/><br />
我们平常的工作中, 经常会用到文本处理, 比如日志分析, 比如文本抽取, 等等, 所以掌握好文本处理, 将会对我们的工作起到极大的作用. <br/><br />
下面我就来逐个介绍下这些强大的工具, 对于我觉得大家可能比较熟知的工具及用法, 我会略过, 或者粗讲下.
</p>
</div>
</div>
<div id="outline-container-2" class="outline-3">
<h3 id="sec-2"><span class="section-number-3">2</span> 关于输入 </h3>
<div class="outline-text-3" id="text-2">
<p>Linux哲学中, 为了更好的组合各种命令达到更加强大的功能, 大多数文本处理命令的输入既可以是文件, 也可以是标准输入, 如果没有指定输入文件, 则默认从标准输入读数据. 输出都是标准输出, 方面传给管道线的下一个命令, 想要输出到文件的话, 重定向下即可. 下面介绍的这些命令, 如无特殊说明, 则都可以从文件或者标准输入读入数据.
</p>
</div>
</div>
<div id="outline-container-3" class="outline-3">
<h3 id="sec-3"><span class="section-number-3">3</span> 文本输出 </h3>
<div class="outline-text-3" id="text-3">
</div>
<div id="outline-container-3_1" class="outline-4">
<h4 id="sec-3_1"><span class="section-number-4">3.1</span> 整个输出 </h4>
<div class="outline-text-4" id="text-3_1">
<ul>
<li id="sec-3_1_1">echo <br/>
<pre class="example">
输入为命令行参数
</pre>
<p>
非常常用的命令, 主要用作输出字符串. 如果只是为了向管道线的下一个命令传输入的话, 可以使用Here String:
</p>
<pre class="src src-sh"><span style="color: #b0c4de;">echo</span> xxx | md5sum
md5sum <span style="color: #00ffff;">&lt;&lt;&lt;</span> xxx
</pre>
<p>
后者速度上应该会快一点, 不需要经过管道.
</p>
<ul>
<li id="sec-3_1_1_1">-n <br/><br />
不输出换行符(默认输出换行符)
</li>
</ul>
<ul>
<li id="sec-3_1_1_2">-e <br/><br />
解释转义符, 常用的转义符 <b>\t</b>, <b>\n</b>; 这个命令还有个最常用的用途就是输出<a href="http://linux.chinaunix.net/doc/2004-10-05/7.shtml">ANSI颜色</a>:</p>
<pre class="src src-sh"><span style="color: #b0c4de;">echo</span> -e <span style="color: #ff00ff;">'\033[1;31mHello, \033[0m\033[1;33mworld!\033[0m'</span>
</pre>
<p>      <span style="color:red;font-weight:bold;">Hello, </span><span style="color:olive;font-weight:bold;">world!</span></p>
<p>
<a href="http://code.google.com/p/ansi-color/">这个工具</a>可以更方便的输出ANSI颜色.
</p>
</li>
</ul>
<ul>
<li id="sec-3_1_1_3">-E <br/><br />
不解释转义符</p>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-3_1_2">printf <br/>
<pre class="example">
输入为命令行参数
</pre>
<p>
更强大的输出你想要的文本的命令, 类似C里面的printf
</p>
<pre class="src src-sh"><span style="color: #b0c4de;">printf</span> <span style="color: #ff00ff;">'\033[1;31m%s, \033[0m\033[1;33m%d\033[0m and \u4e2d\u6587!\n'</span> <span style="color: #ff00ff;">"Hello"</span> 34
</pre>
<p>     <span style="color:red;font-weight:bold;">Hello, </span><span style="color:olive;font-weight:bold;">34</span> and 中文!</p>
<p>
不过此命令较echo来说, 使用率会低很多, 大多数情况下echo就能搞定了.
</p>
</li>
</ul>
<ul>
<li id="sec-3_1_3">yes [STRING] <br/>
<pre class="example">
输入为命令行参数
</pre>
<p>不停的输出字符串STRING, 默认是y. 这个命令用处比较少, 但会有用, 比如测试<a href="#tail">tail</a>命令.
</p>
</li>
</ul>
<ul>
<li id="sec-3_1_4">cat (concatenate) <br/><br />
此命令最大的用途应该就是显示特殊字符了, 如果你告诉我你经常用它给管道线的下一个命令传输入的话, 那么就太浪费了:</p>
<pre class="src src-sh">cat file | grep xxx
grep xxx file
</pre>
<p>
前者相比后者多启动了一个进程, 还经过了管道. file很大的话, 性能差距很容易就看出来了.
</p>
<ul>
<li id="sec-3_1_4_1">-E <br/><br />
在行尾显示$, 有时候行尾是空格或者TAB, 看不清行尾是哪里, 加了这个选项就知道了
</li>
</ul>
<ul>
<li id="sec-3_1_4_2">-T <br/><br />
输出的时候用^I代替TAB符号, 这样很容易知道那一坨空白到底谁是TAB了
</li>
</ul>
<ul>
<li id="sec-3_1_4_3">-v <br/><br />
用^和M-表示法输出不可打印字符</p>
<pre class="src src-sh">taoshanwen@taoshanwen-laptop ~$ echo -e <span style="color: #ff00ff;">'\r'</span> | cat -v
^M
</pre>
</li>
</ul>
<ul>
<li id="sec-3_1_4_4">-e <br/><br />
相当于-vE
</li>
</ul>
<ul>
<li id="sec-3_1_4_5">-t <br/><br />
相当于-vT
</li>
</ul>
<ul>
<li id="sec-3_1_4_6">-A <br/><br />
相当于-vET, 一般这个选项用的最多</p>
</li>
</ul>
<ul>
<li id="sec-3_1_4_7">-n <br/><br />
显示行号, 应该有同学感兴趣
</li>
</ul>
<ul>
<li id="sec-3_1_4_8">-b <br/><br />
只对非空行显示行号, 有人感兴趣吗?</p>
</li>
</ul>
<ul>
<li id="sec-3_1_4_9">-s <br/><br />
对连续的空行只输出一个空行, 见过好多同学有这样的需求</p>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-3_1_5">tac <br/><br />
看名字知道了, 它和cat是反的, 倒着输出, 先输出最后一行, 接着是倒数第二行, 最后输出的是第一行</p>
<ul>
<li id="sec-3_1_5_1">-s, &ndash;separator=STRING <br/><br />
设定分隔符, 代替\n分割文本</p>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-3_1_6">rev (reverse) <br/><br />
这个命令和cat也是反的, 不过它不像tac那样, 它输出行的顺序和cat也是一样的, 不过输出每行的时候, 先输出最后一个字符, 接着是倒数第二个字符, 最后才是第一个字符, 下面这个命令可以把输入全部倒过来:</p>
<pre class="src src-sh">tac | rev
</pre>
</li>
</ul>
<ul>
<li id="sec-3_1_7">nl (number lines) <br/><br />
更强大的行号显示工具, 可以控制行号的格式, 宽度. 没有特殊的需求, 用cat -n就够了.</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-3_2" class="outline-4">
<h4 id="sec-3_2"><span class="section-number-4">3.2</span> 部分输出 </h4>
<div class="outline-text-4" id="text-3_2">
<ul>
<li id="sec-3_2_1">head <br/><br />
只显示文本的开头几行, 比如head -2只显示前面2行
</li>
</ul>
<ul>
<li id="sec-3_2_2"><a name="sec-3_2_2" id="sec-3_2_2"></a>tail <br/><br />
这个命令相对head来说, 最常用的用途就是不停的打印文件的最新内容了(tail -f)</p>
<ul>
<li id="sec-3_2_2_1">-n, -K, &ndash;lines=K <br/><br />
显示尾部K行, -n +K显示第K行到文本尾部的所有内容
</li>
</ul>
<ul>
<li id="sec-3_2_2_2">-f, &ndash;follow[={name|descriptor}] <br/><br />
这个选项表示如果文件尾部有新数据追加进来, 也会显示出来. 这个选项可以根据文件名(name)和文件描述符(descriptor)来监视文件是否有更新. 默认是descriptor.
</li>
</ul>
<ul>
<li id="sec-3_2_2_3">&ndash;retry <br/><br />
当文件不可访问时, 进行重试, 这个选项和&ndash;follow=name组合起来比较有用
</li>
</ul>
<ul>
<li id="sec-3_2_2_4">-F <br/><br />
相当于&ndash;follow=name &ndash;retry</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-4" class="outline-3">
<h3 id="sec-4"><span class="section-number-3">4</span> 文本搜索 </h3>
<div class="outline-text-3" id="text-4">
</div>
<div id="outline-container-4_1" class="outline-4">
<h4 id="sec-4_1"><span class="section-number-4">4.1</span> grep (Global Regular Expression Print) </h4>
<div class="outline-text-4" id="text-4_1">
<p>非常常用的命令, 打印文本中匹配模式的行, 下面的选项最好都能掌握.
</p>
<pre class="src src-sh">grep <span style="color: #ffff00;">[</span>OPTIONS<span style="color: #ffff00;">]</span> PATTERN <span style="color: #ffff00;">[</span>FILE...<span style="color: #ffff00;">]</span>
grep <span style="color: #ffff00;">[</span>OPTIONS<span style="color: #ffff00;">]</span> <span style="color: #ffff00;">[</span>-e PATTERN | -f FILE<span style="color: #ffff00;">]</span> <span style="color: #ffff00;">[</span>FILE...<span style="color: #ffff00;">]</span>
</pre>
<ul>
<li id="sec-4_1_1">-E, &ndash;extended-regexp <br/><br />
使用扩展正则表达式(ERE), 默认的是基础正则表达式(BRE), BRE中元字符?, +, {, |, (, )失去特殊意义(你是否遇到grep "a|b"是否不能打印出含有a或者b的行?), 想要表达特殊意义的话, 需要用转义字符进行转义(\), \?, \+等
</li>
</ul>
<ul>
<li id="sec-4_1_2">-F, &ndash;fixed-strings <br/><br />
把pattern当作一个固定的字符串, 不进行正则解析. 当你要搜索非正则的固定字符串时(还可能含有正则元字符), 这个选项会非常有用, 而且由于不需要解析正则, 速度会快些.
</li>
</ul>
<ul>
<li id="sec-4_1_3">-P, &ndash;perl-regexp <br/><br />
把pattern解析为perl的正则(PCRE), 由于perl的正则强大而简洁, 所以可以多尝试着使用这个选项.</p>
</li>
</ul>
<ul>
<li id="sec-4_1_4">-i, &ndash;ignore-case <br/><br />
搜索pattern的时候, 忽略大小写. 如果没有这个选项, 可以使用PCRE, 比如:</p>
<pre class="src src-sh">grep -P <span style="color: #ff00ff;">"(?i)AB"</span>
</pre>
</li>
</ul>
<ul>
<li id="sec-4_1_5">-v, &ndash;invert-match <br/><br />
显示不能匹配pattern的行
</li>
</ul>
<ul>
<li id="sec-4_1_6">-e pattern <br/><br />
如果你想要指定多个搜索pattern, 或者你想要搜索的pattern由减号(-)开头(如果直接grep pattern的话, 会被解析为grep的选项), 就可以用这个选项了. </p>
</li>
</ul>
<ul>
<li id="sec-4_1_7">-f FILE, &ndash;file=FILE <br/><br />
从文件中获取pattern, 每行一个pattern</p>
</li>
</ul>
<ul>
<li id="sec-4_1_8">-x, &ndash;line-regexp <br/><br />
pattern必须要匹配整行, 这个选项等价于 "^pattern$"
</li>
</ul>
<ul>
<li id="sec-4_1_9">-w, &ndash;word-regexp <br/><br />
pattern必须要匹配整个单词, 这个选项等价于 "\bpattern\b"</p>
</li>
</ul>
<ul>
<li id="sec-4_1_10">-c, &ndash;count <br/><br />
不打印匹配的行, 只打印匹配的行数, 等价于grep pattern | wc -l
</li>
</ul>
<ul>
<li id="sec-4_1_11">&ndash;<a href="http://ahei.info/t/color" class="st_tag internal_tag" rel="tag" title="标签 color 下的日志">color</a>[=WHEN], &ndash;colour[=WHEN] <br/><br />
用颜色高亮出匹配的:</p>
<pre>
(require '<span style="color:red;font-weight:bold;">coding</span>-settings)
   (&quot;C-x U&quot;   revert-buffer-with-<span style="color:red;font-weight:bold;">coding</span>-system-no-confirm-sb)))
   (&quot;C-x M-C&quot; set-buffer-file-<span style="color:red;font-weight:bold;">coding</span>-system)))
  (set-buffer-file-<span style="color:red;font-weight:bold;">coding</span>-system 'unix))
  (set-buffer-file-<span style="color:red;font-weight:bold;">coding</span>-system 'dos))
</pre>
</li>
</ul>
<ul>
<li id="sec-4_1_12">-m NUM, &ndash;max-count=NUM <br/><br />
当发现NUM个匹配行后, 停止扫描剩下的文本.
</li>
</ul>
<ul>
<li id="sec-4_1_13">-q, &ndash;quiet, &ndash;silent <br/><br />
不打印任何信息, 发现匹配即退出, 并返回0, 否则返回1. 我们经常只是想查看整个文本里面是否有匹配, 这时候这个选项就非常有用了, 速度会快很多.
</li>
</ul>
<ul>
<li id="sec-4_1_14">-o, &ndash;only-matching <br/><br />
只显示匹配pattern的字符串, 匹配行的其余部分不显示
</li>
</ul>
<ul>
<li id="sec-4_1_15">-n, &ndash;line-number <br/><br />
在匹配行前面打印行号</p>
<p>
有时候想看看匹配行周围都是啥, 下面这几个选项就非常有用了:
</p>
</li>
</ul>
<ul>
<li id="sec-4_1_16">-A NUM, &ndash;after-context=NUM <br/><br />
打印匹配行的后面NUM行
</li>
</ul>
<ul>
<li id="sec-4_1_17">-B NUM, &ndash;before-context=NUM <br/><br />
打印匹配行的前面NUM行
</li>
</ul>
<ul>
<li id="sec-4_1_18">-C NUM, -NUM, &ndash;context=NUM <br/><br />
打印匹配行的周围NUM行</p>
</li>
</ul>
<ul>
<li id="sec-4_1_19">-a, &ndash;<a href="http://ahei.info/t/text" class="st_tag internal_tag" rel="tag" title="标签 text 下的日志">text</a> <br/><br />
有时候文件中含有一些非可打印字符, grep可能会把它识别成二进制文件, 这时候grep只会打印出是否匹配pattern的信息, 并不会打印匹配的每行, 这个选项会强制grep把该文件当文本文件处理</p>
</li>
</ul>
<ul>
<li id="sec-4_1_20">-R, -r, &ndash;recursive <br/><br />
递归处理文件夹下的所有文件</p>
</li>
</ul>
<ul>
<li id="sec-4_1_21">-l, &ndash;files-with-matches <br/><br />
不打印匹配的行, 只打印匹配的文件
</li>
</ul>
<ul>
<li id="sec-4_1_22">-L, &ndash;files-without-match <br/><br />
和-l相反, 不打印匹配的行, 只打印不匹配的文件
</li>
</ul>
<ul>
<li id="sec-4_1_23">-h, &ndash;no-filename <br/><br />
搜索多个文件时, 会在每行前面输出文件名, 如果你不喜欢, 使用此选项吧.</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-4_2" class="outline-4">
<h4 id="sec-4_2"><span class="section-number-4">4.2</span> fgrep </h4>
<div class="outline-text-4" id="text-4_2">
<p>grep -F
</p>
</div>
</div>
<div id="outline-container-4_3" class="outline-4">
<h4 id="sec-4_3"><span class="section-number-4">4.3</span> egrep </h4>
<div class="outline-text-4" id="text-4_3">
<p>grep -E
</p>
</div>
</div>
<div id="outline-container-4_4" class="outline-4">
<h4 id="sec-4_4"><span class="section-number-4">4.4</span> rgrep </h4>
<div class="outline-text-4" id="text-4_4">
<p>grep -r
</p>
</div>
</div>
<div id="outline-container-4_5" class="outline-4">
<h4 id="sec-4_5"><span class="section-number-4">4.5</span> agrep (approximate grep) </h4>
<div class="outline-text-4" id="text-4_5">
<p>grep的模糊匹配版本
</p>
</div>
</div>
<div id="outline-container-4_6" class="outline-4">
<h4 id="sec-4_6"><span class="section-number-4">4.6</span> zgrep </h4>
<div class="outline-text-4" id="text-4_6">
<p>对压缩文件进行grep, 接受的选项和grep完全一样
</p>
</div>
</div>
<div id="outline-container-4_7" class="outline-4">
<h4 id="sec-4_7"><span class="section-number-4">4.7</span> sgrep (structured grep) </h4>
<div class="outline-text-4" id="text-4_7">
<p>对结构化的文本, 如SGML、XML、HTML进行搜索、抽取, 功能非常强大
</p>
</div>
</div>
<div id="outline-container-4_8" class="outline-4">
<h4 id="sec-4_8"><span class="section-number-4">4.8</span> nrgrep (Nondeterministic Reverse grep) </h4>
<div class="outline-text-4" id="text-4_8">
<p>类似agrep
</p>
</div>
</div>
</div>
<div id="outline-container-5" class="outline-3">
<h3 id="sec-5"><span class="section-number-3">5</span> 文本摘要 </h3>
<div class="outline-text-3" id="text-5">
</div>
<div id="outline-container-5_1" class="outline-4">
<h4 id="sec-5_1"><span class="section-number-4">5.1</span> wc (word count) </h4>
<div class="outline-text-4" id="text-5_1">
<p>最主要的用途就是统计行数
</p>
<ul>
<li id="sec-5_1_1">-l, &ndash;lines <br/><br />
最常用的选项, 统计行数
</li>
</ul>
<ul>
<li id="sec-5_1_2">-L, &ndash;max-line-length <br/><br />
输出文本最长行的长度
</li>
</ul>
<ul>
<li id="sec-5_1_3">-w, &ndash;words <br/><br />
输出单词数
</li>
</ul>
<ul>
<li id="sec-5_1_4">-m, &ndash;chars <br/><br />
输出字符数
</li>
</ul>
<ul>
<li id="sec-5_1_5">-c, &ndash;bytes <br/><br />
输出字节数</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-5_2" class="outline-4">
<h4 id="sec-5_2"><span class="section-number-4">5.2</span> md5sum </h4>
<div class="outline-text-4" id="text-5_2">
<p>打印文本的md5, 主要用作文件校验, 防止文件传输时发生错误或者被篡改. -c选项检查md5是否正确
</p>
</div>
</div>
</div>
<div id="outline-container-6" class="outline-3">
<h3 id="sec-6"><span class="section-number-3">6</span> 排序去重 </h3>
<div class="outline-text-3" id="text-6">
</div>
<div id="outline-container-6_1" class="outline-4">
<h4 id="sec-6_1"><span class="section-number-4">6.1</span> sort </h4>
<div class="outline-text-4" id="text-6_1">
<p>非常常用的命令, 啥序都能排
</p>
<ul>
<li id="sec-6_1_1">-r, &ndash;reverse <br/><br />
逆序排序, 默认是按从小到大排, -r后就从大到小了</p>
</li>
</ul>
<ul>
<li id="sec-6_1_2">-c, &ndash;check, &ndash;check=diagnose-first <br/><br />
检查输入文件是否是有序的, 不是的话, 会打印哪行开始不是有序的
</li>
</ul>
<ul>
<li id="sec-6_1_3">-C, &ndash;check=quiet, &ndash;check=silent <br/><br />
类似-c, 但是不打印错误信息, 只返回错误码1</p>
</li>
</ul>
<ul>
<li id="sec-6_1_4">-k, &ndash;key=POS1[,POS2] <br/><br />
这个应该是sort最nb的地方了, 可以精确控制要排序的对象. POS具备这样的形式:</p>
<pre class="example">
F[.C][OPTS]
</pre>
<p>其中, F是字段号, C是字符号, OPTS是排序选项, 可以每个字段排序的规则不一样. F, C都是从1开始
</p>
<pre class="src src-sh">sort -t <span style="color: #ff00ff;">' '</span> -k1,1d -k2.2,2n <span style="color: #00ffff;">&lt;&lt;</span>-EOF<span style="color: #ff1493;">
bb 113
aa 224
cc 323
dd 444
cc 513
EOF
</span>
aa 224
bb 113
cc 513
cc 323
dd 444
</pre>
</li>
</ul>
<ul>
<li id="sec-6_1_5">-u, &ndash;unique <br/><br />
对输出结果进行去重, 只输出重复的记录中的第一条记录
</li>
</ul>
<ul>
<li id="sec-6_1_6">-m, &ndash;merge <br/><br />
对有序的输入文件进行归并, 这个选项使得你能够在多核机器上优化大数据集的排序
</li>
</ul>
<ul>
<li id="sec-6_1_7">-s, &ndash;stable <br/><br />
使得sort成为稳定排序
</li>
</ul>
<ul>
<li id="sec-6_1_8">-T, &ndash;temporary-directory=DIR <br/><br />
设定指定的临时文件夹, 存放中间数据. 当你排序非常大的文件时, 而且/tmp所在的分区空间不够时, 就会用到该选项了</p>
</li>
</ul>
<ul>
<li id="sec-6_1_9">-n, &ndash;numeric-sort <br/><br />
把输入当整数来排序, 可以有负数, 但是不能含有加号(+)的正数, 这种输入用-g搞定吧
</li>
</ul>
<ul>
<li id="sec-6_1_10">-g, &ndash;general-numeric-sort <br/><br />
把输入当作数值来排序, 可以有浮点数. 如果输入是整数的话, 就用-n搞定吧, 人家性能高些.
</li>
</ul>
<ul>
<li id="sec-6_1_11">-h, &ndash;human-numeric-sort <br/><br />
可以排序2K, 1G等带单位的数字, 很爽啊, 想排序某文件夹下所有文件和文件的大小吗:</p>
<pre class="src src-sh">du -sh * | sort -h
</pre>
</li>
</ul>
<ul>
<li id="sec-6_1_12">-M, &ndash;month-sort <br/><br />
按月份进行排序, `JAN' &lt; `FEB' &lt; &hellip; &lt; `DEC'
</li>
</ul>
<ul>
<li id="sec-6_1_13">-d, &ndash;dictionary-order <br/><br />
按字典序排序, 忽略字母、数字、空白字符外的所有字符
</li>
</ul>
<ul>
<li id="sec-6_1_14">-V, &ndash;version-sort <br/><br />
你开发的软件有很多版了没? 排下吧, 根据版本号</p>
</li>
</ul>
<ul>
<li id="sec-6_1_15">-t, &ndash;field-separator=SEP <br/><br />
设置字段分隔符, 默认为空白字符. 可惜的是, 这个字段分隔符只能为单个字符
</li>
</ul>
<ul>
<li id="sec-6_1_16">-b, &ndash;ignore-leading-blanks <br/><br />
忽略前导空白字符
</li>
</ul>
<ul>
<li id="sec-6_1_17">-f, &ndash;ignore-case <br/><br />
忽略大小写
</li>
</ul>
<ul>
<li id="sec-6_1_18">-i, &ndash;ignore-nonprinting <br/><br />
忽略不可打印字符</p>
</li>
</ul>
<ul>
<li id="sec-6_1_19">-R, &ndash;random-sort <br/><br />
随机排序, 我想你会用到它的, 反正我用过几次. 不过排序结果不完全随机, 因为sort会先对每行进行hash, 然后对hash值进行排序, 所以相同的行一定会排到一块. 不过也许, 这正是你想要的. 如果你想更乱或者更加强大的功能的话, 看<a href="#shuf">这里</a></p>
</li>
</ul>
<ul>
<li id="sec-6_1_20">陷阱 <br/><br />
你是否经常sort一个中文文件却得不到正确结果? 那就对了, 你肯定没设置好语言环境(locale), 试试LC_ALL=C sort吧. sort会根据本地语言环境对输入文本进行排序. LC_ALL=C表示会根据字节值来排序. 或许你说我怎么见到的都是LANG=C sort啊, 来, 我们看看bash info上关于LANG和LC_ALL的解释:</p>
<pre class="example">
LANG   Used to determine the locale category for any category not specifically selected with a variable starting with LC_.
LC_ALL This variable overrides the value of LANG and any other LC_ variable specifying a locale category.
LC_COLLATE
       This variable determines the collation order used when sorting the results of pathname expansion, and  determines  the
       behavior  of  range  expressions,  equivalence  classes, and collating sequences within pathname expansion and pattern
       matching.
</pre>
<ul>
<li>
LANG <br/><br />
如果你没有用LC_来设定某个分类的locale, 将会使用LANG来决定这个分类的locale
</li>
<li>
LC_ALL <br/><br />
该变量会覆盖LANG和LC_
</li>
<li>
LC_COLLATE<br />
该变量设置排序时的locale</p>
</li>
</ul>
<p>所以, sort时, 设置LC_ALL是最保险的做法.
</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-6_2" class="outline-4">
<h4 id="sec-6_2"><span class="section-number-4">6.2</span> tsort (topological sort) </h4>
<div class="outline-text-4" id="text-6_2">
<p>拓朴排序, 该命令可能会用的比较少
</p>
<pre class="src src-sh">tsort <span style="color: #00ffff;">&lt;&lt;</span>EOF<span style="color: #ff1493;">
a b c
d
e f
b c d e
EOF
</span></pre>
<p>
输出:
</p>
<pre class="example">
a
b
c
d
e
f
</pre>
</div>
</div>
<div id="outline-container-6_3" class="outline-4">
<h4 id="sec-6_3"><span class="section-number-4">6.3</span> uniq </h4>
<div class="outline-text-4" id="text-6_3">
<p>也是非常常用的一个命令. 这个命令主要用来对有序序列进行去重, 所以它常和sort联合起来使用, 但是sort -u本身就有去重的功能, 所以当你仅仅只是为了去重时, sort -u就可以帮你搞定了(当输入文本巨大时, 可以用hash来去重提高性能, 比如awk的关联数组), 所以呢, 当年需要对重复的数据进行统计时, 会用到uniq. 当然其实uniq相比sort -u而言, 对重复数据有更加强大的处理
</p>
<ul>
<li id="sec-6_3_1">-c, &ndash;count <br/><br />
在每行文本前面输出重复次数
</li>
</ul>
<ul>
<li id="sec-6_3_2">-d, &ndash;repeated <br/><br />
只显示重复的行, 重复的行只显示一行
</li>
</ul>
<ul>
<li id="sec-6_3_3">-D, &ndash;all-repeated[=delimit-method] <br/><br />
显示所有重复的行, 注意该选项与选项-d的区别
</li>
</ul>
<ul>
<li id="sec-6_3_4">-u, &ndash;unique <br/><br />
只打印不重复的行
</li>
</ul>
<ul>
<li id="sec-6_3_5">-i, &ndash;ignore-case <br/><br />
比较的时候不区分大小写
</li>
</ul>
<ul>
<li id="sec-6_3_6">-f, &ndash;skip-fields=N <br/><br />
不比较前面N个字段, 字段分隔符为空白字符
</li>
</ul>
<ul>
<li id="sec-6_3_7">-s, &ndash;skip-chars=N <br/><br />
不比较前面N个字符
</li>
</ul>
<ul>
<li id="sec-6_3_8">-w, &ndash;check-chars=N <br/><br />
每行最多比较前面N个字符</p>
</li>
</ul>
<ul>
<li id="sec-6_3_9">实例演示 <br/><br />
大家看了uniq上面几个选项后, 是不是有uniq没有太大用处的感觉? 这都是错觉, 下面我给大家演示下uniq在集合运算(统计中有大量的应用)方面巧妙的应用.</p>
<ul>
<li id="sec-6_3_9_1">并集 <br/>
<pre class="src src-sh">sort A B | uniq
</pre>
</li>
</ul>
<ul>
<li id="sec-6_3_9_2">交集 <br/><br />
sort A B | uniq -d
</li>
</ul>
<ul>
<li id="sec-6_3_9_3">差集(A-B) <br/><br />
sort A B B | uniq -u</p>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-6_3_10">缺陷 <br/>
<ul>
<li>
不能控制字段分隔符
</li>
<li>
不能像sort -k那样精确的控制要比较的对象
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-6_4" class="outline-4">
<h4 id="sec-6_4"><span class="section-number-4">6.4</span> comm </h4>
<div class="outline-text-4" id="text-6_4">
<p>逐行比较两个有序文件, 分三列输出文件1独有的行、文件2独有的行、文件12共有的行,
</p>
<pre class="src src-sh">$ cat ab
ax
by
cz

$ cat ac
ax
bd
cz

$ comm ab ac
        ax
    bd
by
        cz
</pre>
<ul>
<li id="sec-6_4_1">-1 <br/><br />
不输出第一列(文件1独有的行)
</li>
</ul>
<ul>
<li id="sec-6_4_2">-2 <br/><br />
不输出第二列(文件2独有的行)
</li>
</ul>
<ul>
<li id="sec-6_4_3">-3 <br/><br />
不输出第三列文件3独有的行
</li>
</ul>
<ul>
<li id="sec-6_4_4">&ndash;check-order <br/><br />
检查输入文件是否有序
</li>
</ul>
<ul>
<li id="sec-6_4_5">&ndash;nocheck-order <br/><br />
不检查输入文件是否有序
</li>
</ul>
<ul>
<li id="sec-6_4_6">&ndash;output-delimiter=STR <br/><br />
设定输出分隔符, 默认为TAB
</li>
</ul>
<ul>
<li id="sec-6_4_7">实例演示 <br/>
<ul>
<li id="sec-6_4_7_1">交集 <br/><br />
comm -12 &lt;(sort A) &lt;(sort B)
</li>
</ul>
<ul>
<li id="sec-6_4_7_2">差集(A-B) <br/><br />
comm -23 &lt;(sort A) &lt;(sort B)</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-shuf" class="outline-4">
<h4 id="shuf"><a name="sec-6_5" id="sec-6_5"></a><span class="section-number-4">6.5</span> shuf (shuffle) </h4>
<div class="outline-text-4" id="text-shuf">
<p>如果sort -R产生的结果还不够乱的话, 我想这个命令应该就是你需要的了. 该命令产生完全乱序的结果, 而且速度应该比sort -R快(shuf不用排序), 还有功能更强大
</p>
<ul>
<li id="sec-6_5_1">-e, &ndash;echo <br/><br />
对命令行参数乱序
</li>
</ul>
<ul>
<li id="sec-6_5_2">-i, &ndash;input-range=LO-HI <br/><br />
对LO到HI之间的数字进行乱序, 比如shuf -i 12-100</p>
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-7" class="outline-3">
<h3 id="sec-7"><span class="section-number-3">7</span> 操作字段 </h3>
<div class="outline-text-3" id="text-7">
</div>
<div id="outline-container-7_1" class="outline-4">
<h4 id="sec-7_1"><span class="section-number-4">7.1</span> cut </h4>
<div class="outline-text-4" id="text-7_1">
<p>挺常用的一个命令, 能非常方便的取某个字段
</p>
<ul>
<li id="sec-7_1_1">-f, &ndash;fields=LIST <br/><br />
选择要输出的字段
</li>
</ul>
<ul>
<li id="sec-7_1_2">-c, &ndash;characters=LIST <br/><br />
选择要输出的字符
</li>
</ul>
<ul>
<li id="sec-7_1_3">-b, &ndash;bytes=LIST <br/><br />
选择要输出的字节
</li>
</ul>
<ul>
<li id="sec-7_1_4">-d, &ndash;delimiter=DELIM <br/><br />
设定字段分隔符, 默认是TAB. 可惜的是, 该分隔符也只能是单个字符.
</li>
</ul>
<ul>
<li id="sec-7_1_5">&ndash;complement <br/><br />
取设定的选择LIST的补集
</li>
</ul>
<ul>
<li id="sec-7_1_6">-s, &ndash;only-delimited <br/><br />
忽略不包含分隔符的行, 默认操作是输出整行
</li>
</ul>
<ul>
<li id="sec-7_1_7">&ndash;output-delimiter=STRING <br/><br />
设定输出分隔符
</li>
</ul>
<ul>
<li id="sec-7_1_8">LIST <br/><br />
-f, -c, -b选项使用的列表, 可以有下面几种形式:</p>
<ul>
<li>
N <br/><br />
第N个字段/字节/字符
</li>
<li>
N- <br/><br />
从第N个到最后一个
</li>
<li>
M-N <br/><br />
从第M个到第N个
</li>
<li>
-M <br/><br />
从第一个到第N个
</li>
</ul>
<p>列表可以有多个, 之间以逗号分割, 比如:
</p>
<pre class="src src-sh">cut -f1-3,4-7
</pre>
</li>
</ul>
</div>
</div>
<div id="outline-container-7_2" class="outline-4">
<h4 id="sec-7_2"><span class="section-number-4">7.2</span> paste </h4>
<div class="outline-text-4" id="text-7_2">
<p>这个命令很有意思, 把两个文件按行粘贴到一块, 曾经我想自己写个程序搞定这个需求, 后来发现linux下竟然已经有这玩意了(linux总能给你带来惊喜)
</p>
<pre class="src src-sh">$ cat num2
1
2
$ cat let3
a
b
c
$ paste num2 let3
1       a
2       b
        c
</pre>
<ul>
<li id="sec-7_2_1">-d, &ndash;delimiters=LIST <br/><br />
paste两个文件的时候, 默认是用TAB分割, 这个选项设定分隔符, 同为可惜的是, 只能为单个字符(主要是paste可以粘贴多个文件, 这个选项的第二个字符用来分割第二个和第三个文件)</p>
</li>
</ul>
<ul>
<li id="sec-7_2_2">-s, &ndash;serial <br/><br />
默认paste是竖着粘贴的, 加了这个选项后, 就横着粘贴了:</p>
<pre class="src src-sh">$ paste -s num2 let3
1       2
a       b       c
</pre>
</li>
</ul>
</div>
</div>
<div id="outline-container-7_3" class="outline-4">
<h4 id="sec-7_3"><span class="section-number-4">7.3</span> join </h4>
<div class="outline-text-4" id="text-7_3">
<p>这是一个稍微高级点的命令, 它把输入文件当成一个key/value对, 然后会把同一个key的所有value粘贴到一块, 来个例子:
</p>
<pre class="src src-sh">$ cat file1
a 1
b 2
c 3
$ cat file2
a 4
c 6
$ join file1 file2
a 1 4
c 3 6
</pre>
<p>
join默认把第一额字段当作key, 字段之间以空格分割, 作为key的字段必须有序.
</p>
<ul>
<li id="sec-7_3_1">-i, &ndash;ignore-case <br/><br />
比较字段时, 忽略大小写
</li>
</ul>
<ul>
<li id="sec-7_3_2">-t CHAR <br/><br />
使用CHAR作为字段分隔符, 又是只能为单个字符(杯具&hellip;)</p>
</li>
</ul>
<ul>
<li id="sec-7_3_3">-1 FIELD <br/><br />
设定第一个文件的key为第FIELD个字段
</li>
</ul>
<ul>
<li id="sec-7_3_4">-2 FIELD <br/><br />
设定第二个文件的key为第FIELD个字段
</li>
</ul>
<ul>
<li id="sec-7_3_5">-j FIELD <br/><br />
-1 FIELD -2 FIELD</p>
</li>
</ul>
<ul>
<li id="sec-7_3_6">-a FILENUM <br/><br />
join默认只打印拥有相同key的行, 该选项会打印第FILENUM个文件中没有匹配上的行
</li>
</ul>
<ul>
<li id="sec-7_3_7">-v FILENUM <br/><br />
和-a选项有点类似, 该选项只打印第FILENUM个文件中没有匹配上的行, 不会打印匹配上的行</p>
</li>
</ul>
<ul>
<li id="sec-7_3_8">&ndash;check-order <br/><br />
检查输入文件作为key的字段是否有序
</li>
</ul>
<ul>
<li id="sec-7_3_9">&ndash;nocheck-order <br/><br />
不检查输入文件作为key的字段是否有序</p>
</li>
</ul>
<ul>
<li id="sec-7_3_10">-o FIELD-LIST <br/><br />
高级的控制输出对象的选项, FIELD-LIST中的每个元素具有下面这样的形式:</p>
<ul>
<li>
0 <br/><br />
表示做为key的字段
</li>
<li>
M.N <br/><br />
M为文件号, 取值为0或者1, N为字段号, M.N就是取第M个文件第N个字段
</li>
</ul>
<p>每个元素之间以逗号或者空格分割
</p>
</li>
</ul>
<ul>
<li id="sec-7_3_11">-e EMPTY <br/><br />
-o选项中, 可能文件M中没有字段号N, 这时候输出的时候用EMPTY代替.</p>
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-8" class="outline-3">
<h3 id="sec-8"><span class="section-number-3">8</span> 操作字符 </h3>
<div class="outline-text-3" id="text-8">
</div>
<div id="outline-container-8_1" class="outline-4">
<h4 id="sec-8_1"><span class="section-number-4">8.1</span> tr (translate) </h4>
<div class="outline-text-4" id="text-8_1">
<p>主要对文本中的字符进行替换、删除.
</p>
<ul>
<li>
该命令只支持标准输入, <b>不支持从文件输入</b>.
</li>
<li>
<b><span style="color: red;">tr仅支持单字节字符.</b></span>
</li>
</ul>
<pre class="src src-sh">tr <span style="color: #ffff00;">[</span>OPTION<span style="color: #ffff00;">]</span>... SET1 <span style="color: #ffff00;">[</span>SET2<span style="color: #ffff00;">]</span>
</pre>
<p>
字符集合可以由一系列的字符构成, 也可以具有以下形式:
</p>
<ul>
<li>
CHAR1-CHAR2 <br/><br />
从CHAR1到CHAR2的所有字符
</li>
<li>
[CHAR*] <br/><br />
这种形式只能出现在SET2中, 表示拷贝CHAR直到SET2和SET1的长度相等
</li>
<li>
[CHAR*REPEAT] <br/><br />
REPEAT个CHAR
</li>
<li>
[:alnum:] <br/><br />
所有的字母和数字
</li>
<li>
[:alpha:] <br/><br />
所有的字母
</li>
<li>
[=CHAR=] <br/><br />
和CHAR属于同一个字符类中的所有字符</p>
</li>
</ul>
<p>当提供2个字符集合时, 表示把SET1中的字符替换成SET2中的对应的字符, 比如:
</p>
<pre class="src src-sh">tr a A <span style="color: #00ffff;">&lt;</span> file <span style="color: #ff7f24;"># </span><span style="color: #ff0000;">&#25226;&#25991;&#20214;file&#20013;&#30340;&#23567;&#20889;a&#37117;&#21464;&#25104;&#22823;&#20889;A
</span>tr <span style="color: #ff00ff;">'[:lower:]'</span> <span style="color: #ff00ff;">'[:upper:]'</span> <span style="color: #00ffff;">&lt;</span> file <span style="color: #ff7f24;"># </span><span style="color: #ff0000;">&#25226;&#25991;&#20214;file&#20840;&#37096;&#22823;&#20889;
</span></pre>
<ul>
<li id="sec-8_1_1">-d, &ndash;delete <br/><br />
删除出现在集合1中的所有字符. 下面的命令把文件file中所有的行连成一行:</p>
<pre class="src src-sh">tr -d <span style="color: #ff00ff;">"\r\n"</span> <span style="color: #00ffff;">&lt;</span> file
</pre>
</li>
</ul>
<ul>
<li id="sec-8_1_2">-s, &ndash;squeeze-repeats <br/><br />
把SET1中连续的字符都替换成1个字符, 当SET2也提供时, 首先执行删除连续字符操作, 然后才执行替换操作</p>
</li>
</ul>
<ul>
<li id="sec-8_1_3">-c, -C, &ndash;complement <br/><br />
使用SET1的补集</p>
</li>
</ul>
<ul>
<li id="sec-8_1_4">-t, &ndash;truncate-set1 <br/><br />
首先把SET1中的字符截断到和SET2长度相等</p>
</li>
</ul>
<ul>
<li id="sec-8_1_5">陷阱 <br/><br />
经常见到有同学对会含有中文的文件用上面的方法进行大小写转换:</p>
<pre class="src src-sh"><span style="color: #ff7f24;"># </span><span style="color: #ff0000;">&#32456;&#31471;&#32534;&#30721;&#20026;GB18030&#32534;&#30721;
</span>$ tr <span style="color: #ff00ff;">'[:upper:]'</span> <span style="color: #ff00ff;">'[:lower:]'</span> <span style="color: #00ffff;">&lt;&lt;&lt;</span> &#29700;
&#29752;
</pre>
<p>
为什么琄会变成琸呢? <br/><br />
上面我们说到, tr是按字节来处理的, 而GB18030编码第二个字节编码范围为0×40-0×7E和0×80-0×FE, 这样, 第二个字节就可能出现ASCII码, 我们来看下上面2个汉字的GB18030编码值:
</p>
<pre class="src src-sh">$ od -c  <span style="color: #00ffff;">&lt;&lt;&lt;</span> &#29700;
0000000 254   K  \n
0000003
$ od -t x1  <span style="color: #00ffff;">&lt;&lt;&lt;</span> &#29700;
0000000 ac 4b 0a
0000003

$ od -c  <span style="color: #00ffff;">&lt;&lt;&lt;</span> &#29752;
0000000 254   k  \n
0000003
$ od -t x1  <span style="color: #00ffff;">&lt;&lt;&lt;</span> &#29752;
0000000 ac 6b 0a
0000003
</pre>
<p>
看来确实如此, 琄的第二个字节是字符大K, 琸的第二个字节是字符小k.
</p>
<p>
看来, 如果文本里含有多字节字符, 使用tr的时候得小心咯.
</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-8_2" class="outline-4">
<h4 id="sec-8_2"><span class="section-number-4">8.2</span> expand </h4>
<div class="outline-text-4" id="text-8_2">
<p>每个编辑器对TAB的显示设置不一样, 有的显示为8个字符, 有的显示为4个字符, 这样就造成了在A编辑器下排版很漂亮, 到了B编辑器下变得一团糟, 所以编码的时候最好使用空白字符代替TAB(Emacs中这样设置: (setq-default indent-tabs-mode nil), <img src='http://ahei.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ), expand命令也可以帮你把TAB转换成空格
</p>
<ul>
<li id="sec-8_2_1">-i, &ndash;initial <br/><br />
不转换非空白字符后的TAB
</li>
</ul>
<ul>
<li id="sec-8_2_2">-t, &ndash;tabs=NUMBER <br/><br />
设置一个TAB转换成几个空格, 默认是8</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-8_3" class="outline-4">
<h4 id="sec-8_3"><span class="section-number-4">8.3</span> unexpand </h4>
<div class="outline-text-4" id="text-8_3">
<p>Makefile缩进的时候, 必须是TAB, 所以有时候又需要把空格变成TAB, 就靠unexpand了
</p>
<ul>
<li id="sec-8_3_1">-a, &ndash;all <br/><br />
转换所有的空格, 默认情况下只转换开头的空格
</li>
</ul>
<ul>
<li id="sec-8_3_2">&ndash;first-only <br/><br />
只转换开头的空格
</li>
</ul>
<ul>
<li id="sec-8_3_3">-t, &ndash;tabs=N <br/><br />
几个空格转换成一个TAB, 默认是8</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-8_4" class="outline-4">
<h4 id="sec-8_4"><span class="section-number-4">8.4</span> colrm (COLumn ReMove) </h4>
<div class="outline-text-4" id="text-8_4">
<pre class="src src-sh">colrm <span style="color: #ffff00;">[</span>start <span style="color: #ffff00;">[</span>stop<span style="color: #ffff00;">]]</span>
</pre>
<p>
该命令只支持标准输入, <b>不支持从文件输入</b>. <br/><br />
删除每行从start到stop之间的字符, 如果stop没有指定的话, 则删除到末尾. 需要注意的是, TAB被认为占8列(不知道为啥这样搞)
</p>
</div>
</div>
</div>
<div id="outline-container-9" class="outline-3">
<h3 id="sec-9"><span class="section-number-3">9</span> 文本切割 </h3>
<div class="outline-text-3" id="text-9">
</div>
<div id="outline-container-9_1" class="outline-4">
<h4 id="sec-9_1"><span class="section-number-4">9.1</span> split </h4>
<div class="outline-text-4" id="text-9_1">
<p>切割文本INPUT成文件PREFIXaa, PREFIXab &hellip; 默认每个文件1000行, PREFIX为x
</p>
<pre class="src src-sh">split <span style="color: #ffff00;">[</span>OPTION<span style="color: #ffff00;">]</span>... <span style="color: #ffff00;">[</span>INPUT <span style="color: #ffff00;">[</span>PREFIX<span style="color: #ffff00;">]]</span>
</pre>
<ul>
<li id="sec-9_1_1">-l, &ndash;lines=NUMBER <br/><br />
按行切割, 每个输出文件NUMBER行, 比较常用的切割方式
</li>
</ul>
<ul>
<li id="sec-9_1_2">-b, &ndash;bytes=SIZE <br/><br />
按字节切分
</li>
</ul>
<ul>
<li id="sec-9_1_3">-C, &ndash;line-bytes=SIZE <br/><br />
每个文件最多SIZE个自己, 但是每行都完整的保存到一个输出文件中, 即不像-b那样, 可能一个整行被拆分到多个文件中去
</li>
</ul>
<ul>
<li id="sec-9_1_4">-d, &ndash;numeric-suffixes <br/><br />
使用数字做为后缀名</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-9_2" class="outline-4">
<h4 id="sec-9_2"><span class="section-number-4">9.2</span> csplit (context split) </h4>
<div class="outline-text-4" id="text-9_2">
<p>根据模式切割文件, 简单了解即可
</p>
<pre class="src src-sh">csplit <span style="color: #ffff00;">[</span>OPTION<span style="color: #ffff00;">]</span>... FILE PATTERN...
</pre>
</div>
</div>
</div>
<div id="outline-container-10" class="outline-3">
<h3 id="sec-10"><span class="section-number-3">10</span> 文本编码 </h3>
<div class="outline-text-3" id="text-10">
</div>
<div id="outline-container-10_1" class="outline-4">
<h4 id="sec-10_1"><span class="section-number-4">10.1</span> iconv </h4>
<div class="outline-text-4" id="text-10_1">
<p>经常会用到, 主要用来转换编码
</p>
<ul>
<li id="sec-10_1_1">&ndash;list, -l <br/><br />
列出可以识别的编码
</li>
</ul>
<ul>
<li id="sec-10_1_2">-c <br/><br />
转换的时候, 忽视无效的字符, 如果没有加这个选项, iconv碰到这个无效字符会直接报错退出, 所以最好加上这个选项</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-10_2" class="outline-4">
<h4 id="sec-10_2"><span class="section-number-4">10.2</span> enca </h4>
<div class="outline-text-4" id="text-10_2">
<p>我们经常需要知道文件的编码, 这个命令帮你搞定
</p>
</div>
</div>
</div>
<div id="outline-container-11" class="outline-3">
<h3 id="sec-11"><span class="section-number-3">11</span> 文本格式化 </h3>
<div class="outline-text-3" id="text-11">
<p>打扮一下你的文本吧.
</p>
</div>
<div id="outline-container-11_1" class="outline-4">
<h4 id="sec-11_1"><span class="section-number-4">11.1</span> column </h4>
<div class="outline-text-4" id="text-11_1">
<p>按列漂亮的输出:
</p>
<pre class="src src-sh">$ <span style="color: #6495ed;">(</span><span style="color: #b0c4de;">printf</span> <span style="color: #ff00ff;">"PERM LINKS OWNER GROUP SIZE DAY HH:MM NAME\n"</span>; ls -l | sed 1d<span style="color: #6495ed;">)</span> | column -t
</pre>
<pre>
PERM        LINKS  OWNER       GROUP       SIZE   DAY         HH:MM  NAME
drwxr-xr-x  3      taoshanwen  taoshanwen  4096   2012-04-03  22:54  <span style="color:blue;font-weight:bold;">ai</span>
drwxr-xr-x  26     taoshanwen  taoshanwen  4096   2012-04-15  11:59  <span style="color:blue;font-weight:bold;">algorithm</span>
drwxr-xr-x  2      taoshanwen  taoshanwen  4096   2012-04-09  13:35  <span style="color:blue;font-weight:bold;">arch</span>
drwxr-xr-x  5      taoshanwen  taoshanwen  4096   2012-04-03  22:47  <span style="color:blue;font-weight:bold;">c-c++</span>
drwxr-xr-x  6      taoshanwen  taoshanwen  4096   2012-04-14  20:33  <span style="color:blue;font-weight:bold;">CIP</span>
drwxr-xr-x  5      taoshanwen  taoshanwen  4096   2012-04-03  22:47  <span style="color:blue;font-weight:bold;">computer-chess</span>
drwxr-xr-x  2      taoshanwen  taoshanwen  4096   2012-04-15  00:23  <span style="color:blue;font-weight:bold;">computer-go</span>
drwxr-xr-x  3      taoshanwen  taoshanwen  4096   2012-04-10  16:25  <span style="color:blue;font-weight:bold;">database</span>
drwxr-xr-x  3      taoshanwen  taoshanwen  4096   2012-04-15  00:57  <span style="color:blue;font-weight:bold;">distributed</span>
drwxr-xr-x  5      taoshanwen  taoshanwen  4096   2012-04-03  22:47  <span style="color:blue;font-weight:bold;">genetic-prog</span>
drwxr-xr-x  3      taoshanwen  taoshanwen  4096   2012-04-03  22:47  <span style="color:blue;font-weight:bold;">infosec</span>
drwxr-xr-x  2      taoshanwen  taoshanwen  4096   2011-03-19  20:40  <span style="color:blue;font-weight:bold;">iphone</span>
drwxr-xr-x  20     taoshanwen  taoshanwen  4096   2012-04-15  00:38  <span style="color:blue;font-weight:bold;"><a href="http://ahei.info/t/java" class="st_tag internal_tag" rel="tag" title="标签 java 下的日志">java</a></span>
drwxr-xr-x  94     taoshanwen  taoshanwen  16384  2012-04-17  20:01  <span style="color:blue;font-weight:bold;"><a href="http://ahei.info/c-linux.htm" class="st_tag internal_tag" rel="tag" title="标签 linux 下的日志">linux</a></span>
drwxr-xr-x  7      taoshanwen  taoshanwen  4096   2012-04-10  19:29  <span style="color:blue;font-weight:bold;">math</span>
drwxr-xr-x  2      taoshanwen  taoshanwen  4096   2012-04-17  15:37  <span style="color:blue;font-weight:bold;">mysql</span>
drwxr-xr-x  2      taoshanwen  taoshanwen  4096   2011-10-19  17:04  <span style="color:blue;font-weight:bold;">nosql</span>
drwxr-xr-x  11     taoshanwen  taoshanwen  4096   2012-04-16  12:54  <span style="color:blue;font-weight:bold;">other</span>
drwxr-xr-x  2      taoshanwen  taoshanwen  4096   2012-04-07  14:03  <span style="color:blue;font-weight:bold;">perl</span>
drwxr-xr-x  3      taoshanwen  taoshanwen  4096   2012-04-15  00:18  <span style="color:blue;font-weight:bold;">python</span>
drwxr-xr-x  6      taoshanwen  taoshanwen  4096   2012-04-03  22:50  <span style="color:blue;font-weight:bold;">ruby</span>
drwxr-xr-x  52     taoshanwen  taoshanwen  4096   2012-04-15  00:59  <span style="color:blue;font-weight:bold;">search-engine</span>
drwxr-xr-x  9      taoshanwen  taoshanwen  4096   2012-04-15  00:23  <span style="color:blue;font-weight:bold;">software-engineering</span>
drwxr-xr-x  5      taoshanwen  taoshanwen  4096   2010-10-11  22:56  <span style="color:blue;font-weight:bold;">svnroot</span>
drwxr-xr-x  7      taoshanwen  taoshanwen  4096   2012-04-14  20:33  <span style="color:blue;font-weight:bold;">web</span>
drwxr-xr-x  66     taoshanwen  taoshanwen  12288  2012-04-17  23:47  <span style="color:blue;font-weight:bold;">work</span>
</pre>
</div>
</div>
<div id="outline-container-11_2" class="outline-4">
<h4 id="sec-11_2"><span class="section-number-4">11.2</span> fold </h4>
<div class="outline-text-4" id="text-11_2">
<p>将一个比较长的文本行输出进行"折行".
</p>
</div>
</div>
<div id="outline-container-11_3" class="outline-4">
<h4 id="sec-11_3"><span class="section-number-4">11.3</span> fmt </h4>
<div class="outline-text-4" id="text-11_3">
<p>将输入按照指定宽度进行折行, 功能较fold强大些
</p>
</div>
</div>
</div>
<div id="outline-container-12" class="outline-3">
<h3 id="sec-12"><span class="section-number-3">12</span> 微语言 </h3>
<div class="outline-text-3" id="text-12">
<p>下面介绍文本处理中两个最强大的命令sed和awk, 它们已经具有一些程序设计语言的特征了, 特别是awk, 所以, 我们的脚本中, 放眼望去, 皆是awk阿. 熟练掌握这两个命令, 你的文本处理功力将会极大的提升阿.
</p>
</div>
<div id="outline-container-12_1" class="outline-4">
<h4 id="sec-12_1"><span class="section-number-4">12.1</span> sed (Stream EDitor) </h4>
<div class="outline-text-4" id="text-12_1">
<p>sed是一个流编辑器, 类似ed(行编辑器, 通过各种命令编辑文件), 它提供了各种替换、删除的命令, 使得这些编辑操作能自动化起来.
</p>
<ul>
<li id="sec-12_1_1">工作流程 <br/><br />
sed维护2快内存(也可以理解为2个变量, 或者说是2个寄存器), 分别叫做 <b>pattern space</b> 和 <b>hold space</b>, sed对每行输入执行下面的循环:</p>
<ol>
<li>
读入输入行, 去掉尾部的换行符, 存入pattern space
</li>
<li>
执行sed命令, 每条sed命令都可以有个地址与它关联, 这个地址就类似于条件语句, 只有这个条件语句通过验证时, 其对应的命令才会执行
</li>
<li>
执行完所有的sed命令后, 如果没有指定sed的-n选项, 将会打印pattern space的内容, 然后再输出换行符. 最后继续读入下一行, 进行下一次的循环
</li>
</ol>
<p>每次循环开始时, pattern space的内容会被清空, hold space则不会
</p>
</li>
</ul>
<ul>
<li id="sec-12_1_2">地址格式 <br/><br />
sed地址可以具有以下的形式:</p>
<ul>
<li>
NUMBER <br/><br />
指定执行命令的行号, 只有在这行, 对应的命令才会被执行, 行号从1开始, 另外, 如果没有指定-i或者-s选项的话, 所有的输入文件会被当成一个输入流, 行号就会一直累加的
</li>
<li>
FIRST~STEP <br/><br />
在FIRST、FIRST+STEP、FIRST+2*STEP、、FIRST+3*STEP行执行对应的命令
</li>
<li>
$ <br/><br />
最后一行
</li>
<li>
<i>REGEXP</i> <br/><br />
在匹配上正则REGEXP的行执行对应的命令, 如果REGEXP中含有/, 需要用\转义
</li>
<li>
\%REGEXP%<br />
在匹配上正则REGEXP的行执行对应的命令, %也可以是其他字符, 如果REGEXP中含有%, 需要用\转义
</li>
<li>
/REGEXP/I, \%REGEXP%I<br />
忽略大小写
</li>
<li>
/REGEXP/M, \%REGEXP%M<br />
可以匹配多行, M表示multi-line
</li>
</ul>
<p>如果没有指定地址的话, 表示所有行对执行命令. 还可以提供2个地址, 指定一个地址范围, 这2个地址之间以逗号分割, 比如:
</p>
<pre class="example">
ADDRESS1,ADDRESS2
</pre>
<p>这样, 第一次匹配上ADDRESS1的行与第一次匹配上ADDRESS2的行之间的所有行都会执行对应的命令. <br/><br />
GNU sed还支持下面几种地址范围:
</p>
<ul>
<li>
ADDR1,+N <br/><br />
匹配ADDR1, 以及接下来的N行
</li>
<li>
ADDR1,~N <br/><br />
匹配ADDR1, 直到行号为N倍数的行</p>
</li>
</ul>
<p>在地址或者地址范围后加感叹号(!), 表示取反.
</p>
</li>
</ul>
<ul>
<li id="sec-12_1_3">常用命令 <br/><br />
由于sed默认会打印pattern space, 所以不加任何命令的话, 就和cat一样, 打印所有的输出:</p>
<pre class="src src-sh">$ cat ab
ab
ab
ac
ad
ae
ac
ab

$ sed <span style="color: #ff00ff;">""</span> ab
ab
ab
ac
ad
ae
ac
ab
</pre>
<ul>
<li id="sec-12_1_3_1">d <br/><br />
删除pattern space, 立即进入下一轮循环. <br/><br />
ls输出的时候, 第一行有个摘要, 如果不想显示的话, 这样:</p>
<pre class="src src-sh">ls -l | sed 1d
</pre>
</li>
</ul>
<ul>
<li id="sec-12_1_3_2">p <br/><br />
打印pattern space. <br/><br />
输出文件ab第5到第10行:</p>
<pre class="src src-sh">sed -n 5,10p ab
</pre>
</li>
</ul>
<ul>
<li id="sec-12_1_3_3">q [EXIT-CODE] <br/><br />
立即以返回码EXIT-CODE(默认为0)退出sed, 如果没有加-n选项的话, 当前的模式空间也会打印出来. <br/><br />
如果文件很大的话, 下面的方法输出文件ab第5到第10行会快很多:</p>
<pre class="src src-sh">sed -n <span style="color: #ff00ff;">"5,10p; 10q"</span> ab
</pre>
</li>
</ul>
<ul>
<li id="sec-12_1_3_4">n <br/><br />
如果没有加-n的话, 打印模式空间, 然后直接进入下一轮循环. <br/><br />
不打印第18行:</p>
<pre class="src src-sh">sed -n <span style="color: #ff00ff;">"p; 18n"</span>
</pre>
</li>
</ul>
<ul>
<li id="sec-12_1_3_5">{ COMMANDS } <br/><br />
一组命令, 这组命令共用同一个地址. <br/><br />
打印第8行:</p>
<pre class="src src-sh">sed -n <span style="color: #ff00ff;">"8 {p; q}"</span>
</pre>
</li>
</ul>
<ul>
<li id="sec-12_1_3_6">s/REGEXP/REPLACEMENT/FLAGS <br/><br />
这个命令估计是大家用的最多的命令了. 前面几个命令大家不知道的情况下, 可能通过其他命令解决了, 但是这个命令的功能除了awk, 其他的做不了, 而sed比awk更简洁.<br />
这个命令主要是对pattern space进行替换, 对匹配REGEXP的部分用REPLACEMENT进行替换, 用来分割的/可以由其他字符组成, 比如s:REGEXP:REPLACEMENT:FLAGS. REPLACEMENT可以由原始的字符组成, 也可以由下面带有特殊意义的串组成:</p>
<ul>
<li>
&amp; <br/><br />
匹配REGEXP的部分
</li>
<li>
\d <br/><br />
d为1-9的数字, \d表示REGEXP中第d个括号匹配的部分, 比如REGEXP为:</p>
<pre class="example">
a(..(..))
</pre>
<p>pattern space为abcde, 那么\1为bcde, \2为de
</p>
</li>
<li>
\L <br/><br />
把REPLACEMENT中的字符变成小写, 直到遇到\U和\E. 比如:</p>
<pre class="src src-sh">$ cat ab
AB
AB
ac
ad
ae
ac
AB

$ sed -r <span style="color: #ff00ff;">'s/(AB)/\L\1YYY/'</span> ab
abyyy
abyyy
ac
ad
ae
ac
abyyy
</pre>
<p>
上例中, 本来\1应该是AB, 但是\L把它全变成小写了. 而且后面的YYY也变成小写了.
</p>
</li>
<li>
\l <br/><br />
把下一个字符变成小写
</li>
<li>
\U <br/><br />
把REPLACEMENT中的字符变成大写, 直到遇到\L和\E.
</li>
<li>
\u <br/><br />
把下一个字符变成大写
</li>
<li>
\E <br/><br />
结束\L和\U的作用</p>
</li>
</ul>
<p>s命令后的FLAGS可以由下面几种:
</p>
<ul>
<li>
g <br/><br />
s命令默认只替换第一个匹配, g可以让它全部替换
</li>
<li>
NUMBER <br/><br />
替换第NUMBER个匹配
</li>
<li>
p <br/><br />
如果发生了替换, 打印模式空间. <br/><br />
搜索文件ab中xxx并替换成yyy打印出来:</p>
<pre class="src src-sh">sed s/xxx/yyy/p ab
</pre>
</li>
<li>
i, I <br/><br />
正则匹配忽略大小写
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-12_1_3_7">y/SOURCE-CHARS/DEST-CHARS/ <br/><br />
类似tr命令, 用DEST-CHARS对应的字符替换出现在SOURCE-CHARS中的字符. 和s命令一样, 分隔符/也可以是其他字符.</p>
</li>
</ul>
<ul>
<li id="sec-12_1_3_8">= <br/><br />
打印行号. 下面的命令类似grep -n:</p>
<pre class="src src-sh">sed  -n <span style="color: #ff00ff;">'/xxx/ {=; p}'</span>
</pre>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-12_1_4">高级命令 <br/>
<ul>
<li id="sec-12_1_4_1">h <br/><br />
用pattern space替换hold space
</li>
</ul>
<ul>
<li id="sec-12_1_4_2">H <br/><br />
先在hold space追加换行符, 再往hold space追加pattern space
</li>
</ul>
<ul>
<li id="sec-12_1_4_3">g <br/><br />
用hold space替换pattern space
</li>
</ul>
<ul>
<li id="sec-12_1_4_4">G <br/><br />
先在pattern space追加换行符, 再往pattern space追加hold space
</li>
</ul>
<ul>
<li id="sec-12_1_4_5">x <br/><br />
交换pattern space和hold space的内容
</li>
</ul>
<ul>
<li id="sec-12_1_4_6">D <br/><br />
删除模式空间的第一行. 如果模式空间中还有内容的话, 开始进入下一轮循环, 但不读入输入. 如果没有内容的话, 读入输入并进行下一轮循环.
</li>
</ul>
<ul>
<li id="sec-12_1_4_7">N <br/><br />
追加换行符到pattern space, 并读入下一行输入追加到pattern space, 如果已经没有任何输入, 直接退出sed, 不再处理任何命令.
</li>
</ul>
<ul>
<li id="sec-12_1_4_8">P <br/><br />
大写p, 打印pattern space第一行
</li>
</ul>
<ul>
<li id="sec-12_1_4_9">z <br/><br />
清空pattern space</p>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-12_1_5">专家命令 <br/>
<ul>
<li id="sec-12_1_5_1">: LABEL <br/><br />
设定标签, 类似C语言中设定一个标签, 然后可以goto之
</li>
</ul>
<ul>
<li id="sec-12_1_5_2">b [LABEL] <br/><br />
跳转到标签, 如果没有提供标签的话, 直接进入下一轮循环
</li>
</ul>
<ul>
<li id="sec-12_1_5_3">t [LABEL] <br/><br />
这轮循环中, 如果s命令替换成功过, 则跳转到标签. 如果没有提供标签的话, 直接进入下一轮循环
</li>
</ul>
<ul>
<li id="sec-12_1_5_4">T LABEL <br/><br />
和t LABEL相反, s命令替换失败, 才跳转到标签</p>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-12_1_6">命令选项 <br/>
<ul>
<li id="sec-12_1_6_1">-n, &ndash;quiet, &ndash;silent <br/><br />
禁止自动打印pattern space
</li>
</ul>
<ul>
<li id="sec-12_1_6_2">-i[SUFFIX], &ndash;in-place[=SUFFIX] <br/><br />
原地编辑文件, 文本修改后会直接影响到输入文件
</li>
</ul>
<ul>
<li id="sec-12_1_6_3">-r, &ndash;regexp-extended <br/><br />
启用扩展正则, 默认是基础正则
</li>
</ul>
<ul>
<li id="sec-12_1_6_4">-s, &ndash;separate <br/><br />
把每个文件当作单独的输入流, 而不是一个输入流</p>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-12_1_7">给我一个寄存器, 我可以干好多事 <br/><br />
各位看完上面所说的sed命令后, 是不是觉得sed只能做一些替换、删除等操作， 为什么称为微语言呢, 我之所以把它归到微语言这一类是有原因的, 它具有下面几个语言的特征:</p>
<ol>
<li>
有条件判断能力, sed的地址就是一种条件判断, 还有标签命令也是条件判断
</li>
<li>
有流程控制能力, 标签命令就可以做到
</li>
<li>
有变量, 虽然很少, 只有2个, 但是仍然可以干好多事了, 看下面的例子
</li>
</ol>
<p>更多的例子参加sed info
</p>
<ul>
<li id="sec-12_1_7_1">tac <br/><br />
模拟tac</p>
<pre class="src src-sh">sed -n <span style="color: #ff00ff;">'1!G; $p; h'</span>
</pre>
</li>
</ul>
<ul>
<li id="sec-12_1_7_2">为数字字串增加逗号分隔符号， 将1234567变为1,234,567 <br/>
<pre class="src src-sh">sed -r <span style="color: #ff00ff;">':a; s/(.*[0-9])([0-9]{3})/\1,\2/; ta'</span> <span style="color: #00ffff;">&lt;&lt;&lt;</span> 124523536543652
</pre>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-12_2" class="outline-4">
<h4 id="sec-12_2"><span class="section-number-4">12.2</span> awk (Alfred V. Aho, Peter J. Weinberger, Brian W. Kernighan.) </h4>
<div class="outline-text-4" id="text-12_2">
<p>awk是文本处理的利器, 前面那些命令能干的事它都能搞定.
</p>
<ul>
<li id="sec-12_2_1">工作流程 <br/><br />
awk的工作方式有点类似sed, sed是地址+命令, awk则是pattern+action, pattern是要匹配的模式, action是要执行的命令, pattern可以由下面几种形式:</p>
<ul>
<li>
BEGIN <br/><br />
awk程序开始处理输入时
</li>
<li>
END <br/><br />
awk程序结束处理输入时
</li>
<li>
BEGINFILE <br/><br />
awk程序开始处理每个文件时
</li>
<li>
ENDFILE <br/><br />
awk程序结束处理每个文件时
</li>
<li>
<i>regular expression</i>
</li>
<li>
relational expression <br/><br />
关系表达式
</li>
<li>
pattern &amp;&amp; pattern
</li>
<li>
pattern || pattern
</li>
<li>
pattern ? pattern : pattern
</li>
<li>
(pattern)
</li>
<li>
! pattern
</li>
<li>
pattern1, pattern2
</li>
</ul>
<p>action要以大括号括起来, 比如:
</p>
<pre class="src src-sh">awk <span style="color: #ff00ff;">'0{print} 1{print}'</span> .<a href="http://ahei.info/c-emacs.htm" class="st_tag internal_tag" rel="tag" title="标签 Emacs 下的日志">emacs</a>
</pre>
</li>
</ul>
<ul>
<li id="sec-12_2_2">内置变量 <br/>
<ul>
<li>
ARGC <br/><br />
awk输入参数的个数, 不包括awk自己
</li>
<li>
ARGV <br/><br />
命令行参数
</li>
<li>
ARGIND <br/><br />
当前处理的文件在ARGV中的位置, ARGV[ARGIND]表示当前正在处理的文件, 可以通过这个变量来对不同的输入文件做不同的处理
</li>
<li>
FNR (File Number Record) <br/><br />
当前文件的记录总数
</li>
<li>
NR (Number Record) <br/><br />
目前处理的记录总数
</li>
<li>
NF (Number of Field)<br/><br />
当前记录的字段数
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-12_2_3">网络编程 <br/><br />
awk能开发网络程序, 你相信吗?</p>
<pre class="src src-sh">$ cat test.awk
<span style="color: #ff7f24;">#</span><span style="color: #ff0000;">!/usr/bin/awk -f
</span>
BEGIN <span style="color: #00ff00;">{</span>
    str = <span style="color: #ff00ff;">"GET /index.html HTTP/1.1\r\nHost: www.baidu.com\r\n\r\n"</span>
    print str |&amp; <span style="color: #ff00ff;">"/inet/tcp/0/www.baidu.com/80"</span>
    <span style="color: #ff00ff;">"/inet/tcp/0/www.baidu.com/80"</span> |&amp; getline
    print
<span style="color: #00ff00;">}</span>

$ awk -f test.awk
HTTP/1.1 200 OK
</pre>
</li>
</ul>
<ul>
<li id="sec-12_2_4">陷阱 <br/>
<ul>
<li id="sec-12_2_4_1">tolower/toupper <br/><br />
和tr一样, 这2个函数也是对字节进行处理</p>
</li>
</ul>
<ul>
<li id="sec-12_2_4_2">判断元素是否存在 <br/><br />
你是否这样判断某元素是否存在于某数组:</p>
<pre class="example">
if (a[e] != 2) { ... }
</pre>
<p>如果输入很大的话, 过会你就会发现你的awk占了很多内存, 原因就是a[e]的时候, 如果awk发现a中没有e, 就会把e插入到a中, 这样一来内存自然越来越大, 正确的判断方法是:
</p>
<pre class="example">
if (!(e in a)) { ... }
</pre>
<p>用过python的朋友可能会这样写:
</p>
<pre class="example">
if (e not in a) { ... }
</pre>
<p>很不幸, 没有这样的语法, 而且还不报错, 我猜awk把e not连接成一个字符串了&hellip;
</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-13" class="outline-3">
<h3 id="sec-13"><span class="section-number-3">13</span> 语言 </h3>
<div class="outline-text-3" id="text-13">
<ul>
<li>
a2p
</li>
<li>
s2p
</li>
<li>
perl
</li>
<li>
python
</li>
</ul>
</div>
</div>
<div id="outline-container-14" class="outline-3">
<h3 id="sec-14"><span class="section-number-3">14</span> 实例 </h3>
<div class="outline-text-3" id="text-14">
</div>
<div id="outline-container-14_1" class="outline-4">
<h4 id="sec-14_1"><span class="section-number-4">14.1</span> 我的正则会数学 </h4>
<div class="outline-text-4" id="text-14_1">
<ul>
<li>
multi-sort
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-15" class="outline-3">
<h3 id="sec-15"><span class="section-number-3">15</span> 参考文献 </h3>
<div class="outline-text-3" id="text-15">
<ul>
<li>
相关命令的info及coreutils的<a href="http://www.gnu.org/software/coreutils/manual/coreutils.pdf">info</a>
</li>
<li>
<a href="http://www.tsnc.edu.cn/default/tsnc_wgrj/doc/abs-3.9.1_cn/html/textproc.html">高级Bash脚本编程指南: 文本处理命令</a>
</li>
<li>
<a href="http://www.cosc.brocku.ca/~taa/papers/abou-assaleh_csci6306a.pdf">Survey of Global Regular Expression Print (GREP) Tools</a>
</li>
</ul>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/text-processing.htm/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>灵异的shell</title>
		<link>http://ahei.info/shell.htm</link>
		<comments>http://ahei.info/shell.htm#comments</comments>
		<pubDate>Thu, 01 Dec 2011 04:36:35 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://ahei.info/?p=40851</guid>
		<description><![CDATA[1 引子 2 语法介绍 2.1 定义 2.2 管道 2.3 引用 (QUOTING) 2.4 参数 (PARAMETERS) 2.5 扩展 (EXPANSION) 2.6 重定向 3 小技巧 4 工具 4.1 log4sh 4.2 shunit 4.3 bashdb 5 shell快捷键 6 shell炸弹 7 shell加密 7.1 shc 7.2 wzsh 8 宝典 1 引子 我06年开始接触shell编程, 一开始照着别人的例子写些简单的脚本, 后来在网上找些shell语法的教程来看看(我想大多数同学学习shell也是这么个过程), 觉得shell挺简单的, 比其他语言简单多了. 但是随着写shell脚本次数的增多, 发现根本不是那么回事, 觉得shell太灵异了, 经常出现一些奇怪的错误, 比如: 变量赋值 #!/usr/bin/env [...]]]></description>
			<content:encoded><![CDATA[<div id="table-of-contents">
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 引子 </a></li>
<li><a href="#sec-2">2 语法介绍 </a>
<ul>
<li><a href="#sec-2_1">2.1 定义 </a></li>
<li><a href="#sec-2_2">2.2 管道 </a></li>
<li><a href="#sec-2_3">2.3 引用 (QUOTING) </a></li>
<li><a href="#sec-2_4">2.4 参数 (PARAMETERS) </a></li>
<li><a href="#sec-2_5">2.5 扩展 (EXPANSION) </a></li>
<li><a href="#sec-2_6">2.6 重定向 </a></li>
</ul>
</li>
<li><a href="#sec-3">3 小技巧 </a></li>
<li><a href="#sec-4">4 工具 </a>
<ul>
<li><a href="#sec-4_1">4.1 log4sh </a></li>
<li><a href="#sec-4_2">4.2 shunit </a></li>
<li><a href="#sec-4_3">4.3 bashdb </a></li>
</ul>
</li>
<li><a href="#sec-5">5 shell快捷键 </a></li>
<li><a href="#sec-6">6 shell炸弹 </a></li>
<li><a href="#sec-7">7 shell加密 </a>
<ul>
<li><a href="#sec-7_1">7.1 shc </a></li>
<li><a href="#sec-7_2">7.2 wzsh </a></li>
</ul>
</li>
<li><a href="#sec-8">8 宝典 </a></li>
</ul>
</div>
</div>
<div id="outline-container-1" class="outline-3">
<h3 id="sec-1"><span class="section-number-3">1</span> 引子 </h3>
<div class="outline-text-3" id="text-1">
<p>我06年开始接触shell编程, 一开始照着别人的例子写些简单的脚本, 后来在网上找些shell语法的教程来看看(我想大多数同学学习shell也是这么个过程), 觉得shell挺简单的, 比其他语言简单多了. 但是随着写shell脚本次数的增多, 发现根本不是那么回事, 觉得shell太灵异了, 经常出现一些奇怪的错误, 比如:<span id="more-40851"></span>
</p>
<ol>
<li>
变量赋值</p>
<pre class="src src-sh"><span style="color: #b22222;">#</span><span style="color: #b22222;">!/usr/bin/</span><span style="color: #a020f0;">env</span><span style="color: #b22222;"> bash
</span>
action = <span style="color: #8b2252;">"$1"</span>

<span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"You want $action"</span>
</pre>
<p>
把上面的代码保存为test.sh并加上可执行权限, 执行./test.sh exit, 得到这样的错误提示:
</p>
<pre class="example">
./test.sh: line 3: action: command not found
</pre>
<p>再改下:
</p>
<pre class="src src-sh"><span style="color: #b22222;">#</span><span style="color: #b22222;">!/usr/bin/</span><span style="color: #a020f0;">env</span><span style="color: #b22222;"> bash
</span>
<span style="color: #a0522d;">action</span>= <span style="color: #8b2252;">"$1"</span>

<span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"You want $action"</span>
</pre>
<p>
不错, 好像没错误了, 不过怎么啥都不打印啦? <br/><br />
有过其他语言编程经验的同学可能也会像我犯那样的错误, 同时会产生这样的疑问: 怎么shell中赋个值还这么多麻烦啊?
</p>
</li>
<li>
判断2个字符串是否相等</p>
<pre class="src src-sh"><span style="color: #a020f0;">if</span> [ $<span style="color: #a0522d;">user</span> = <span style="color: #8b2252;">"admin"</span> ]; <span style="color: #a020f0;">then</span>
    <span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"You are admin!"</span>
<span style="color: #a020f0;">fi</span>
</pre>
<p>
上述代码判断一个用户是否为管理员, 但是有时候上面的代码运行时会出现这样的错误:
</p>
<pre class="example">
-bash: [: =: unary operator expected
</pre>
<p>这意思好像是说"期待一元运算符"? 啥意思? <br/><br />
有的教程里指出这样就可以了:
</p>
<pre class="src src-sh"><span style="color: #a020f0;">if</span> [ x$<span style="color: #a0522d;">user</span> = <span style="color: #8b2252;">"xadmin"</span> ]; <span style="color: #a020f0;">then</span>
    <span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"You are admin!"</span>
<span style="color: #a020f0;">fi</span>
</pre>
<p>
我试了下, 好像还真行, 为啥这样就可以呢? 这个&#8221;x&#8221;这么神奇? 其他的字母也可以这么神奇吗? <br/><br />
我看有的人这样写:
</p>
<pre class="src src-sh"><span style="color: #a020f0;">if</span> [ <span style="color: #8b2252;">"x$user"</span> = <span style="color: #8b2252;">"xadmin"</span> ]; <span style="color: #a020f0;">then</span>
</pre>
<p>
还有人这样写:
</p>
<pre class="src src-sh"><span style="color: #a020f0;">if</span> [ x$<span style="color: #a0522d;">user</span> = xadmin ]; <span style="color: #a020f0;">then</span>
</pre>
<p>
甚至有人这样:
</p>
<pre class="src src-sh"><span style="color: #a020f0;">if</span> [ x$<span style="color: #a0522d;">user</span> = x<span style="color: #8b2252;">"admin"</span> ]; <span style="color: #a020f0;">then</span>
</pre>
<p>
后来我无意中发现这样就可以了:
</p>
<pre class="src src-sh"><span style="color: #a020f0;">if</span> [ <span style="color: #8b2252;">"$user"</span> = <span style="color: #8b2252;">"admin"</span> ]; <span style="color: #a020f0;">then</span>
</pre>
<p>
这加不加双引号和加在哪到底有什么不同?
</p>
</li>
<li>
read好使不</p>
<ol>
<li>
echo str | read a, 怎么$a就不是我想要的str呢?
</li>
<li>
假如文件a的第一行是&#8221;str1 TAB str2&#8243;, 执行:</p>
<pre class="src src-sh"><span style="color: #7a378b;">read</span> str &lt; a
<span style="color: #7a378b;">echo</span> $<span style="color: #a0522d;">str</span>
</pre>
<p>
怎么输出是&#8221;str1 str2&#8243;, 而不是&#8221;str1 TAB str2&#8243;呢, 我的TAB哪去了?
</p>
</li>
</ol>
</li>
<li>
神奇的注释 <br/><br />
有人说下面这些符号可以用来注释shell:</p>
<pre class="src src-sh">: &lt;&lt; COMMENT<span style="color: #ffa54f;">
COMMENT
</span></pre>
<p>
比如:
</p>
<pre class="src src-sh"><span style="color: #7a378b;">echo</span> xxx

: &lt;&lt; COMMENT<span style="color: #ffa54f;">
echo "mm said, you could not touch me!"
COMMENT
</span>
<span style="color: #7a378b;">echo</span> yyy
</pre>
<p>
好像还真行, 但是下面这个怎么不行呢?
</p>
<pre class="src src-sh"><span style="color: #7a378b;">echo</span> xxx

: &lt;&lt; COMMENT<span style="color: #ffa54f;">
file=a
result=$(</span><span style="color: #ff00ff;">grep</span><span style="color: #ffa54f;"> str $file)
COMMENT
</span>
<span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"mm said, you can touch me!"</span>
</pre>
<p>
还有其他的神奇注释吗?
</p>
</li>
</ol>
<p>其实类似上面这些灵异的例子还有很多, 但是纵观那些shell教程, 很少有能把shell的这些灵异的地方给读者讲明白的. 下面, 我结合我自己的一些经验, 力图把shell的一些本质语法给大家讲明白, 让大家在遇到一些灵异的问题时, 能迅速的定位和解决问题.
</p>
</div>
</div>
<div id="outline-container-2" class="outline-3">
<h3 id="sec-2"><span class="section-number-3">2</span> 语法介绍 </h3>
<div class="outline-text-3" id="text-2">
</div>
<div id="outline-container-2_1" class="outline-4">
<h4 id="sec-2_1"><span class="section-number-4">2.1</span> 定义 </h4>
<div class="outline-text-4" id="text-2_1">
<ul>
<li id="sec-2_1_1">单词 (word) <br/><br />
一串字符构成一个单词, 也叫token
</li>
</ul>
<ul>
<li id="sec-2_1_2">name (identifier) <br/><br />
仅有字母、数字、下划线构成， 而且由字母或者下划线开头的word叫name, 也叫标识符(identifier)
</li>
</ul>
<ul>
<li id="sec-2_1_3">元字符 (metacharacter) <br/>
<pre class="example">
| &amp; ; ( ) &lt; &gt; space tab
</pre>
<p>这些字符没有被引号引起来时, 可以用来分割单词
</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-2_2" class="outline-4">
<h4 id="sec-2_2"><span class="section-number-4">2.2</span> 管道 </h4>
<div class="outline-text-4" id="text-2_2">
<pre class="example">
command | command2
command |&amp; command2
</pre>
<p>把command的输出通过管道连接到command2的输入, |&amp;连标准错误也一起做为command2的输入.
</p>
<p>
这里要注意的时, command2是在子shell里面执行的, command2对环境所做的改变不会影响到command所在的shell环境. 这就解释了本文开头的问题3.1
</p>
</div>
</div>
<div id="outline-container-2_3" class="outline-4">
<h4 id="sec-2_3"><span class="section-number-4">2.3</span> 引用 (QUOTING) </h4>
<div class="outline-text-4" id="text-2_3">
<p>引用用来去掉某些字符的特殊意义. 比如想使用元字符的字面意义必须对其进行引用.
</p>
<p>
引用有3类: 反斜线引用(\)、单引号引用、双引号引用.
</p>
<p>
单引号引用屏蔽单引号内的任何字符所具有的特殊意义, 包括反斜线(\), 所以单引号引用不能再包含单引号(比较杯具&hellip;)
</p>
<p>
双引号引用中除了 <b>$</b> 、 <b>`</b> 、 <b>\</b> 、 <b>!</b> , 其他特殊字符的意义都被屏蔽.
</p>
<p>
<b>小技巧:</b>
</p>
<ul>
<li>
$&#8217;string&#8217;<br />
这个语法的意思是: string中含有的反斜线及其后的字符会被特殊解释, 比如: \t会被解释成TAB. 这个非常有用, 比如sort的字段分隔符只能是单个字符, 如果想用TAB做字段分隔符的话, 好多人都这样: sort -t &#8221;   &#8220;, 由于好多编辑器会把TAB变成4个空格, 所以这样做经常会出问题, 那现在你可以这样了: sort -t $&#8217;\t&#8217;
</li>
</ul>
</div>
</div>
<div id="outline-container-2_4" class="outline-4">
<h4 id="sec-2_4"><span class="section-number-4">2.4</span> 参数 (PARAMETERS) </h4>
<div class="outline-text-4" id="text-2_4">
<p>参数是用来存储值的实体, 它可以是数字(0, 1, 2 &hellip;)、name、某些特殊字符(@, *, &hellip;). 当参数是一个name时, 也叫变量(variable), 变量赋值:
</p>
<pre class="src src-sh"><span style="color: #a0522d;">name</span>=[value]
</pre>
<p>
等号2边不能有空格, 如果有空格的话, shell解释程序怎么知道你到底是想要运行name命令还是给name赋值呢? 所以的shell的变量赋值才不得不这样&#8221;讲究&#8221;
</p>
<p>
<b>小技巧:</b>
</p>
<ul>
<li>
shell变量也可以 <b>+=</b>
</li>
<li>
在命令之前的变量赋值语句只影响该命令, 比如:</p>
<pre class="src src-sh"><span style="color: #a0522d;">LANG</span>= sort file
</pre>
<p>
上面的命令表示在运行sort file的时候LANG为空, 不会影响其他的后续命令. 你是否还记得这样的代码:
</p>
<pre class="src src-sh"><span style="color: #a0522d;">tmp_LANG</span>=$<span style="color: #a0522d;">LANG</span>
<span style="color: #a0522d;">LANG</span>=zh_CN
codes ...
<span style="color: #a0522d;">LANG</span>=$<span style="color: #a0522d;">tmp_LANG</span>
</pre>
</li>
</ul>
<ul>
<li id="sec-2_4_1">位置参数 (Positional Parameters) <br/><br />
$0, $1, &hellip;</p>
<p>
<b>小技巧:</b>
</p>
<ul>
<li>
怎么重设位置参数? 用set
</li>
<li>
$10可以吗? 用${10}
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-2_4_2">特殊参数 (Special Parameters) <br/>
<ul>
<li id="sec-2_4_2_1">$* <br/>
<pre class="example">
$* == $1 $2 $3 ...
"$*" == "$1c$2c$3...", c为IFS的第一个字符
</pre>
<p>
<b>IFS</b> 参见<a href="#IFS">这里</a>
</p>
</li>
</ul>
<ul>
<li id="sec-2_4_2_2">$@ <br/>
<pre class="example">
$2 == $*
"$@" == "$1" "$2" "$3" ...
</pre>
<p>
<b>$*</b> 和 <b>$@</b> 啥区别? 见后文
</p>
</li>
</ul>
</li>
</ul>
<ul>
<li id="sec-2_4_3">shell内置变量 (Shell Variables) <br/>
<ul>
<li id="sec-2_4_3_1"><a name="sec-2_4_3_1" id="sec-2_4_3_1"></a>IFS <br/><br />
Internal Field Separator, 用来扩展后分割单词, read命令也是用它来分割单词. 默认值为: &lt;space&gt;&lt;tab&gt;&lt;newline&gt;</p>
</li>
</ul>
<ul>
<li id="sec-2_4_3_2">LANG <br/><br />
这个变量控制你的环境所使用的语言(locale), 还有LC_开头的好几个shell变量也控制locale相关的一些方面. 当你sort一个含有中文的文件时, 是不是结果不如你所愿? 试试LANG=C sort</p>
</li>
</ul>
<ul>
<li id="sec-2_4_3_3">PATH <br/><br />
可执行文件的搜索路径</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-2_5" class="outline-4">
<h4 id="sec-2_5"><span class="section-number-4">2.5</span> 扩展 (EXPANSION) </h4>
<div class="outline-text-4" id="text-2_5">
<p>命令行被分割成单词后, 开始执行扩展. 扩展有大括号扩展(brace expansion), 波浪号扩展(tilde expansion), 参数和变量扩展(parameter and variable expansion), 算术扩展(arithmetic expansion), 命令替换(command substitution), 单词分割(word splitting), 路径扩展(pathname expansion). 扩展的优先级也如上所示. 有的系统还支持进程替换(process substitution)
</p>
<ul>
<li id="sec-2_5_1">大括号扩展 <br/>
<pre class="src src-sh"><span style="color: #7a378b;">echo</span> a{b,c}
ab ac

<span style="color: #7a378b;">echo</span> {1..10}
1 2 3 4 5 6 7 8 9 10

<span style="color: #7a378b;">echo</span> {10..1}
10 9 8 7 6 5 4 3 2 1

<span style="color: #7a378b;">echo</span> {1..10..3}
1 4 7 10

<span style="color: #7a378b;">echo</span> {a..f}
a b c d e f

<span style="color: #7a378b;">echo</span> {a..f..2}
a c e
</pre>
</li>
</ul>
<ul>
<li id="sec-2_5_2">波浪号扩展 <br/>
<pre class="src src-sh"><span style="color: #7a378b;">echo</span> ~/sdfa
/home/taoshanwen/sdfa

~+ =&gt; PWD
~- =&gt; OLDPWD
</pre>
</li>
</ul>
<ul>
<li id="sec-2_5_3">参数扩展 <br/><br />
${parameter}, 就是取出parameter的值, 有很多形式:</p>
<ul>
<li>
${parameter:offset}
</li>
<li>
${parameter:offset:length} <br/><br />
对parameter进行substr</p>
</li>
<li>
${parameter#word}
</li>
<li>
${parameter##word} <br/><br />
删掉匹配的前缀</p>
</li>
<li>
${parameter%word}
</li>
<li>
${parameter%%word} <br/><br />
删掉匹配的后缀</p>
</li>
</ul>
<p>还有很多, 详见bash man
</p>
</li>
</ul>
<ul>
<li id="sec-2_5_4">命令替换 <br/><br />
$(command) 或者`command`, 把command的输出做为结果</p>
</li>
</ul>
<ul>
<li id="sec-2_5_5">算术扩展 <br/><br />
$((expression)), 对expression进行算术表达式操作, 例如:</p>
<pre class="src src-sh"><span style="color: #7a378b;">echo</span> $((9 + 8 * 9))
81

<span style="color: #7a378b;">echo</span> $((9 + 8 ** 9))
134217737
</pre>
</li>
</ul>
<ul>
<li id="sec-2_5_6">进程替换 <br/><br />
假如我现在想比较两个目录dir1和dir2中的文件有啥不同, 我想很多人会这样做:</p>
<pre class="src src-sh">ls dir1 &gt; 1
ls dir2 &gt; 2
diff 1 2
</pre>
<p>
但你试试这样:
</p>
<pre class="src src-sh">diff &lt;(ls dir1) &lt;(ls dir2)
</pre>
<p>
是不是也可以? 很神奇吧. 上面的这个语法&lt;(command)就是进程替换. &lt;(command)表示把command的输出生成一个临时文件, 并把这个文件名作为另外一个命令的参数. 对于上面的命令, 就是把&#8221;ls dir1&#8243;命令的输出生成一个临时文件, 并把临时文件名做为diff命令的第一个参数. 再举一个例子:
</p>
<pre class="src src-sh">wget -q -O &gt;(cat) http://baidu.com
</pre>
<p>
wget命令会把下载后的文件保存到文件中去, 但是我们可以用上面的命令不让它保存到文件中去, 而是显示出来. wget的&#8221;-O&#8221;选项后本来应该是一个文件名的参数, 但是我们现在用&gt;(cat)代替, 表示wget下载下来的内容放到一个临时文件中, 然后把这个临时文件名再传给&gt;()里面的cat命令.<br />
灵活运用进程替换, 将会非常的方便, <b>严重推荐</b>
</p>
</li>
</ul>
<ul>
<li id="sec-2_5_7">单词分割 <br/><br />
<b>shell解释器最为重要的一步! shell灵异的来源</b></p>
<p>
上述扩展如果没有双引号扩起来, 扩展完后, shell将会对结果用IFS进行单词分割. 例如:
</p>
<pre class="src src-sh"><span style="color: #a0522d;">str</span>=<span style="color: #8b2252;">"a         b          c"</span>

<span style="color: #7a378b;">echo</span> $<span style="color: #a0522d;">str</span>
a b c

<span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"$str"</span>
a         b          c
</pre>
<p>
为什么加不加双引号结果会迥然不同? 因为没加双引号时, shell会对扩展结果进行单词分割, $str的扩展结果为&#8221;a         b          c&#8221;, 分割后变成3个单词a、b、c, 这3个单词做为echo命令的三个参数, 最终输出结果自然是&#8221;a b c&#8221;了.
</p>
<p>
想起来本文开头的3.2问题了吗? 知道怎么回事了吧?
</p>
<p>
另外, 扩展结果为空的话, 如果没有被双引号或者单引号扩起来的话, 会被删掉. 例如:
</p>
<pre class="src src-sh"><span style="color: #b22222;">#</span><span style="color: #b22222;">!/usr/bin/</span><span style="color: #a020f0;">env</span><span style="color: #b22222;"> bash
</span>
<span style="color: #a0522d;">user</span>=<span style="color: #8b2252;">"$1"</span>

mysql -u $<span style="color: #a0522d;">user</span> db -e <span style="color: #8b2252;">"$sql"</span>
</pre>
<p>
上面这个脚本如果第一个参数为空的话, $user将会被删掉, 从而mysql的用户名会变成db, 正确的代码应该是:
</p>
<pre class="src src-sh">mysql -u <span style="color: #8b2252;">"$user"</span> db -e <span style="color: #8b2252;">"$sql"</span>
</pre>
<p>
那你知道下面这些代码的错误之处了吗?
</p>
<pre class="src src-sh"><span style="color: #a0522d;">str</span>=$(<span style="color: #ff00ff;">cat</span> file)

<span style="color: #a020f0;">for</span> line<span style="color: #a020f0;"> in</span> <span style="color: #8b2252;">"$str"</span>; <span style="color: #a020f0;">do</span>
    <span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"$line"</span>
<span style="color: #a020f0;">done</span>
</pre>
<p>
说到这里, 我们来说说$*和$@的差别. 它们在不加双引号时完全一样, 但是不加双引号时, 他们都有一个问题, 就是扩展会进行单词分割, 如果输入的参数中含有空格, 可能有时候结果就不是我们想要的了, 比如:
</p>
<pre class="src src-sh"><span style="color: #b22222;">#</span><span style="color: #b22222;">!/usr/bin/</span><span style="color: #a020f0;">env</span><span style="color: #b22222;"> bash
</span>
<span style="color: #a020f0;">for</span> i<span style="color: #a020f0;"> in</span> $<span style="color: #a0522d;">*</span>; <span style="color: #a020f0;">do</span>
    <span style="color: #7a378b;">echo</span> $<span style="color: #a0522d;">i</span>
<span style="color: #a020f0;">done</span>
</pre>
<p>
保存上述的程序为test.sh, 该程序想打印每个输入参数,
</p>
<pre class="src src-sh">taoshanwen@taoshanwen-laptop ~$ ./test.sh ab cd ef
ab
<span style="color: #7a378b;">cd</span>
ef

taoshanwen@taoshanwen-laptop ~$ ./test.sh <span style="color: #8b2252;">"ab xx"</span> <span style="color: #8b2252;">"cd yy"</span> <span style="color: #8b2252;">"ef zz"</span>
ab
xx
<span style="color: #7a378b;">cd</span>
yy
ef
zz
</pre>
<p>
上述结果并不是我们想要的, 那怎么取得准确的输入参数呢? &#8220;$@&#8221;可以解决, 你可以试试, <img src='http://ahei.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
</li>
</ul>
<ul>
<li id="sec-2_5_8">路径扩展 <br/><br />
如果当前路径下有文件ab、ac、ad, 那么:</p>
<pre class="src src-sh"><span style="color: #7a378b;">echo</span> a*
ab ac ad
</pre>
</li>
</ul>
<ul>
<li id="sec-2_5_9">删除引用(Quote Removal) <br/><br />
经过上述扩展之后, 对于不是由于上述扩展产生的并且没有被引用的双引号、单引号、反斜线都会被删掉， 例如:</p>
<pre class="src src-sh"><span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"xx"</span> =&gt; xx
<span style="color: #7a378b;">echo</span> a<span style="color: #8b2252;">"xx"</span> =&gt; axx
</pre>
<p>
经过上面这么多的了解, 我们大致知道了shell解释器的解释过程:
</p>
<p>
<img src="screenshots/shell_interpreter.png"  alt="shell_69e8ac756c50863cb4a741bbc96c7b4b47dd5c66.png" />
</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-2_6" class="outline-4">
<h4 id="sec-2_6"><span class="section-number-4">2.6</span> 重定向 </h4>
<div class="outline-text-4" id="text-2_6">
<ul>
<li id="sec-2_6_1">Here Documents <br/>
<pre class="example">
&lt;&lt;[-]word
here-document
delimiter
</pre>
<p>把here-document作为某个命令的标准输入. 例子:
</p>
<pre class="src src-sh">grep a &lt;&lt; EOF<span style="color: #ffa54f;">
asdf
qweszd
asdf
EOF
</span></pre>
<p>
如果word用双引号括住, delimiter就是word删除引用后的结果, here-document里面不进行任何扩展. 如果word没有用双引号括住, 那么here-document里面会进行参数替换、命令替换、算术扩展.
</p>
<p>
我们再来看看本文开头说的那个神奇的注释,
</p>
<pre class="src src-sh">: &lt;&lt; COMMENT<span style="color: #ffa54f;">
COMMENT
</span></pre>
<p>
*&#8221;:&#8221;* 是一个shell内置命令, 它不干任何事情, 它的返回值为0. 这样就好理解了, 被注释的内容实际上是作为 <b>:</b> 的标准输入, 而这个命令啥事情都没干, 起到注释的作用了. 但是你现在知道为啥下面这个没起到注释作用了吗? 咋解决呢?
</p>
<pre class="src src-sh"><span style="color: #7a378b;">echo</span> xxx

: &lt;&lt; COMMENT<span style="color: #ffa54f;">
file=a
result=$(</span><span style="color: #ff00ff;">grep</span><span style="color: #ffa54f;"> str $file)
COMMENT
</span>
<span style="color: #7a378b;">echo</span> <span style="color: #8b2252;">"mm said, you can touch me!"</span>
</pre>
</li>
</ul>
<ul>
<li id="sec-2_6_2">Here Strings <br/>
<pre class="example">
&lt;&lt;&lt; here-strings
</pre>
<p>把word作为命令的标准输入, 例子:<br />
grep a &lt;&lt;&lt; abc
</p>
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-3" class="outline-3">
<h3 id="sec-3"><span class="section-number-3">3</span> 小技巧 </h3>
<div class="outline-text-3" id="text-3">
<ul>
<li>
type <br/><br />
这个内置命令比which强大多了, 可以查找别名、函数、内置命令</p>
<pre class="example">
taoshanwen@taoshanwen-laptop ~$ type ls
ls 是 `ls --<a href="http://ahei.info/t/color" class="st_tag internal_tag" rel="tag" title="标签 color 下的日志">color</a> -N --show-<a href="http://ahei.info/t/control" class="st_tag internal_tag" rel="tag" title="标签 control 下的日志">control</a>-chars' 的别名
ls 是 /bin/ls

taoshanwen@taoshanwen-laptop ~$ type [
[ 是 shell 内嵌
[ 是 /usr/bin/[
</pre>
</li>
<li>
丰富多彩 <br/></p>
<ol>
<li>
grep有个&ndash;color选项, 可以高亮匹配的地方, 非常不错
</li>
<li>
在你的.bashrc里面加入下面的代码:</p>
<pre class="src src-sh"><span style="color: #b22222;"># </span><span style="color: #b22222;">less color configure
</span><span style="color: #b22222;"># </span><span style="color: #b22222;">blue
</span><span style="color: #7a378b;">export</span> <span style="color: #a0522d;">LESS_TERMCAP_mb</span>=$<span style="color: #8b2252;">'\E[01;34m'</span>
<span style="color: #b22222;"># </span><span style="color: #b22222;">red
</span><span style="color: #7a378b;">export</span> <span style="color: #a0522d;">LESS_TERMCAP_md</span>=$<span style="color: #8b2252;">'\E[01;31m'</span>
<span style="color: #b22222;"># </span><span style="color: #b22222;">magenta
</span><span style="color: #7a378b;">export</span> <span style="color: #a0522d;">LESS_TERMCAP_me</span>=$<span style="color: #8b2252;">'\E[01;35m'</span>
<span style="color: #b22222;"># </span><span style="color: #b22222;">write
</span><span style="color: #7a378b;">export</span> <span style="color: #a0522d;">LESS_TERMCAP_<a href="http://ahei.info/t/se" class="st_tag internal_tag" rel="tag" title="标签 se 下的日志">se</a></span>=$<span style="color: #8b2252;">'\E[0m'</span>
<span style="color: #b22222;"># </span><span style="color: #b22222;">yellow
</span><span style="color: #7a378b;">export</span> <span style="color: #a0522d;">LESS_TERMCAP_so</span>=$<span style="color: #8b2252;">'\E[01;44;33m'</span>
<span style="color: #b22222;"># </span><span style="color: #b22222;">cyan
</span><span style="color: #7a378b;">export</span> <span style="color: #a0522d;">LESS_TERMCAP_ue</span>=$<span style="color: #8b2252;">'\E[01;36m'</span>
<span style="color: #b22222;"># </span><span style="color: #b22222;">green
</span><span style="color: #7a378b;">export</span> <span style="color: #a0522d;">LESS_TERMCAP_us</span>=$<span style="color: #8b2252;">'\E[01;32m'</span>
</pre>
<p>
保证你的man会色彩缤纷, 重点突出, 非常方便
</p>
</li>
</ol>
</li>
<li>
[[]]和[]的区别 <br/></p>
<ol>
<li>
[[]]内不进行单词分割和路径扩展, 所以 <a href="#$a=====ab"> $a = ab </a>是可以的. []内则进行所有的扩展, [ $a = ab ]是不保险的.
</li>
<li>
[[]]内的&lt;&gt;是用当前locale做字符串比较的, []内的&lt;&gt;是根据ASCII顺序做比较的, 2者都不是对数字进行比较的, 这个需要注意, 比如可以试试<a href="#3==>==11"> 3 &gt; 11 </a>; echo $?, 是不是返回0? 另外, [只是内置的命令, 所以不能直接[ 3 &lt; 2 ], 这样的话, &lt;是元字符, 当作重定向符号了, 需要对&lt;进行转义, 需要这样 [ 3 "&lt;" 2 ]
</li>
<li>
[[]]的==、!=、=~确实是正则匹配的, 具体用法可以见bash man
</li>
</ol>
</li>
</ul>
</div>
</div>
<div id="outline-container-4" class="outline-3">
<h3 id="sec-4"><span class="section-number-3">4</span> 工具 </h3>
<div class="outline-text-3" id="text-4">
</div>
<div id="outline-container-4_1" class="outline-4">
<h4 id="sec-4_1"><span class="section-number-4">4.1</span> log4sh </h4>
<div class="outline-text-4" id="text-4_1">
<p><a href="http://sourceforge.net/projects/log4sh/" target="_blank">http://sourceforge.net/projects/log4sh/</a>, shell里的日志工具, 和log4系列的其他日志库配置基本差不多
</p>
</div>
</div>
<div id="outline-container-4_2" class="outline-4">
<h4 id="sec-4_2"><span class="section-number-4">4.2</span> shunit </h4>
<div class="outline-text-4" id="text-4_2">
<p><a href="http://shunit.sourceforge.net/" target="_blank">http://shunit.sourceforge.net/</a>, shell的单元测试工具
</p>
</div>
</div>
<div id="outline-container-4_3" class="outline-4">
<h4 id="sec-4_3"><span class="section-number-4">4.3</span> bashdb </h4>
<div class="outline-text-4" id="text-4_3">
<p><a href="http://bashdb.sourceforge.net/" target="_blank">http://bashdb.sourceforge.net/</a>, shell的调试工具
</p>
</div>
</div>
</div>
<div id="outline-container-5" class="outline-3">
<h3 id="sec-5"><span class="section-number-3">5</span> shell快捷键 </h3>
<div class="outline-text-3" id="text-5">
<p><a href="http://ahei.info/bash.htm" target="_blank">高效操作Bash</a>
</p>
</div>
</div>
<div id="outline-container-6" class="outline-3">
<h3 id="sec-6"><span class="section-number-3">6</span> shell炸弹 </h3>
<div class="outline-text-3" id="text-6">
<pre class="example">
 <img src='http://ahei.info/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> ) { <img src='http://ahei.info/wp-includes/images/smilies/icon_neutral.gif' alt=':|' class='wp-smiley' /> :&amp;};:
</pre>
<p>上面的命令能迅速的灭了你的系统, <b>慎用!</b> ulimit -u进行限制
</p>
</div>
</div>
<div id="outline-container-7" class="outline-3">
<h3 id="sec-7"><span class="section-number-3">7</span> shell加密 </h3>
<div class="outline-text-3" id="text-7">
</div>
<div id="outline-container-7_1" class="outline-4">
<h4 id="sec-7_1"><span class="section-number-4">7.1</span> shc </h4>
<div class="outline-text-4" id="text-7_1">
<p><a href="http://www.datsi.fi.upm.es/~frosal/" target="_blank">http://www.datsi.fi.upm.es/~frosal/</a>, 简单的加密工具, 会把shell转换成一个二进制文件
</p>
</div>
</div>
<div id="outline-container-7_2" class="outline-4">
<h4 id="sec-7_2"><span class="section-number-4">7.2</span> wzsh </h4>
<div class="outline-text-4" id="text-7_2">
<p><a href="http://wzce.tripod.com/wzsh.html" target="_blank">http://wzce.tripod.com/wzsh.html</a>, 更加强大的加密工具
</p>
</div>
</div>
</div>
<div id="outline-container-8" class="outline-3">
<h3 id="sec-8"><span class="section-number-3">8</span> 宝典 </h3>
<div class="outline-text-3" id="text-8">
<ul>
<li>
<a href="http://www.linuxsir.org/main/doc/abs/abs3.7cnhtm/index.html" target="_blank">高级Bash脚本编程指南</a>
</li>
<li>
bash man, <a href="http://ahei.info/chinese-bash-man.htm" target="_blank">中文bash man</a>
</li>
</ul>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/shell.htm/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>高效操作Bash</title>
		<link>http://ahei.info/bash.htm</link>
		<comments>http://ahei.info/bash.htm#comments</comments>
		<pubDate>Sat, 18 Dec 2010 14:37:12 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[中级]]></category>

		<guid isPermaLink="false">http://ahei.info/?p=40838</guid>
		<description><![CDATA[我们在平常工作中大量使用linux, 而使用linux的过程中操作Bash更是非常之频繁, 所以怎样高效的操作Bash是一个非常重要的问题. 下面我结合自己的经验总结一下高效操作Bash的一些技巧. 1 快捷键 1.1 注意 1.2 重度推荐 1.3 常用快捷键 1.4 高级快捷键 1.5 总结 2 历史扩展 2.1 概念 2.2 事件指示器(Event Designators) 2.3 单词指示器(Word Designators) 2.4 修饰符(Modifiers) 2.5 例子 2.6 总结 3 shell技巧 3.1 Here Documents 3.2 Here Strings 3.3 进程替换(Process Substitution) 4 广告 1 快捷键 1.1 注意 本文的快捷键表示中, C 表示Ctrl键, M表示Alt健. 这些快捷键中, 有一个小规律, [...]]]></description>
			<content:encoded><![CDATA[<p>
我们在平常工作中大量使用linux, 而使用linux的过程中操作Bash更是非常之频繁, 所以怎样高效的操作Bash是一个非常重要的问题. 下面我结合自己的经验总结一下高效操作Bash的一些技巧.<span id="more-40838"></span>
</p>
<div id="table-of-contents">
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 快捷键 </a>
<ul>
<li><a href="#sec-1_1">1.1 注意 </a></li>
<li><a href="#sec-1_2">1.2 重度推荐 </a></li>
<li><a href="#sec-1_3">1.3 常用快捷键 </a></li>
<li><a href="#sec-1_4">1.4 高级快捷键 </a></li>
<li><a href="#sec-1_5">1.5 总结 </a></li>
</ul>
</li>
<li><a href="#sec-2">2 历史扩展 </a>
<ul>
<li><a href="#sec-2_1">2.1 概念 </a></li>
<li><a href="#sec-2_2">2.2 事件指示器(Event Designators) </a></li>
<li><a href="#sec-2_3">2.3 单词指示器(Word Designators) </a></li>
<li><a href="#sec-2_4">2.4 修饰符(Modifiers) </a></li>
<li><a href="#sec-2_5">2.5 例子 </a></li>
<li><a href="#sec-2_6">2.6 总结 </a></li>
</ul>
</li>
<li><a href="#sec-3">3 shell技巧 </a>
<ul>
<li><a href="#sec-3_1">3.1 Here Documents </a></li>
<li><a href="#sec-3_2">3.2 Here Strings </a></li>
<li><a href="#sec-3_3">3.3 进程替换(Process Substitution) </a></li>
</ul>
</li>
<li><a href="#sec-4">4 广告 </a></li>
</ul>
</div>
</div>
<div id="outline-container-1" class="outline-3">
<h3 id="sec-1"><span class="section-number-3">1</span> 快捷键 </h3>
<div class="outline-text-3" id="text-1">
</div>
<div id="outline-container-1_1" class="outline-4">
<h4 id="sec-1_1"><span class="section-number-4">1.1</span> 注意 </h4>
<div class="outline-text-4" id="text-1_1">
<p>本文的快捷键表示中, <b>C</b> 表示Ctrl键, M表示Alt健. 这些快捷键中, 有一个小规律, 对字符操作一般是C开头, 对单词操作一般是M开头. 如果你用SecureCRT, 默认的话, 会输入不了Alt开头的快捷键, 因为Alt被当作菜单快捷键了, 可以点 选项 -&gt; 回话选项, 选择tab 终端-&gt;仿真-&gt;<a href="http://ahei.info/c-emacs.htm" class="st_tag internal_tag" rel="tag" title="标签 Emacs 下的日志">Emacs</a>, 把&#8221;使用Alt键作为元键&#8221;打勾. 如果你用gnome-terminal, 默认状态下也输入不了Alt开头的快捷键，也被当作菜单快捷键了，可以点 编辑 -&gt; 键盘快捷键, 把＂启用菜单快捷键＂前面的勾去掉.<br />
下面的快捷键中很多以Ctrl键开头, 很多键盘的Ctrl键并不是很好按, 可以尝试把<a href="http://emacser.com/capslocak.htm" target="_blank">Ctrl键和Capslock键交换</a>.
</p>
</div>
</div>
<div id="outline-container-1_2" class="outline-4">
<h4 id="sec-1_2"><span class="section-number-4">1.2</span> 重度推荐 </h4>
<div class="outline-text-4" id="text-1_2">
<ul>
<li id="sec-1_2_1">C-r <br/><br />
有时候，如果你想重新输入以前输入过的某条命令怎么办? 我见过两种做法：</p>
<ol>
<li>
不停的按向上方向键，试图找出那条命令
</li>
<li>
输入history命令，然后找到那条命令，或者grep一把history命令的输出
</li>
</ol>
<p>其实, 你有更好的选择, 那就是按 <b>C-r</b>, 然后输入你想要的命令中含有的单词, 就会出现含有这个单词的命令, 如果它不是你想要的命令, 就继续按C-r, 知道出现你想要的命令为止. C-r效果: <br/>
</p>
<pre class="example">
(reverse-i-search)`ls': ls a b c
</pre>
</li>
</ul>
<ul>
<li id="sec-1_2_2">M-. <br/><br />
我经常见别人用mkdir long-long-long-name-dir后, 再输入cd, 后面跟那个长的不能再长的目录名, 这时候我就会告诉他, 其实你输入完cd后, 可以按M-., 就可以自动输入那个长的不能再长的目录名了. 其实, M-.的真正作用就是把上一条命令的最后一个参数输入到当前命令行. <b>非常非常之方便, 强烈推荐</b>. 如果继续按M-., 会把上上条命令的最后一个参数拿过来. 同样, 如果你想把上一条命令第一个参数拿过来咋办呢? 用M-0 M-., 就是先输入M-0, 再输入M-.. 如果是上上条命令的第一个参数呢? 当然是M-0 M-. M-.了.</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-1_3" class="outline-4">
<h4 id="sec-1_3"><span class="section-number-4">1.3</span> 常用快捷键 </h4>
<div class="outline-text-4" id="text-1_3">
<ul>
<li id="sec-1_3_1">程序控制 <br/><br />
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">意义</th>
<th scope="col" class="left">快捷键</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">终止当前在前台运行的程序</td>
<td class="left">C-c</td>
</tr>
<tr>
<td class="left">挂起当前在前台运行的程序</td>
<td class="left">C-z</td>
</tr>
<tr>
<td class="left">如果光标在行首且当前行没有输入任何字符, C-d会退出当前会话</td>
<td class="left">C-d</td>
</tr>
</tbody>
</table>
</li>
</ul>
<ul>
<li id="sec-1_3_2">光标移动 <br/><br />
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">意义</th>
<th scope="col" class="left">快捷键</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">向前(Forward)移动一个字符</td>
<td class="left">C-f</td>
</tr>
<tr>
<td class="left">向后(Backward)移动一个字符</td>
<td class="left">C-b</td>
</tr>
<tr>
<td class="left">向前移动一个单词</td>
<td class="left">M-f</td>
</tr>
<tr>
<td class="left">向后移动一个单词</td>
<td class="left">M-b</td>
</tr>
<tr>
<td class="left">移动光标到行首</td>
<td class="left">C-a</td>
</tr>
<tr>
<td class="left">移动光标到行尾</td>
<td class="left">C-e</td>
</tr>
</tbody>
</table>
</li>
</ul>
<ul>
<li id="sec-1_3_3">编辑 <br/><br />
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">意义</th>
<th scope="col" class="left">快捷键</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">向前删一个字符</td>
<td class="left">C-d</td>
</tr>
<tr>
<td class="left">向后删一个字符</td>
<td class="left">C-h</td>
</tr>
<tr>
<td class="left">向前删一个单词</td>
<td class="left">M-d</td>
</tr>
<tr>
<td class="left">向后删一个单词, 单词之间以符号分割</td>
<td class="left">C-M-h</td>
</tr>
<tr>
<td class="left">向后删一个单词, 单词之间以空格分割</td>
<td class="left">C-w</td>
</tr>
<tr>
<td class="left">清屏, 相当于命令clear, 有了这个快捷键, 就不用每次努力的敲clear了</td>
<td class="left">C-l</td>
</tr>
<tr>
<td class="left">删除当前光标到行尾的字符</td>
<td class="left">C-k</td>
</tr>
<tr>
<td class="left">删除当前光标到行首的字符</td>
<td class="left">C-u</td>
</tr>
<tr>
<td class="left">粘贴删除环里面的第一项</td>
<td class="left">C-y</td>
</tr>
<tr>
<td class="left">粘贴删除环里面的后面的项</td>
<td class="left">M-y</td>
</tr>
<tr>
<td class="left">undo</td>
<td class="left">C-/</td>
</tr>
<tr>
<td class="left">取出上一条命令的最后一个参数</td>
<td class="left">M-.</td>
</tr>
</tbody>
</table>
<p>
对于C-M-h和C-w的区别, 看下面这个例子: <br/><br />
如果当前光标前面的字符串为&#8221;abc def-ghi&#8221;, C-M-h会删掉ghi, 但是C-w会删掉&#8221;def-ghi&#8221;, 也就是说, C-M-h向后删的时候碰到非字母和数字就会停止, 但是C-w碰到空格才会停止. <br/>
</p>
<p>
Bash下有一个删除环(kill-ring), 所有被删除的东西(用C-d删除的字符不算)都会进入这个环, C-y会粘贴环里面最近进去的项, 想要粘贴后面的项, 必须在按C-y后, 不停的按M-y, 直到出来你想要的项为止. <br/>
</p>
<p>
有时候, 你想搜索某个文件中是否有TAB键, 你这时候会怎么做呢? 你或许会用grep, 在你输入完grep后, 你再按TAB, 这时候会出来什么? 什么都没出现! 再按? 出来:
</p>
<pre class="example">
Display all N possibilities? (y or n)
</pre>
<p>这是为何呢? 因为TAB是补全键. 那么是否是输入不了TAB吗? 不是! 按C-v后, 再按TAB即可. 同样, 想输入C-a, C-b也是同样的道理. <br/>
</p>
</li>
</ul>
<ul>
<li id="sec-1_3_4">历史命令操作 <br/><br />
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">意义</th>
<th scope="col" class="left">快捷键</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">从历史命令列表中取下一条命令, 相当于向下方向键</td>
<td class="left">C-n</td>
</tr>
<tr>
<td class="left">从历史命令列表中取上一条命令, 相当于向上方向键</td>
<td class="left">C-p</td>
</tr>
<tr>
<td class="left">向后增量搜索历史命令, <b>非常方便</b>, 严重推荐, 有了它, 以前输入过的很长的命令, 可以不用重复输入</td>
<td class="left">C-r</td>
</tr>
<tr>
<td class="left">循环执行历史命令</td>
<td class="left">C-o</td>
</tr>
</tbody>
</table>
<p>
用C-p取出历史命令列表中某一个命令后, 按C-o可以在这条命令到历史命令列表后面的命令之间循环执行命令, 比如历史命令列表中有50条命令, 后面三项分别是命令A, 命令B, 命令C, 用C-p取出命令A后, 再按C-o就可以不停的在命令A, 命令B, 命令C中循环执行这三个命令. C-o有一个非常好用的地方, 比如用cp命令在拷贝一个大目录的时候, 你肯定很想知道当前的拷贝进度, 那么你现在该怎样做呢? 估计很多人会想到不停的输入du -sh dir去执行, 但用C-o可以非常完美的解决这个问题, 方法就是:
</p>
<ol>
<li>
输入du -sh dir, 按回车执行命令
</li>
<li>
C-p, C-o, 然后就可以不停的按C-o了, 会不停的执行du -sh dir这条命令
</li>
</ol>
<p>其实上面这个问题也可以用watch命令解决:
</p>
<pre class="src src-sh">watch -n 1 -d du -sh dir
</pre>
</li>
</ul>
</div>
</div>
<div id="outline-container-1_4" class="outline-4">
<h4 id="sec-1_4"><span class="section-number-4">1.4</span> 高级快捷键 </h4>
<div class="outline-text-4" id="text-1_4">
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<caption></caption>
<colgroup>
<col class="left" />
<col class="left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="left">意义</th>
<th scope="col" class="left">快捷键</th>
</tr>
</thead>
<tbody>
<tr>
<td class="left">从当前光标处向前搜索字符</td>
<td class="left">C-]</td>
</tr>
<tr>
<td class="left">从当前光标处向后搜索字符</td>
<td class="left">C-M-]</td>
</tr>
<tr>
<td class="left">交换当前光标下的字符和光标前面的一个字符, 交换后, 光标向后移东一个字符</td>
<td class="left">C-t</td>
</tr>
<tr>
<td class="left">交换当前光标所在单词和光标前面一个单词, 交换后, 光标向后移动一个单词</td>
<td class="left">M-t</td>
</tr>
<tr>
<td class="left">把单词首字符变成大写, 其他变成小写</td>
<td class="left">M-c</td>
</tr>
<tr>
<td class="left">把单词变成小写</td>
<td class="left">M-l</td>
</tr>
<tr>
<td class="left">把单词变成大写</td>
<td class="left">M-u</td>
</tr>
<tr>
<td class="left">删除当前光标前面所有的空白字符</td>
<td class="left">M-\</td>
</tr>
<tr>
<td class="left">向后非增量搜索历史命令</td>
<td class="left">M-p</td>
</tr>
<tr>
<td class="left">相当于TAB健</td>
<td class="left">C-i</td>
</tr>
<tr>
<td class="left">相当于回车键</td>
<td class="left">C-m/C-j</td>
</tr>
<tr>
<td class="left">在当前光标处和上一次光标处不停的移动</td>
<td class="left">C-x C-x</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-1_5" class="outline-4">
<h4 id="sec-1_5"><span class="section-number-4">1.5</span> 总结 </h4>
<div class="outline-text-4" id="text-1_5">
<p>其实, 上面所说的快捷键并不是由Bash来控制的, 而是有一个叫<a href="http://www.gnu.org/software/readline/" target="_blank"><b>readline</b></a>的库来控制的, readline库用在很多地方, 比如gdb, mysql, 你使用gdb的时候, 是不是很奇怪, 为啥它也能用上下方向键取出前面后面的命令? 因为它用的也是readline库. 所以只要掌握了readline, 就掌握了Bash, gdb, mysql等程序里面的快捷键操作技巧. readline是一个非常非常强悍的库, 它有两种模式, 一个是<a href="http://emacser.com" target="_blank">Emacs</a>模式, 另外一个是vi模式, Emacs模式非常适合在命令行下使用, 我上面说的快捷键都是针对Emacs模式来说的. readline的Emacs模式下的光标移动, 编辑等快捷键和Emacs下的快捷键也非常相近. 所以你学会了这些快捷键, 也快入门Emacs了, <img src='http://ahei.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . readline也可以自定义快捷键, 它还有一套配置语法. 关于它的详细介绍, 可以man readline或者info readline, 也可以看看大牛王垠写的<a href="http://docs.huihoo.com/homepage/shredderyin/readline.html" target="_blank">readline介绍</a>.
</p>
</div>
</div>
</div>
<div id="outline-container-2" class="outline-3">
<h3 id="sec-2"><span class="section-number-3">2</span> 历史扩展 </h3>
<div class="outline-text-3" id="text-2">
</div>
<div id="outline-container-2_1" class="outline-4">
<h4 id="sec-2_1"><span class="section-number-4">2.1</span> 概念 </h4>
<div class="outline-text-4" id="text-2_1">
<p>首先举个例子:<br />
首先输入一条命令:
</p>
<pre class="src src-sh">ls abc def ghi
</pre>
<p>
再输入:
</p>
<pre class="src src-sh">!!*:s/b/d
</pre>
<p>
那么实际上执行的命令是:
</p>
<pre class="src src-sh">adc def ghi
</pre>
<p>
我来解释一下, !!表示从命令历史列表中取上一条历史命令&#8221;ls abc def ghi&#8221;, *表示选择取刚才选择的命令的所有参数, 即: &#8220;abc def ghi&#8221;, :s/b/d表示对刚才取出来的参数&#8221;abc def ghi&#8221;进行替换, 把第一个出现的b替换成d <br/><br />
从上面可以看出, 操作历史命令分为三步: <br/>
</p>
<ul>
<li>
首先从历史命令列表中选择某条命令, 被选择到的命令被称作 <b>事件(event)</b> (对应上面的!!)
</li>
<li>
再从选择好的事件中选择一部分单词(words), 事件中的每个单词以空格分割(对应上面的*)
</li>
<li>
最后对选择好的一部分单词进行修改(Modifiers)
</li>
</ul>
</div>
</div>
<div id="outline-container-2_2" class="outline-4">
<h4 id="sec-2_2"><span class="section-number-4">2.2</span> 事件指示器(Event Designators) </h4>
<div class="outline-text-4" id="text-2_2">
<p>事件指示器用来从历史命令列表中选择一条命令, 也就是选择事件 <br/>
</p>
<ul>
<li>
!n <br/><br />
选择历史命令列表中第n条命令
</li>
<li>
!-n <br/><br />
选择倒数第n条命令
</li>
<li>
!! <br/><br />
选择上一条命令, 相当于!-1, 和 <b>C-p</b> 的作用也一样
</li>
<li>
!string <br/><br />
选择最近的以string开头的命令
</li>
<li>
!?string[?] <br/><br />
选择最近的包含string的命令, 如果该指示器后面是换行符, 则可以不用输入结尾的&#8221;?&#8221;
</li>
<li>
^string1^string2 <br/><br />
取上一条命令, 并把第一个出现的string1替换成string2
</li>
<li>
!# <br/><br />
引用目前输入的所有命令, 比如输入:</p>
<pre class="src src-sh">more a !#
</pre>
<p>
那么最终执行的命令就是:
</p>
<pre class="src src-sh">more a more a
</pre>
</li>
</ul>
</div>
</div>
<div id="outline-container-2_3" class="outline-4">
<h4 id="sec-2_3"><span class="section-number-4">2.3</span> 单词指示器(Word Designators) </h4>
<div class="outline-text-4" id="text-2_3">
<p>单词指示器用来从被选择好的事件中选择一部分单词, 单词指示器必须以冒号(:)和事件指示器分割开来, 除非单词指示器以^, $, *, -, %开头 <br/>
</p>
<ul>
<li>
0 <br/><br />
选择第0个word, 也就是命令. 假如事件为&#8221;ls abc&#8221;, 那么单词指示器0选择的word即为&#8221;ls&#8221;
</li>
<li>
n <br/><br />
选择第n个word
</li>
<li>
^ <br/><br />
选择命令的第一个参数, 也就是第一个word, 相当于单词指示器1
</li>
<li>
$ <br/><br />
选择命令的最后一个参数
</li>
<li>
% <br/><br />
选择最近的与 &#8220;?string?&#8221; 搜索相匹配的单词
</li>
<li>
x-y <br/><br />
选择第x到第y个word, -y表示0-y
</li>
<li>
* <br/><br />
选择命令的所有参数, 相当于1-$
</li>
<li>
x* <br/><br />
x-$的缩写
</li>
<li>
x- <br/><br />
类似x*, 不过不包含最后一个word. -选择除最后一个word外所有的words
</li>
</ul>
</div>
</div>
<div id="outline-container-2_4" class="outline-4">
<h4 id="sec-2_4"><span class="section-number-4">2.4</span> 修饰符(Modifiers) </h4>
<div class="outline-text-4" id="text-2_4">
<p>对选择的单词进行修改, 修饰符可以出现多次, 每个修饰符要以冒号开头 <br/>
</p>
<ul>
<li>
p <br/><br />
打印新命令, 但不执行
</li>
<li>
s/old/new/<br />
把 <b>第一次出现的</b> old替换成new, 如果分隔符&#8221;/&#8221;是最后一个字符的话, 可以省略. 就像sed中一样, 分隔符&#8221;/&#8221;可以用其他字符代替, 比如s:old:new:. new中出现的&amp;将被old代替. 如果old省略, 那么就用上一次替换用的old代替.
</li>
<li>
&amp; <br/><br />
重复上一次替换
</li>
<li>
g <br/><br />
使修饰符所做的修改应用于整个选择的单词. 类似于sed中的s命令最后的g, 可配合:s和:&amp;修饰符使用, 比如:gs/old/new则对整个事件进行替换.
</li>
<li>
a <br/><br />
和g作用一样
</li>
<li>
G <br/><br />
使后面的:s修饰符对每个word只替换一次
</li>
</ul>
</div>
</div>
<div id="outline-container-2_5" class="outline-4">
<h4 id="sec-2_5"><span class="section-number-4">2.5</span> 例子 </h4>
<div class="outline-text-4" id="text-2_5">
<ul>
<li id="sec-2_5_1">例一 <br/><br />
从别的机器的一个目录拷贝一个a.log文件, 执行:</p>
<pre class="src src-sh">scp user@machine:/home/user/a/a.log .
</pre>
<p>
后来执行:
</p>
<pre class="src src-sh">ls a.log
rm -rf a.log
</pre>
<p>
这时候再想拷贝一下b/b.log, 这时候就可以这样做:
</p>
<pre class="src src-sh">!scp:gs/a/b
</pre>
<p>
如果只想看看用历史扩展出来的命令, 那可以这样:
</p>
<pre class="src src-sh">!scp:gs/a/b/:p
</pre>
</li>
</ul>
<ul>
<li id="sec-2_5_2">例二 <br/><br />
从别的机器同时拷贝a/a.log和b/b.log:</p>
<pre class="src src-sh">scp user@mbchine:/home/user/a/a.log . &amp;&amp; !#-:gs/a/b
</pre>
<p>
上面的!#为事件指示器, 选择前面已经输入的命令&#8221;scp user@mbchine:/home/user/a/a.log . &amp;&amp;&#8221;, &#8220;-&#8221;为单词指示器, 选择除最后一个word, 即&#8221;&amp;&amp;&#8221;外的所有words, 也就是&#8221;scp user@mbchine:/home/user/a/a.log . &#8220;, 最后的&#8221;:gs/a/b&#8221;为修饰符, 对刚才选择的words进行全局替换, 把a替换成b, 最后就成了&#8221;scp user@mbchine:/home/user/b/b.log .&#8221;, 那么最终命令也就成了&#8221;scp user@mbchine:/home/user/a/a.log . &amp;&amp; scp user@mbchine:/home/user/b/b.log .&#8221;
</p>
</li>
</ul>
</div>
</div>
<div id="outline-container-2_6" class="outline-4">
<h4 id="sec-2_6"><span class="section-number-4">2.6</span> 总结 </h4>
<div class="outline-text-4" id="text-2_6">
<p>上面的例子都可以用前面所说的快捷键完成, 不过灵活利用历史扩展有时候还是能更高效的完成同样的事情
</p>
</div>
</div>
</div>
<div id="outline-container-3" class="outline-3">
<h3 id="sec-3"><span class="section-number-3">3</span> shell技巧 </h3>
<div class="outline-text-3" id="text-3">
</div>
<div id="outline-container-3_1" class="outline-4">
<h4 id="sec-3_1"><span class="section-number-4">3.1</span> Here Documents </h4>
<div class="outline-text-4" id="text-3_1">
<pre class="example">
&lt;&lt;[-]word
here-documents
delimiter
</pre>
<p>把here-documents作为某个命令的标准输入, 例子:
</p>
<pre class="src src-sh">grep a <span style="color: #00ffff;">&lt;&lt;</span> EOF<span style="color: #ff1493;">
asdf
qweszd
asdf
EOF
</span></pre>
</div>
</div>
<div id="outline-container-3_2" class="outline-4">
<h4 id="sec-3_2"><span class="section-number-4">3.2</span> Here Strings </h4>
<div class="outline-text-4" id="text-3_2">
<pre class="example">
&lt;&lt;&lt; here-strings
</pre>
<p>把word作为命令的标准输入, 例子:<br />
grep a &lt;&lt;&lt; abc
</p>
</div>
</div>
<div id="outline-container-3_3" class="outline-4">
<h4 id="sec-3_3"><span class="section-number-4">3.3</span> 进程替换(Process Substitution) </h4>
<div class="outline-text-4" id="text-3_3">
<p>假如我现在想比较两个目录dir1和dir2中的文件有啥不同, 我想很多人会这样做:
</p>
<pre class="src src-sh">ls dir1 <span style="color: #00ffff;">&gt;</span> 1
ls dir2 <span style="color: #00ffff;">&gt;</span> 2
diff 1 2
</pre>
<p>
但你试试这样:
</p>
<pre class="src src-sh">diff <span style="color: #00ffff;">&lt;</span><span style="color: #6495ed;">(</span>ls dir1<span style="color: #6495ed;">)</span> <span style="color: #00ffff;">&lt;</span><span style="color: #6495ed;">(</span>ls dir2<span style="color: #6495ed;">)</span>
</pre>
<p>
是不是也可以? 很神奇吧. 上面的这个语法&lt;(command)就是进程替换. &lt;(command)表示把command的输出生成一个临时文件, 并把这个文件名作为另外一个命令的参数. 对于上面的命令, 就是把&#8221;ls dir1&#8243;命令的输出生成一个临时文件, 并把临时文件名做为diff命令的第一个参数. 再举一个例子:
</p>
<pre class="src src-sh">wget -q -O <span style="color: #00ffff;">&gt;</span><span style="color: #6495ed;">(</span>cat<span style="color: #6495ed;">)</span> <a href="http://baidu.com">http://baidu.com</a>
</pre>
<p>
wget命令会把下载后的文件保存到文件中去, 但是我们可以用上面的命令不让它保存到文件中去, 而是显示出来. wget的&#8221;-O&#8221;选项后本来应该是一个文件名的参数, 但是我们现在用&gt;(cat)代替, 表示wget下载下来的内容放到一个临时文件中, 然后把这个临时文件名再传给&gt;()里面的cat命令.<br />
灵活运用进程替换, 将会非常的方便, <b>严重推荐</b>
</p>
</div>
</div>
</div>
<div id="outline-container-4" class="outline-3">
<h3 id="sec-4"><span class="section-number-3">4</span> 广告 </h3>
<div class="outline-text-3" id="text-4">
<p>呵呵, 最后做一点小广告, 这篇文章是在Emacs Org Mode下写的(本文最后一句话, HTML generated by org-mode 7.3 in emacs 23, 你看到了吗), <a href="http://orgmode.org/" target="_blank">Org Mode</a>是Emacs内置的一个非常强悍非常强悍的Mode, 是实践<a href="http://zh.wikipedia.org/zh/GTD" target="_blank">GTD</a>最好的工具, 它的功能包括但不限于: <b>时间管理</b>, 做笔记, 用原始的文本格式html/pdf/latex, 画流程图等. 可以看看这几篇文章以引起你的兴趣: <a href="http://emacser.com/org-mode.htm" target="_blank">Emacs org mode学习笔记</a>, <a href="http://emacser.com/emacs-ditaa.htm" target="_blank">Emacs中绘图 － ditaa篇</a>, <a href="http://emacser.com/emacs-simple-use.htm" target="_blank">Emacs － 普通人的编辑利器</a>.
</p>
</div>
</div>
<div id="postamble">
<p class="creator">HTML generated by org-mode 7.3 in emacs 23</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/bash.htm/feed</wfw:commentRss>
		<slash:comments>51</slash:comments>
		</item>
		<item>
		<title>nutch的分布式抓取</title>
		<link>http://ahei.info/nutch-distributed-crawl.htm</link>
		<comments>http://ahei.info/nutch-distributed-crawl.htm#comments</comments>
		<pubDate>Fri, 12 Feb 2010 15:26:30 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[Nutch]]></category>
		<category><![CDATA[中级]]></category>
		<category><![CDATA[分布式]]></category>
		<category><![CDATA[搜索引擎]]></category>
		<category><![CDATA[ahei]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[crawl]]></category>
		<category><![CDATA[crawler]]></category>
		<category><![CDATA[DEA]]></category>
		<category><![CDATA[Hadoop]]></category>
		<category><![CDATA[hdfs]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mapreduce]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[readlink]]></category>
		<category><![CDATA[se]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[ssh-copy-id]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[top]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[vi]]></category>
		<category><![CDATA[抓取]]></category>
		<category><![CDATA[插件]]></category>
		<category><![CDATA[配置]]></category>
		<category><![CDATA[配置文件]]></category>

		<guid isPermaLink="false">http://emacser.com/?p=40688</guid>
		<description><![CDATA[前段时间我写了一篇文章讲nutch的简单使用，是单台机器抓取，今天我讲一下nutch的分布式抓取。 由于nutch的分布式是采用hadoop，所以nutch的分布式抓取主要涉及到hadoop和nutch本身两方面的配置。 hadoop的配置 hadoop的配置主要涉及到以下几个文件: hadoop-env.sh hadoop-env.sh里面是一些hadoop脚本文件需要用到的环境变量。 JAVA_HOME hadoop-env.sh中最重要的选项是JAVA_HOME, 如果这个选项没有设置的话，而且你的系统也没有设置这个环境变量的话，运行hadoop脚本的时候会出现下面的错误提示： ?View Code TEXTError: JAVA_HOME is not set. 我改了一下hadoop脚本，当你没有设置JAVA_HOME的时候，可以通过&#8221;which java&#8221;命令来自动设置JAVA_HOME，代码如下： ?View Code BASH1 2 3 if &#91;&#91; -z &#34;$JAVA_HOME&#34; &#93;&#93;; then JAVA_HOME=`cd $&#40;dirname $&#40;readlink -m $&#40;which java&#41;&#41;&#41;/../../ &#38;&#38; pwd` fi 原理很简单，首先用which命令找到java命令的路径，然后用readlink命令得到软链接指向的真正目录，而java命令一般都在$JAVA_HOME/jre/bin/下，所以得到java命令的目录就知道了JAVA_HOME了。这个方法在ubuntu下一般都有效，但是在gentoo下无效，gentoo下java命令是/usr/bin/run-java-tool。 HADOOP_SSH_OPTS 这个选项是传给ssh的。由于hadoop在启动集群内别的机器上的hadoop程序的时候，是通过ssh来操作的，所以你可以通过设置这个选项来控制ssh的选项。ssh登录到别的机器的时候，如果目标机器没有经过你的认证，即它的key不在你的~/.ssh/known_hosts里面，你就会得到如下的提示： ?View Code TEXT1 2 3 The authenticity of host 'aheiu (172.0.1.208)' can't be established. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="Nutch" src="screenshots/nutch-logo.gif"/></p>
<p>前段时间我写了一篇文章讲<a href="nutch-tutorial.htm" target="_blank">nutch的简单使用</a>，是单台机器抓取，今天我讲一下nutch的分布式抓取。</p>
<p>由于nutch的分布式是采用hadoop，所以nutch的分布式抓取主要涉及到hadoop和nutch本身两方面的配置。<span id="more-40688"></span></p>
<h4>hadoop的配置</h4>
<p>hadoop的配置主要涉及到以下几个文件:
<ul>
<li>hadoop-env.sh<br />
    hadoop-env.sh里面是一些hadoop脚本文件需要用到的环境变量。
<ol>
<li><a href="http://ahei.info/t/java" class="st_tag internal_tag" rel="tag" title="标签 java 下的日志">JAVA</a>_HOME<br />
    hadoop-env.sh中最重要的选项是JAVA_HOME, 如果这个选项没有设置的话，而且你的系统也没有设置这个环境变量的话，运行hadoop脚本的时候会出现下面的错误提示：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code18'); return false;">View Code</a> TEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068818"><td class="code" id="p40688code18"><pre class="text" style="font-family:monospace;">Error: JAVA_HOME is not set.</pre></td></tr></table></div>

<p>      我改了一下hadoop脚本，当你没有设置JAVA_HOME的时候，可以通过&#8221;which java&#8221;命令来自动设置JAVA_HOME，代码如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code19'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068819"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p40688code19"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$JAVA_HOME</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">cd</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">readlink</span> <span style="color: #660033;">-m</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> java<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;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p>      原理很简单，首先用which命令找到java命令的路径，然后用readlink命令得到软链接指向的真正目录，而java命令一般都在$JAVA_HOME/jre/bin/下，所以得到java命令的目录就知道了JAVA_HOME了。这个方法在ubuntu下一般都有效，但是在gentoo下无效，gentoo下java命令是/usr/bin/run-java-tool。</li>
<li>HADOOP_SSH_OPTS<br />
        这个选项是传给ssh的。由于hadoop在启动集群内别的机器上的hadoop程序的时候，是通过ssh来操作的，所以你可以通过设置这个选项来控制ssh的选项。ssh登录到别的机器的时候，如果目标机器没有经过你的认证，即它的key不在你的~/.ssh/known_hosts里面，你就会得到如下的提示：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code20'); return false;">View Code</a> TEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068820"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p40688code20"><pre class="text" style="font-family:monospace;">The authenticity of host 'aheiu (172.0.1.208)' can't be established.
RSA key fingerprint is fa:e0:57:4e:6a:1d:e3:3e:49:86:8f:13:e5:45:47:f0.
Are you sure you want to continue connecting (yes/no)?</pre></td></tr></table></div>

<p>        这时候你必须输入yes才能继续。这样就需要人工的干预了。那么怎样才能做到自动化呢？<br />
        ssh有个选项StrictHostKeyChecking, 这个选项控制当目标主机没有进行过认证的时候，是否显示上面的信息，所以我们登录别的机器的时候，只需要ssh -O StrictHostKeyChecking=no就可以直接登录了，就不会有上面烦人的提示了, 而且还会讲目标主机key加到~/.ssh/known_hosts里面。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code21'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068821"><td class="code" id="p40688code21"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">ssh</span>=<span style="color: #ff0000;">'ssh -o StrictHostKeyChecking=no'</span></pre></td></tr></table></div>

<p>        这样以后每次只要输入ssh, 不用输入那么长的命令了。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code22'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068822"><td class="code" id="p40688code22"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_SSH_OPTS</span>=<span style="color: #ff0000;">&quot;-o StrictHostKeyChecking=no&quot;</span></pre></td></tr></table></div>

<p>        这样配置以后，启动hadoop集群的时候，也不需要手工输入那个yes了。
        </li>
<li>HADOOP_PID_DIR<br />
        hadoop脚本启动hadoop程序的时候，把每一个程序的pid写到一个文件里，这个文件所在的目录就是HADOOP_PID_DIR的值。HADOOP_PID_DIR的默认值是/tmp, 这样如果想在同一个机器集群上启动多个hadoop集群，就会覆盖pid文件，所以要设置成其他目录：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code23'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068823"><td class="code" id="p40688code23"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_PID_DIR</span>=<span style="color: #800000;">${HADOOP_HOME}</span><span style="color: #000000; font-weight: bold;">/</span>pids</pre></td></tr></table></div>

</li>
</ol>
<p>    hadoop-env.sh配置如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code24'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068824"><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
</pre></td><td class="code" id="p40688code24"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Set Hadoop-specific environment variables here.</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The only required environment variable is JAVA_HOME.  All others are</span>
<span style="color: #666666; font-style: italic;"># optional.  When running a distributed configuration it is best to</span>
<span style="color: #666666; font-style: italic;"># set JAVA_HOME in this file, so that it is correctly defined on</span>
<span style="color: #666666; font-style: italic;"># remote nodes.</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The java implementation to use.  Required.</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>jvm<span style="color: #000000; font-weight: bold;">/</span>java-<span style="color: #000000;">6</span>-sun
&nbsp;
<span style="color: #666666; font-style: italic;"># The maximum amount of heap to use, in MB. Default is 1000.</span>
<span style="color: #666666; font-style: italic;"># export HADOOP_HEAPSIZE=2000</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Extra Java runtime options.  Empty by default.</span>
<span style="color: #666666; font-style: italic;"># export HADOOP_OPTS=-server</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Extra ssh options.  Default: '-o ConnectTimeout=1 -o SendEnv=HADOOP_CONF_DIR'.</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_SSH_OPTS</span>=<span style="color: #ff0000;">&quot;-o StrictHostKeyChecking=no&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Where log files are stored.  $HADOOP_HOME/logs by default.</span>
<span style="color: #666666; font-style: italic;"># export HADOOP_LOG_DIR=${HADOOP_HOME}/logs</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># File naming remote slave hosts.  $HADOOP_HOME/conf/slaves by default.</span>
<span style="color: #666666; font-style: italic;"># export HADOOP_SLAVES=${HADOOP_HOME}/conf/slaves</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># host:path where hadoop code should be rsync'd from.  Unset by default.</span>
<span style="color: #666666; font-style: italic;"># export HADOOP_MASTER=master:/home/$USER/src/hadoop</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The directory where pid files are stored. /tmp by default.</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_PID_DIR</span>=<span style="color: #800000;">${HADOOP_HOME}</span><span style="color: #000000; font-weight: bold;">/</span>pids
&nbsp;
<span style="color: #666666; font-style: italic;"># A string representing this instance of hadoop. $USER by default.</span>
<span style="color: #666666; font-style: italic;"># export HADOOP_IDENT_STRING=$USER</span></pre></td></tr></table></div>

</li>
<li>hadoop-site.xml<br />
    hadoop-site.xml是对hadoop的java程序进行配置。和nutch一样，hadoop-default.xml是默认的配置，不要直接修改它，把你的配置放到hadoop-site.xml中来。<br />
    必须的选项：
<ol>
<li>hadoop.tmp.dir<br />
        hadoop的dfs数据和map reduce程序运行的时候临时数据存放在此
      </li>
<li>fs.default.name<br />
        namenode的ip和端口
      </li>
<li>mapred.job.tracker<br />
        jobtracker的ip和端口
      </li>
</ol>
<p>可选的选项：
<ol>
<li>mapred.job.tracker.http.address<br />
        jobtracker的web ip和端口配置
      </li>
<li>dfs.http.address<br />
        hdfs的web ip和端口配置
      </li>
<p>      在一个机器集群上配置多个hadoop集群的时候，需要修改上面这两个选项和上面的必须的选项中关于namenode和jobtracker的两个选项。
<li>mapred.map.tasks<br />
        每个任务的map task数目
      </li>
<li>mapred.reduce.tasks<br />
        每个任务的reduce task书目
      </li>
<li>mapred.tasktracker.map.tasks.maximum<br />
        每个tasktracker能运行的map task的最大的数目
      </li>
<li>mapred.tasktracker.reduce.tasks.maximum<br />
        每个tasktracker能运行的reduce task的最大的数目
      </li>
<li>mapred.child.java.opts<br />
        传给每个task程序的java选项，默认的是设置最大内存为200M
      </li>
</ol>
<p>    hadoop-site.xml配置如下：</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40688&amp;download=hadoop-site.xml">hadoop-site.xml</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068825"><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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
</pre></td><td class="code" id="p40688code25"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml-stylesheet</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/xsl&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;configuration.xsl&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- Put site-specific property overrides in this file. --&gt;</span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>master<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hadoop.tmp.dir<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/opt/crawler/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>A base for other temporary directories.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>fs.default.name<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hdfs://${master}:9000/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The name of the default file system. Either the literal string
      &quot;local&quot; or a host:port for DFS.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.job.tracker<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hdfs://${master}:9001/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The host and port that the MapReduce job tracker runs at. If
      &quot;local&quot;, then jobs are run in-process as a single map and reduce task.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.job.tracker.http.address<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.0.0:50030<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The job tracker http server address and port the server will listen on.
      If the port is 0 then the server will start on a free port.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dfs.http.address<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.0.0:50070<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The address and the base port where the dfs namenode web ui will listen on.
      If the port is 0 then the server will start on a free port.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.map.tasks<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>31<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The default number of map tasks per job.  Typically set
      to a prime several times greater than number of available hosts.
      Ignored when mapred.job.tracker is &quot;local&quot;.  
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.reduce.tasks<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The default number of reduce tasks per job.  Typically set
      to a prime close to the number of available hosts.  Ignored when
      mapred.job.tracker is &quot;local&quot;.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.tasktracker.map.tasks.maximum<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The maximum number of map tasks that will be run
      simultaneously by a task tracker.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.tasktracker.reduce.tasks.maximum<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The maximum number of reduce tasks that will be run
      simultaneously by a task tracker.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.child.java.opts<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-Xmx1024m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Java opts for the task tracker child processes.  
      The following symbol, if present, will be interpolated: @taskid@ is replaced 
      by current TaskID. Any other occurrences of '@' will go unchanged.
      For example, to enable verbose gc logging to a file named for the taskid in
      /tmp and to set the heap maximum to be a gigabyte, pass a 'value' of:
      -Xmx1024m -verbose:gc -Xloggc:/tmp/@taskid@.gc
&nbsp;
      The configuration variable mapred.child.ulimit can be used to control the
      maximum virtual memory of the child processes. 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapred.task.tracker.http.address<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.0.0:0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The task tracker http server address and port.
      If the port is 0 then the server will start on a free port.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dfs.secondary.http.address<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.0.0:0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The secondary namenode http server address and port.
      If the port is 0 then the server will start on a free port.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dfs.datanode.address<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.0.0:0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The address where the datanode server will listen to.
      If the port is 0 then the server will start on a free port.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dfs.datanode.http.address<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.0.0:0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The datanode http server address and port.
      If the port is 0 then the server will start on a free port.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dfs.datanode.ipc.address<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.0.0:0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      The datanode ipc server address and port.
      If the port is 0 then the server will start on a free port.
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

</li>
<li>master secondarymasters<br />
    hadoop中默认的没有master这个文件，只有个masters文件，启动hadoop集群的时候只能在master上启动，不能在slave上启动，masters文件里面存放的是secondarynamenode的ip。我改了一下hadoop的脚本，master文件里面存放master的ip，secondarymasters里面存放secondarynamenode的ip。
  </li>
</ul>
<h4>nutch的配置</h4>
<ul>
<li>urlfilter<br />
    由于plugin.includes中只包含了urlfilter-regex，而根据<a href="nutch-load-conf.htm">《nutch配置文件的加载》</a>一文，<a href="http://ahei.info/t/crawl" class="st_tag internal_tag" rel="tag" title="标签 crawl 下的日志">crawl</a>-tool.xml文件的优先级最高，所以urlfilter-regex插件所用到的配置文件应该是crawl-tool.xml中配置的，默认是crawl-urlfilter.txt，改其配置如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code26'); return false;">View Code</a> TEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068826"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p40688code26"><pre class="text" style="font-family:monospace;"># skip file:, ftp:, &amp; mailto: urls
-^(file|ftp|mailto):
&nbsp;
# skip image and other suffixes we can't yet parse
-\.(gif|GIF|jpg|JPG|png|PNG|ico|ICO|css|sit|eps|wmf|zip|ppt|mpg|xls|gz|rpm|tgz|mov|MOV|exe|jpeg|JPEG|bmp|BMP)$
&nbsp;
+.</pre></td></tr></table></div>

</li>
<li><a href="http://ahei.info/c-nutch.htm" class="st_tag internal_tag" rel="tag" title="标签 Nutch 下的日志">nutch</a>-site.xml<br />
    必须的配置是http.agent.name和http.robots.agents，和<a href="nutch-load-conf.htm">《nutch配置文件的加载》</a>文中一样。
  </li>
</ul>
<h4>一些方便部署的脚本</h4>
<p>我修改了一些hadoop的脚本，使得部署和监控hadoop更方便。
<ul>
<li>restart-all.sh<br />
    重启hadoop集群
  </li>
<li>all.sh<br />
    这个脚本使你可以同时在集群的所有机器上执行同一个命令，比如你想查看集群上的日志里有没有错误，这样就可以了：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code27'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068827"><td class="code" id="p40688code27"><pre class="bash" style="font-family:monospace;">      .<span style="color: #000000; font-weight: bold;">/</span>all.sh <span style="color: #c20cb9; font-weight: bold;">grep</span> ERROR path-of-logs<span style="color: #000000; font-weight: bold;">/</span>hadoop.log</pre></td></tr></table></div>


<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40688&amp;download=all.sh">all.sh</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068828"><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
</pre></td><td class="code" id="p40688code28"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Time-stamp: &lt;2010-01-04 16:21:16 Monday by ahei&gt;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">readonly</span> <span style="color: #007800;">PROGRAM_NAME</span>=<span style="color: #ff0000;">&quot;rm-all.sh&quot;</span>
&nbsp;
usage<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;usage: <span style="color: #007800;">${PROGRAM_NAME}</span> -h | [-s] &lt;COMMAND&gt; ...&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Options&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;-s<span style="color: #000099; font-weight: bold;">\t</span>sort output&quot;</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;x$1&quot;</span> = <span style="color: #ff0000;">&quot;x-h&quot;</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">&quot;x$1&quot;</span> = <span style="color: #ff0000;">&quot;x--help&quot;</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">$#</span> = <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    usage
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;$0&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span>; <span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;x$1&quot;</span> = <span style="color: #ff0000;">&quot;x-s&quot;</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;">shift</span>
    <span style="color: #007800;">output</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>/slaves.sh&quot;</span> <span style="color: #660033;">--hosts</span> master <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${bin}</span>&quot;</span> \<span style="color: #000000; font-weight: bold;">&amp;</span>\<span style="color: #000000; font-weight: bold;">&amp;</span> <span style="color: #ff0000;">&quot;$@&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #007800;">output</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$output</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #007800;">$bin</span><span style="color: #000000; font-weight: bold;">/</span>slaves.sh<span style="color: #ff0000;">&quot; --hosts secondarymasters cd &quot;</span><span style="color: #800000;">${bin}</span><span style="color: #ff0000;">&quot; \&amp;\&amp; &quot;</span>$<span style="color: #000000; font-weight: bold;">@</span><span style="color: #ff0000;">&quot;<span style="color: #780078;">`
    output=&quot;${output}\n&quot;`</span>&quot;</span><span style="color: #007800;">$bin</span><span style="color: #000000; font-weight: bold;">/</span>slaves.sh<span style="color: #ff0000;">&quot; cd &quot;</span><span style="color: #800000;">${bin}</span><span style="color: #ff0000;">&quot; \&amp;\&amp; &quot;</span>$<span style="color: #000000; font-weight: bold;">@</span><span style="color: #ff0000;">&quot;`
    echo &quot;</span><span style="color: #800000;">${output}</span><span style="color: #ff0000;">&quot; | sort
else
    &quot;</span><span style="color: #007800;">$bin</span><span style="color: #000000; font-weight: bold;">/</span>slaves.sh<span style="color: #ff0000;">&quot; --hosts master cd &quot;</span><span style="color: #800000;">${bin}</span><span style="color: #ff0000;">&quot; \&amp;\&amp; &quot;</span>$<span style="color: #000000; font-weight: bold;">@</span><span style="color: #ff0000;">&quot;
    &quot;</span><span style="color: #007800;">$bin</span><span style="color: #000000; font-weight: bold;">/</span>slaves.sh<span style="color: #ff0000;">&quot; --hosts secondarymasters cd &quot;</span><span style="color: #800000;">${bin}</span><span style="color: #ff0000;">&quot; \&amp;\&amp; &quot;</span>$<span style="color: #000000; font-weight: bold;">@</span><span style="color: #ff0000;">&quot;
    &quot;</span><span style="color: #007800;">$bin</span><span style="color: #000000; font-weight: bold;">/</span>slaves.sh<span style="color: #ff0000;">&quot; cd &quot;</span><span style="color: #800000;">${bin}</span><span style="color: #ff0000;">&quot; \&amp;\&amp; &quot;</span>$<span style="color: #000000; font-weight: bold;">@</span><span style="color: #ff0000;">&quot;
fi</span></pre></td></tr></table></div>

</li>
<li>clean-logs.sh<br />
    删除所有机器上的log</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40688&amp;download=clean-logs.sh">clean-logs.sh</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068829"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p40688code29"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Time-stamp: &lt;10/24/2008 11:09:33 星期五 by ahei&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Clean logs on all hadoop daemons.</span>
&nbsp;
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;$0&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span>; <span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
. <span style="color: #007800;">$bin</span><span style="color: #000000; font-weight: bold;">/</span>hadoop-config.sh
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${HADOOP_CONF_DIR}</span>/hadoop-env.sh&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  . <span style="color: #ff0000;">&quot;<span style="color: #007800;">${HADOOP_CONF_DIR}</span>/hadoop-env.sh&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">HADOOP_LOG_DIR</span>=<span style="color: #800000;">${HADOOP_LOG_DIR:-&quot;${HADOOP_HOME}</span><span style="color: #000000; font-weight: bold;">/</span>logs<span style="color: #ff0000;">&quot;}
&nbsp;
&quot;</span><span style="color: #800000;">${bin}</span><span style="color: #000000; font-weight: bold;">/</span>rm-all.sh<span style="color: #ff0000;">&quot; &quot;</span><span style="color: #800000;">${HADOOP_LOG_DIR}</span><span style="color: #ff0000;">&quot;</span></pre></td></tr></table></div>

</li>
<li>df-all.sh du-all.sh jps-all.sh ll-all.sh mv-all.sh rm-all.sh<br />
    在所有的机器上执行对应的前缀命令，比如df-all.sh，即在所有机器上执行df命令，这些脚本调用的都是all.sh。
  </li>
<li>update-conf.sh<br />
    配置hadoop的时候，有两个地方需要配置master的ip，一个是master文件夹，另一个是hadoop-site.xml中配置namenode和jobtracker的ip，那么每次配置hadoop的时候都需要配置这两个项，能不能只配置一个呢？还有，为了方便管理，我部署nutch的时候，建立的文件结构是这样的，/opt/crawler，/opt/crawler/data，/opt/crawler/program，data这个文件夹是hadoop.tmp.dir，program则是nutch的程序，所以hadoop.tmp.dir实际上即使$HADOOP_HOME/../data。为了方便部署，我写了这个update-conf.sh脚本，自动把master文件中的内容写到haoop-site.xml中去，而且自动更新hadoop-site.xml中的hadoop.tmp.dir的值，这样你配置的时候，只需要配置master文件就可以了。</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40688&amp;download=update-conf.sh">update-conf.sh</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068830"><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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code" id="p40688code30"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Time-stamp: &lt;2010-01-22 15:31:53 Friday by ahei&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># @version 1.0</span>
<span style="color: #666666; font-style: italic;"># @author ahei</span>
&nbsp;
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;$0&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
resolveLink<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: #007800;">this</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-L</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$this</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$this</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #007800;">link</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">readlink</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$this</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">link</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>normalizePath <span style="color: #ff0000;">&quot;<span style="color: #007800;">$link</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${link:0:1}</span>&quot;</span> = <span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #007800;">this</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$link</span>&quot;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #007800;">dir</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$this</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$dir</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;.&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #007800;">this</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$dir</span>/<span style="color: #007800;">$link</span>&quot;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #007800;">this</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$link</span>&quot;</span>
            <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$this</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
normalizePath<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;">local</span> <span style="color: #007800;">path</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>
&nbsp;
    <span style="color: #007800;">dir</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$path</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$dir</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;.&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #007800;">path</span>=<span style="color: #007800;">$dir</span><span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$path</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #007800;">path</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$path</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$path</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #007800;">confFile</span>=hadoop-site.xml
&nbsp;
<span style="color: #666666; font-style: italic;"># update master setting</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>conf <span style="color: #000000; font-weight: bold;">&amp;&amp;</span>
<span style="color: #007800;">no</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-xE</span> <span style="color: #ff0000;">&quot;[[:space:]]*&lt;name&gt;master&lt;/name&gt;[[:space:]]*&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$confFile</span>&quot;</span> <span style="color: #660033;">-n</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: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $1}'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">let</span> no++
<span style="color: #007800;">master</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> master<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$no</span> s#&lt;value&gt;.*&lt;/value&gt;#&lt;value&gt;<span style="color: #007800;">$master</span>&lt;/value&gt;#g&quot;</span> <span style="color: #660033;">-i</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$confFile</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># update hadoop.tmp.dir</span>
&nbsp;
<span style="color: #007800;">no</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-xE</span> <span style="color: #ff0000;">&quot;[[:space:]]*&lt;name&gt;hadoop.tmp.dir&lt;/name&gt;[[:space:]]*&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$confFile</span>&quot;</span> <span style="color: #660033;">-n</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: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $1}'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">let</span> no++
&nbsp;
<span style="color: #007800;">dataDir</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>resolveLink <span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>.. <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>data
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$no</span> s#&lt;value&gt;.*&lt;/value&gt;#&lt;value&gt;<span style="color: #007800;">$dataDir</span>&lt;/value&gt;#g&quot;</span> <span style="color: #660033;">-i</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$confFile</span>&quot;</span></pre></td></tr></table></div>

</li>
<li><span style="color: #0000ff;">kill-all.sh</span><br />
    由于hadoop的stop-all.sh脚本是根据pid文件来kill hadoop的daemon程序的，所以如果你不小心删除了pid文件，stop-all.sh就不能kill掉那些daemon程序了。kill-all.sh弥补了stop-all.sh的缺陷，它是通过jps命令来得到所有的java进程pid，然后根据daemon程序的名字来得到所有的daemon程序的pid，再根据/proc文件夹得到这些进程的当前目录，如果这个当前目录与HADOOP_HOME一样，就kill掉这个进程。<br />
    <span style="color: #0000ff;">ping-all.sh</span><br />
    这个脚本不是在所有的机器上运行ping命令，而是ping一下所有机器上的daemon程序，还是否还活着，管理hadoop集群的时候很方便。<br />
    由于kill-all.sh和ping-all.sh最终都是通过hadoop-daemon.sh来实现的, 我这里只列出hadoop-daemon.sh的代码:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40688&amp;download=hadoop-daemon.sh">hadoop-daemon.sh</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068831"><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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
</pre></td><td class="code" id="p40688code31"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Time-stamp: &lt;2010-02-03 15:34:22 Wednesday by ahei&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Runs a Hadoop command as a daemon.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Environment Variables</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#   HADOOP_CONF_DIR  Alternate conf dir. Default is ${HADOOP_HOME}/conf.</span>
<span style="color: #666666; font-style: italic;">#   HADOOP_LOG_DIR   Where log files are stored.  PWD by default.</span>
<span style="color: #666666; font-style: italic;">#   HADOOP_MASTER    host:path where hadoop code should be rsync'd from</span>
<span style="color: #666666; font-style: italic;">#   HADOOP_PID_DIR   The pid files are stored. /tmp by default.</span>
<span style="color: #666666; font-style: italic;">#   HADOOP_IDENT_STRING   A string representing this instance of hadoop. $USER by default</span>
<span style="color: #666666; font-style: italic;">#   HADOOP_NICENESS The scheduling priority for daemons. Defaults to 0.</span>
&nbsp;
<span style="color: #007800;">usage</span>=<span style="color: #ff0000;">&quot;Usage: hadoop-daemon.sh [--config &lt;conf-dir&gt;] [--hosts hostlistfile] (start|stop|ping) &lt;hadoop-command&gt; &lt;args...&gt;&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># if no args specified, show usage</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-le</span> <span style="color: #000000;">1</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;">echo</span> <span style="color: #007800;">$usage</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;$0&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span>; <span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
. <span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>hadoop-config.sh
&nbsp;
<span style="color: #666666; font-style: italic;"># get arguments</span>
<span style="color: #007800;">startStop</span>=<span style="color: #007800;">$1</span>
<span style="color: #7a0874; font-weight: bold;">shift</span>
<span style="color: #007800;">command</span>=<span style="color: #007800;">$1</span>
<span style="color: #7a0874; font-weight: bold;">shift</span>
&nbsp;
hadoop_rotate_log <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: #007800;">log</span>=<span style="color: #007800;">$1</span>;
    <span style="color: #007800;">num</span>=<span style="color: #000000;">5</span>;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;$2&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	    <span style="color: #007800;">num</span>=<span style="color: #007800;">$2</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$log</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;"># rotate logs</span>
	    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$num</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
	        <span style="color: #007800;">prev</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$num</span> - <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
	        <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$log</span>.<span style="color: #007800;">$prev</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$log</span>.<span style="color: #007800;">$prev</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$log</span>.<span style="color: #007800;">$num</span>&quot;</span>
	        <span style="color: #007800;">num</span>=<span style="color: #007800;">$prev</span>
	    <span style="color: #000000; font-weight: bold;">done</span>
	    <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$log</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$log</span>.<span style="color: #007800;">$num</span>&quot;</span>;
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${HADOOP_CONF_DIR}</span>/hadoop-env.sh&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    . <span style="color: #ff0000;">&quot;<span style="color: #007800;">${HADOOP_CONF_DIR}</span>/hadoop-env.sh&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># get log directory</span>
<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;">$HADOOP_LOG_DIR</span>&quot;</span> = <span style="color: #ff0000;">&quot;&quot;</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;">export</span> <span style="color: #007800;">HADOOP_LOG_DIR</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$HADOOP_HOME</span>/logs&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$HADOOP_LOG_DIR</span>&quot;</span>
&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;">$HADOOP_PID_DIR</span>&quot;</span> = <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #007800;">HADOOP_PID_DIR</span>=<span style="color: #000000; font-weight: bold;">/</span>tmp
<span style="color: #000000; font-weight: bold;">fi</span>
&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;">$HADOOP_IDENT_STRING</span>&quot;</span> = <span style="color: #ff0000;">&quot;&quot;</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;">export</span> <span style="color: #007800;">HADOOP_IDENT_STRING</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$USER</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># some variables</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_LOGFILE</span>=hadoop-<span style="color: #007800;">$HADOOP_IDENT_STRING</span>-<span style="color: #007800;">$command</span>-<span style="color: #007800;">$HOSTNAME</span>.log
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">HADOOP_ROOT_LOGGER</span>=<span style="color: #ff0000;">&quot;INFO,DRFA&quot;</span>
<span style="color: #007800;">log</span>=<span style="color: #007800;">$HADOOP_LOG_DIR</span><span style="color: #000000; font-weight: bold;">/</span>hadoop-<span style="color: #007800;">$HADOOP_IDENT_STRING</span>-<span style="color: #007800;">$command</span>-<span style="color: #007800;">$HOSTNAME</span>.out
<span style="color: #007800;">pid</span>=<span style="color: #007800;">$HADOOP_PID_DIR</span><span style="color: #000000; font-weight: bold;">/</span>hadoop-<span style="color: #007800;">$HADOOP_IDENT_STRING</span>-<span style="color: #007800;">$command</span>.pid
&nbsp;
<span style="color: #666666; font-style: italic;"># Set default scheduling priority</span>
<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;">$HADOOP_NICENESS</span>&quot;</span> = <span style="color: #ff0000;">&quot;&quot;</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;">export</span> <span style="color: #007800;">HADOOP_NICENESS</span>=<span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$startStop</span> <span style="color: #000000; font-weight: bold;">in</span>
    start<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$HADOOP_PID_DIR</span>&quot;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$pid</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-0</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$pid</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$command</span> running <span style="color: #c20cb9; font-weight: bold;">as</span> process <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$pid</span><span style="color: #000000; font-weight: bold;">`</span>.  Stop it first.
                <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
            <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
        hadoop_rotate_log <span style="color: #007800;">$log</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> starting <span style="color: #007800;">$command</span>, logging to <span style="color: #007800;">$log</span>
        <span style="color: #c20cb9; font-weight: bold;">nohup</span> <span style="color: #c20cb9; font-weight: bold;">nice</span> <span style="color: #660033;">-n</span> <span style="color: #007800;">$HADOOP_NICENESS</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$HADOOP_HOME</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>hadoop <span style="color: #660033;">--config</span> <span style="color: #007800;">$HADOOP_CONF_DIR</span> <span style="color: #007800;">$command</span> <span style="color: #ff0000;">&quot;$@&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$log</span>&quot;</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000; font-weight: bold;">&amp;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$!</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$pid</span>
        <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>; <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$log</span>&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
    stop<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$pid</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-0</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$pid</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> stopping <span style="color: #007800;">$command</span>
                <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-9</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$pid</span><span style="color: #000000; font-weight: bold;">`</span>
            <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> no <span style="color: #007800;">$command</span> to stop
            <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #7a0874; font-weight: bold;">echo</span> no <span style="color: #007800;">$command</span> to stop
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">kill</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">pids</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>jps <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tr</span> <span style="color: #ff0000;">'[A-Z]'</span> <span style="color: #ff0000;">'[a-z]'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">&quot;{if (NF &gt; 1 &amp;&amp; <span style="color: #000099; font-weight: bold;">\$</span>2 == <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$command</span><span style="color: #000099; font-weight: bold;">\&quot;</span>){print <span style="color: #000099; font-weight: bold;">\$</span>1}}&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">exist</span>=
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pids</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #000000; font-weight: bold;">for</span> p <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$pids</span>; <span style="color: #000000; font-weight: bold;">do</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(readlink -m /proc/$p/cwd)</span>&quot;</span> = <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(readlink -m &quot;$HADOOP_HOME&quot;)</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</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;">echo</span> <span style="color: #ff0000;">&quot;killing <span style="color: #007800;">$command</span> of pid <span style="color: #007800;">$p</span> ...&quot;</span>
                    <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-9</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$p</span>&quot;</span>
                    <span style="color: #007800;">exist</span>=<span style="color: #000000;">1</span>
                <span style="color: #000000; font-weight: bold;">fi</span>
            <span style="color: #000000; font-weight: bold;">done</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$exist</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</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;">echo</span> <span style="color: #ff0000;">&quot;Can not found any <span style="color: #007800;">$command</span> to kill&quot;</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">ping</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$pid</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-0</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$pid</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$command</span> is alive&quot;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #007800;">pids</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>jps <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tr</span> <span style="color: #ff0000;">'[A-Z]'</span> <span style="color: #ff0000;">'[a-z]'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">&quot;{if (NF &gt; 1 &amp;&amp; <span style="color: #000099; font-weight: bold;">\$</span>2 == <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$command</span><span style="color: #000099; font-weight: bold;">\&quot;</span>){print <span style="color: #000099; font-weight: bold;">\$</span>1}}&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #007800;">maybePids</span>=
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pids</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #000000; font-weight: bold;">for</span> p <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$pids</span>; <span style="color: #000000; font-weight: bold;">do</span>
                    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(readlink -m /proc/$p/cwd)</span>&quot;</span> = <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(readlink -m &quot;$HADOOP_HOME&quot;)</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                        <span style="color: #007800;">maybePids</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$maybePids</span> <span style="color: #007800;">$p</span>&quot;</span>
                    <span style="color: #000000; font-weight: bold;">fi</span>
                <span style="color: #000000; font-weight: bold;">done</span>
            <span style="color: #000000; font-weight: bold;">fi</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$maybePids</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</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;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$command</span> is dead&quot;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pid</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                    <span style="color: #007800;">output</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$command</span> pid can not found in its pid file <span style="color: #007800;">$pid</span>&quot;</span>
                <span style="color: #000000; font-weight: bold;">else</span>
                    <span style="color: #007800;">output</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${command}</span>'s pid file <span style="color: #007800;">$pid</span> does not exist&quot;</span>
                <span style="color: #000000; font-weight: bold;">fi</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$output</span>, but some pids<span style="color: #007800;">$maybePids</span> of <span style="color: #007800;">$command</span> exist&quot;</span>
            <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
    <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;">$usage</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></td></tr></table></div>

</li>
<li><span style="color: #0000ff;">rsync-slaves.sh</span><br />
    假如你修改了一项配置或者改了一下程序，那你怎么把所有机器上的程序都更新一下？hadoop已经替你想好了，它默认的是在hadoop-daemon.sh里调用rsync命令，来把某台机器与master同步，我单独写了这个脚本，来把所有的slave和master同步。在start-all.sh脚本里会自动调用rsync-slaves.sh，所以基本上不需要你手动执行它。该脚本会忽略名为ignores的文件或文件夹，你可以把你不想同步的文件都放到ignores文件夹里面。</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40688&amp;download=rsync-slaves.sh">rsync-slaves.sh</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068832"><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
</pre></td><td class="code" id="p40688code32"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Time-stamp: &lt;2010-01-14 17:07:05 Thursday by ahei&gt;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">readonly</span> <span style="color: #007800;">PROGRAM_NAME</span>=<span style="color: #ff0000;">&quot;rsync-slaves.sh&quot;</span>
&nbsp;
usage<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;usage: <span style="color: #007800;">${PROGRAM_NAME}</span> [--hosts hostlistfile] [-h]&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;x$1&quot;</span> = <span style="color: #ff0000;">&quot;x-h&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    usage
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;$0&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">bin</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span>; <span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
. <span style="color: #007800;">$bin</span><span style="color: #000000; font-weight: bold;">/</span>hadoop-config.sh
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${HADOOP_CONF_DIR}</span>/hadoop-env.sh&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  . <span style="color: #ff0000;">&quot;<span style="color: #007800;">${HADOOP_CONF_DIR}</span>/hadoop-env.sh&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">command</span>=<span style="color: #ff0000;">&quot;mkdir -p <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$HADOOP_HOME</span><span style="color: #000099; font-weight: bold;">\&quot;</span> &amp;&amp; rsync -azvh --delete --progress --exclude=logs --exclude=ignores --exclude=pids <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${HADOOP_MASTER}</span><span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${HADOOP_HOME}</span><span style="color: #000099; font-weight: bold;">\&quot;</span> $@&quot;</span>
<span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>slaves.sh <span style="color: #007800;">$command</span>
<span style="color: #ff0000;">&quot;<span style="color: #007800;">$bin</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>slaves.sh <span style="color: #660033;">--hosts</span> secondarymasters <span style="color: #007800;">$command</span></pre></td></tr></table></div>

</li>
</ul>
<h4>部署</h4>
<p>讲完配置，下面就开始部署了。
<ol>
<li>配置机器连通性<br />
    由于hadoops是通过ssh启动没个节点上的daemon程序，所以先配置好机器之间的<a href="ssh-copy-id.htm" target="_blank">免认证登录</a>，免得每次启动hadoop集群的时候都需要输入密码。
  </li>
<li>启动hadoop集群

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code33'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068833"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p40688code33"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>crawler
<span style="color: #c20cb9; font-weight: bold;">cp</span> nutch <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>crawler<span style="color: #000000; font-weight: bold;">/</span>program <span style="color: #660033;">-r</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>crawler<span style="color: #000000; font-weight: bold;">/</span>program<span style="color: #000000; font-weight: bold;">/</span>bin
.<span style="color: #000000; font-weight: bold;">/</span>hadoop namenode <span style="color: #660033;">-format</span>
.<span style="color: #000000; font-weight: bold;">/</span>start-all.sh</pre></td></tr></table></div>

</li>
<li>开始抓取<br />
    抓取和文<a href="nutch-load-conf.htm">《nutch配置文件的加载》</a>中一样，有一个不通的地方是url文件夹必须是在hdfs里面存放的，你可以用这个命令把本地url文件夹拷贝到hdfs中：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40688code34'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4068834"><td class="code" id="p40688code34"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>hadoop fs <span style="color: #660033;">-copyFromLocal</span> ignores<span style="color: #000000; font-weight: bold;">/</span>urls urls</pre></td></tr></table></div>

</li>
<li>查看hadoop job task状态
<p>http://master:50030查看jobtracker状态，http://master:50070可以浏览hdfs中内容</p>
</li>
</ol>
<h4>部署多个hadoop集群</h4>
<p>如果你的机器比较紧张，想在一个机器集群上部署多个hadoop集群，该怎么弄呢？很简单，首先把nutch文件夹拷贝到另一个不同的地方，然后你只需要修改hadoop-site.xml中以下几项为不同的值就可以了：<br />
fs.default.name mapred.job.tracker mapred.job.tracker.http.address dfs.http.address</p>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/nutch-distributed-crawl.htm/feed</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>ssh的免认证登录</title>
		<link>http://ahei.info/ssh-copy-id.htm</link>
		<comments>http://ahei.info/ssh-copy-id.htm#comments</comments>
		<pubDate>Fri, 12 Feb 2010 15:13:32 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[中级]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[expect]]></category>
		<category><![CDATA[keychain]]></category>
		<category><![CDATA[se]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[ssh-agent]]></category>
		<category><![CDATA[ssh-copy-id]]></category>
		<category><![CDATA[配置]]></category>

		<guid isPermaLink="false">http://emacser.com/?p=40693</guid>
		<description><![CDATA[linux下用ssh登录别的机器的时候，需要通过交互方式手工输入密码，ssh不支持直接加密码的选项，它觉得这样不安全。 但是有时候要完成一些自动的任务，比如登录到别的机器上，并在那台机器上启动一些程序，这时候该怎么办呢？ 我下面提供几种方法： 通过expect ?Download ssh.exp1 2 3 4 5 6 #!/usr/bin/expect &#160; spawn ssh -o StrictHostKeyChecking=no -l username hostname expect &#34;*password:&#34; send &#34;password\r&#34; interact 把上面的代码中的username和hostname替换为你的用户名和ip，然后保存为ssh.exp，再执行下面的代码： ?View Code BASH1 2 chown +x ssh.exp ./ssh.exp 就可以自动登录到目标机器上并执行一下ls命令。 这个方法有个缺点，就是密码以明文的方式保存在文件里，不安全。 sshpass sshpass是专门为ssh的免认证登录设计的, 它可以通过标准输入读入密码, 也可以通过把密码放在它的&#8221;-p&#8221;选项后面, 还可以用&#8221;-f&#8221;选项来制定密码文件, 还可以用&#8221;-e&#8221;选项从环境变量&#8221;SSHPASS&#8221;来读入密码, ssh的命令跟在sshpass的选项后面, 例如: ?View Code BASHsshpass -p password ssh host -l username rsync是一个同步的命令, 它是通过ssh来同步的, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="OpenSSH" src="screenshots/openssh.gif" width="200" height="90"/></p>
<p>linux下用ssh登录别的机器的时候，需要通过交互方式手工输入密码，ssh不支持直接加密码的选项，它觉得这样不安全。 但是有时候要完成一些自动的任务，比如登录到别的机器上，并在那台机器上启动一些程序，这时候该怎么办呢？<span id="more-40693"></span></p>
<p>我下面提供几种方法：</p>
<ol>
<li><span style="color: #0000ff;">通过expect</span>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40693&amp;download=ssh.exp">ssh.exp</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069347"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p40693code47"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/expect</span>
&nbsp;
spawn <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">StrictHostKeyChecking</span>=no <span style="color: #660033;">-l</span> username <span style="color: #c20cb9; font-weight: bold;">hostname</span>
expect <span style="color: #ff0000;">&quot;*password:&quot;</span>
send <span style="color: #ff0000;">&quot;password<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
interact</pre></td></tr></table></div>

<p>把上面的代码中的username和hostname替换为你的用户名和ip，然后保存为ssh.exp，再执行下面的代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code48'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069348"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p40693code48"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chown</span> +x ssh.exp
.<span style="color: #000000; font-weight: bold;">/</span>ssh.exp</pre></td></tr></table></div>

<p>就可以自动登录到目标机器上并执行一下ls命令。<br />
这个方法有个缺点，就是密码以明文的方式保存在文件里，<span style="color: #0000ff;">不安全</span>。
  </li>
<li><span style="color: #0000ff;">sshpass</span><br />
    sshpass是专门为ssh的免认证登录设计的, 它可以通过标准输入读入密码, 也可以通过把密码放在它的&#8221;-p&#8221;选项后面, 还可以用&#8221;-f&#8221;选项来制定密码文件, 还可以用&#8221;-e&#8221;选项从环境变量&#8221;SSHPASS&#8221;来读入密码, ssh的命令跟在sshpass的选项后面, 例如:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code49'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069349"><td class="code" id="p40693code49"><pre class="bash" style="font-family:monospace;">sshpass <span style="color: #660033;">-p</span> password <span style="color: #c20cb9; font-weight: bold;">ssh</span> host <span style="color: #660033;">-l</span> username</pre></td></tr></table></div>

<p>    rsync是一个同步的命令, 它是通过ssh来同步的, 如果你想执行rsync的时候也不输入密码, 可以通过指定rsync的&#8221;&#8211;rsh&#8221;选项来实现, 比如:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code50'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069350"><td class="code" id="p40693code50"><pre class="bash" style="font-family:monospace;">rsync <span style="color: #660033;">--rsh</span>=<span style="color: #ff0000;">'sshpass -p password ssh -l username'</span> host.example.com:path</pre></td></tr></table></div>

<p>    如果想要scp也不输入密码的话，建立下面的文件：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40693&amp;download=ssh.sh">ssh.sh</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069351"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p40693code51"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env bash</span>
&nbsp;
sshpass <span style="color: #660033;">-p</span> password <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #ff0000;">&quot;$@&quot;</span></pre></td></tr></table></div>

<p>    然后这样使用scp：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code52'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069352"><td class="code" id="p40693code52"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">scp</span> <span style="color: #660033;">-S</span> path-of-ssh.sh<span style="color: #000000; font-weight: bold;">/</span>ssh.sh <span style="color: #c20cb9; font-weight: bold;">file</span> user<span style="color: #000000; font-weight: bold;">@</span>host:path</pre></td></tr></table></div>

<p>    该方法使用起来简单, 缺点也是密码以明文方式保存.
  </li>
<li><span style="color: #0000ff;">通过密钥文件来实现免认证登录</span>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code53'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069353"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p40693code53"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># 1. 生成密钥</span>
<span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa <span style="color: #660033;">-P</span> <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #660033;">-f</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa
&nbsp;
<span style="color: #666666; font-style: italic;"># 2. 把本机的公钥拷到目标机器上</span>
<span style="color: #c20cb9; font-weight: bold;">scp</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa.pub username<span style="color: #000000; font-weight: bold;">@</span>remote-hostname:~<span style="color: #000000; font-weight: bold;">/</span>temp</pre></td></tr></table></div>

<p>    经过上面的操作，再登录到目标机器上执行：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code54'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069354"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p40693code54"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh
<span style="color: #666666; font-style: italic;"># 改变权限，必须</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">700</span> .ssh
<span style="color: #c20cb9; font-weight: bold;">cat</span> temp <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys
<span style="color: #666666; font-style: italic;"># 改变权限，必须</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">600</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> temp</pre></td></tr></table></div>

<p>    注意：上面代码中的chmod修改权限的语句必须执行，有的ssh设置使得.ssh目录和authorized_keys的权限必须只能自己可读可写，如果权限没设对的话，照样不能免认证登录，这是为了安全考虑。<br />
    经过上面的操作，你现在就可以不需要输入密码就可以登录到目标机器上了。
  </li>
<li><span style="color: #0000ff;"><a href="http://ahei.info/t/ssh-copy-id" class="st_tag internal_tag" rel="tag" title="标签 ssh-copy-id 下的日志">ssh-copy-id</a></span><br />
    上面那个方法，虽然执行的命令不多，但是你想想，如果我们要让一台机器对100台机器都实现免认证登录，岂不是还是很麻烦。那我们把上面的命令写成一个脚本岂不甚好？好注意，不过不用你写了，<a href="http://ahei.info/t/ssh" class="st_tag internal_tag" rel="tag" title="标签 ssh 下的日志">ssh</a>-copy-id这个命令已经帮你写了，你可以去看看/usr/bin/<a href="http://ahei.info/t/ssh" class="st_tag internal_tag" rel="tag" title="标签 ssh 下的日志">ssh</a>-copy-id这个文件，它实际上就是一个shell脚本，帮你把你的公钥拷到目标主机的认证文件里，并修改权限，不过它不帮你生成公钥，还得你自己生成。使用方法：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code55'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069355"><td class="code" id="p40693code55"><pre class="bash" style="font-family:monospace;">ssh-copy-id <span style="color: #7a0874; font-weight: bold;">&#91;</span>username<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">@</span><span style="color: #c20cb9; font-weight: bold;">hostname</span></pre></td></tr></table></div>

<p>    很简单吧！</li>
</ol>
<p>上面的方法3由于不需要把密码明文直接写到文件里面，所以比较安全，方法4和方法3本质上是一样的。</p>
<p>现在我们来考虑另外一个问题。</p>
<p>假如你通过方法2设置了免认证登录，这样, 只要别人拿到了你的私钥, 就可以登录所有已经认证你的机器了. 当然你也可以为你的私钥设置一个密码, 这是通过ssh-keygen的&#8221;-P&#8221;的来设置的. 但是现在又有一个问题, 那就是你每次登录到已经认证过你的机器的时候, 你都要输入一次你的私钥密码. keychain是解决这个问题的一个很好的工具, 它是ssh-agent的一个前端, 它会把已经认证过的密钥加入ssh-agent的高速缓存, 这样, 只有你第一次使用你的私钥登录别的机器的时候, 需要输入一下密码, 以后再次使用你的私钥的时候, 就不用输入密码了, 既保证了安全性, 又保证了便捷性.</p>
<p>讲完了上面说的免认证登录方法，我们现在可以很简单的让一个集群之间的每一台机器之间都互相免认证，而且完全自动化。<br />
方法就是：先在集群中的某一台机器上生成好密钥，并且把这台机器自己的公钥添加到它自己的认证文件里面，这样就实现了这台机器免认证登录自己。然后利用expect向每一台机器拷贝刚才那台机器的公私钥和认证文件，这样这个集群中所有机器的公私钥和认证文件都一样了，而刚才那台机器已经可以免认证登录自己，它们之间也当然可以免认证登录了（:)，是不是有点绕？）。这个方法会让每台机器的公私钥都一样，如果集群机器中已经配置了一些其他免认证登录的信息，不能破坏已有的公私钥，这个方法就不能凑效了，只能每两台机器之间互相调用ssh-copy-id命令。<br />
上面的方法具体实现如下：<br />
在集群中某一台机器上执行：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code56'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069356"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p40693code56"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa <span style="color: #660033;">-P</span> <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #660033;">-f</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa
<span style="color: #666666; font-style: italic;"># 改变权限，必须</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">700</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh
<span style="color: #c20cb9; font-weight: bold;">cat</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_rsa.pub <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys
<span style="color: #666666; font-style: italic;"># 改变权限，必须</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">600</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys</pre></td></tr></table></div>

<p>然后拷贝该机器的公私钥和认证文件到其他的机器上：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://ahei.info/wp-content/plugins/wp-codebox/wp-codebox.php?p=40693&amp;download=scp-auth.exp">scp-auth.exp</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069357"><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
</pre></td><td class="code" id="p40693code57"><pre class="tcl" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/expect</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">proc</span> scp <span style="color: #483d8b;">{user password host}</span> <span style="color: black;">&#123;</span>
    <span style="color: #ff7700;font-weight:bold;">global</span> env
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">set</span> home <span style="color: #008000;"><span style="color: #ff3333;">$env</span></span><span style="color: black;">&#40;</span>HOME<span style="color: black;">&#41;</span>
&nbsp;
    spawn ssh -o StrictHostKeyChecking=no <span style="color: #ff3333;">$user</span>@<span style="color: #ff3333;">$host</span> mkdir -p ~/.ssh
    expect <span style="color: #483d8b;">&quot;*password:&quot;</span> <span style="color: #483d8b;">{send &quot;$password\r&quot;}</span>
&nbsp;
    spawn scp -r -o StrictHostKeyChecking=no <span style="color: #ff3333;">$home</span>/.ssh/id_rsa <span style="color: #ff3333;">$user</span>@<span style="color: #ff3333;">$host</span>:~/.ssh
    expect <span style="color: #483d8b;">&quot;*password:&quot;</span> <span style="color: #483d8b;">{send &quot;$password\r&quot;}</span>
&nbsp;
    spawn scp -r -o StrictHostKeyChecking=no <span style="color: #ff3333;">$home</span>/.ssh/authorized_keys <span style="color: #ff3333;">$user</span>@<span style="color: #ff3333;">$host</span>:~/.ssh
    expect <span style="color: #483d8b;">&quot;*password:&quot;</span> <span style="color: #483d8b;">{send &quot;$password\r&quot;}</span>
&nbsp;
    wait
<span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">set</span> user <span style="color: black;">&#91;</span><span style="color: #008000;">lindex</span> <span style="color: #008000;"><span style="color: #ff3333;">$argv</span></span> <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">set</span> password <span style="color: black;">&#91;</span><span style="color: #008000;">lindex</span> <span style="color: #008000;"><span style="color: #ff3333;">$argv</span></span> <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">set</span> host <span style="color: black;">&#91;</span><span style="color: #008000;">lindex</span> <span style="color: #008000;"><span style="color: #ff3333;">$argv</span></span> <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
&nbsp;
scp <span style="color: #ff3333;">$user</span> <span style="color: #ff3333;">$password</span> <span style="color: #ff3333;">$host</span></pre></td></tr></table></div>

<p>把上面的scp-auth.exp文件保存后,执行下面的命令:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40693code58'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4069358"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p40693code58"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x scp-auth.exp
<span style="color: #c20cb9; font-weight: bold;">cat</span> hostlist <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-l</span> .<span style="color: #000000; font-weight: bold;">/</span>scp-auth.exp</pre></td></tr></table></div>

<p>其中hostlist文件为你的所有要拷贝认证文件的机器列表,每行一条记录, 每条记录的格式为:<br />
&lt;username&gt; &lt;password&gt; &lt;ip&gt;<br />
现在你可以在这个机器集群上自由的穿梭了, 不用输入任何密码!</p>
<p>expect很方便吧, 它最大的用处就是用来为那些需要交互的程序模拟用户的输入, 比如passwd, ssh, fsck, ftp等.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/ssh-copy-id.htm/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>我的wordpress插件</title>
		<link>http://ahei.info/wordpress-plugins.htm</link>
		<comments>http://ahei.info/wordpress-plugins.htm#comments</comments>
		<pubDate>Thu, 21 Jan 2010 06:00:49 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[初级]]></category>
		<category><![CDATA[技术杂记]]></category>
		<category><![CDATA[ahei]]></category>
		<category><![CDATA[akismet]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[DEA]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[emacser]]></category>
		<category><![CDATA[emacser.com]]></category>
		<category><![CDATA[favicon]]></category>
		<category><![CDATA[Gallery]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[highlight]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[lightbox]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[Permalink]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Redirection]]></category>
		<category><![CDATA[se]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[top]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[vi]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp-syntax]]></category>
		<category><![CDATA[yasnippet]]></category>
		<category><![CDATA[安装]]></category>
		<category><![CDATA[插件]]></category>
		<category><![CDATA[搜索引擎]]></category>
		<category><![CDATA[模板]]></category>
		<category><![CDATA[配置]]></category>

		<guid isPermaLink="false">http://emacser.com/?p=40617</guid>
		<description><![CDATA[最近自己用wordpress把博客搭起来了，由于以前在yo2上写博客的时候，wordpress都是yo2已搭好的，插件也都是他们加的，用户不能自己上传插件，少了很多麻烦，也多了一些不方便。这次自己搭博客，由于没有以前yo2上wordpress的插件列表，只有自己根据印象一个的去找插件，找的过程中，也发现了一些比较好的插件，这里我记录下来，做备录用，也供没有搭过wordpress的同志参考。（注：以下插件根据重要性和功能强大性做基本有序排序，这些插件都可以根据名字去wordpress插件官网下载。） Google XML Sitemaps 这个插件会生成你网站的xml地图，发送给google，yahoo，ask，bing等搜索引擎，而且可以配置成当你写了一篇博文后，自动生成xml地图给搜索引擎，使得搜索引擎能尽快知道你博客的更新。我使用了这个插件后，写了一篇博文后，有时候只需要一二十分钟后就能在google里检索到我刚写的博文，非常实用。 Baidu Sitemap Generator 和google XML Sitemaps插件功能类似，也是生成网站的xml地图，方便百度爬取，因为百度的sitemap xml和其他搜索引擎不一样，所以才有了这个插件。这个插件不支持你发文后通知百度。 jadedcoder Sticky Permalinks 这个插件太强悍了，随便你怎么更改你网站上的任何链接，旧的链接会重定向到新的链接，这样你的网站就不会因为更改了链接而丢失任何流量了。 Redirection 这个插件可以根据你配置的模板，把一个页面重定向到另一个页面，由于我的网站上的链接基本都是.htm的结尾，我就配置了把所有的.html结尾的url重定向到对应的.htm结尾的页面。这个插件貌似不支持中文url。 Custom Permalinks 对你博客中的分类，tag，页面，博文进行任意的url自定义，与你的永久链接设置不冲突，非常好用。安装完成后，可以去分类，tag，页面，博文编辑页面，里面有一个“Custom Permalink”选项，可以自己输入任意你喜欢的url。你可以把你的url都弄成&#8221;.htm&#8221;或&#8221;.html&#8221;结尾，让搜索引擎更好的索引你的博客。 All in One SEO Pack 对你的网站做SEO，挺好用的 Lightbox Gallery 这个插件可以让你网站上的图片有jQuery中的lightbox效果，这里有效果演示。安装好这个插件后，在文章你增加相册，会自动加上lightbox效果。但是默认的，弹出的图片是缩略图，把代码改成 ?View Code HTML4STRICT[ gallery lightboxsize=&#34;full&#34; ] 弹出的图片就是原来的图片。 向文章中增加单个图片的时候，默认的没有lightbox效果，在a标签中增加rel=&#8221;lightbox&#8221;就可以使单个图片也有lightbox效果，就像下面这样： ?View Code HTML4STRICT1 2 3 &#60;a href=&#34;image.jpg&#34; rel=&#34;lightbox&#34; title=&#34;this is a caption&#34;&#62; &#60;img src=&#34;thumbnail.jpg&#34; alt=&#34;&#34; /&#62; [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="WordPress" src="screenshots/wordpress.jpg" width="100" height="100"/></p>
<p>最近自己用wordpress把<a href="blog-reborn.htm" target="_blank">博客搭起来了</a>，由于以前在yo2上写博客的时候，wordpress都是yo2已搭好的，插件也都是他们加的，用户不能自己上传插件，少了很多麻烦，也多了一些不方便。这次自己搭博客，由于没有以前yo2上wordpress的插件列表，只有自己根据印象一个的去找插件，找的过程中，也发现了一些比较好的插件，这里我记录下来，做备录用，也供没有搭过wordpress的同志参考。（注：以下插件根据重要性和功能强大性做基本有序排序，这些插件都可以根据名字去wordpress<a href="http://wordpress.org/extend/plugins/" target="_blank">插件官网</a>下载。）<span id="more-40617"></span></p>
<ul>
<li><span style="color: #0000ff;">Google XML Sitemaps</span><br />
    这个插件会生成你网站的xml地图，发送给google，yahoo，ask，bing等搜索引擎，而且可以配置成当你写了一篇博文后，自动生成xml地图给搜索引擎，使得搜索引擎能尽快知道你博客的更新。我使用了这个插件后，写了一篇博文后，有时候只需要一二十分钟后就能在google里检索到我刚写的博文，非常实用。
  </li>
<li><span style="color: #0000ff;">Baidu Sitemap Generator</span><br />
    和google XML Sitemaps插件功能类似，也是生成网站的xml地图，方便百度爬取，因为百度的sitemap xml和其他搜索引擎不一样，所以才有了这个插件。这个插件不支持你发文后通知百度。
  </li>
<li><span style="color: #0000ff;">jadedcoder Sticky Permalinks</span><br />
    这个插件太强悍了，随便你怎么更改你网站上的任何链接，旧的链接会重定向到新的链接，这样你的网站就不会因为更改了链接而丢失任何流量了。
  </li>
<li><span style="color: #0000ff;"><a href="http://ahei.info/t/redirection" class="st_tag internal_tag" rel="tag" title="标签 Redirection 下的日志">Redirection</a></span><br />
    这个插件可以根据你配置的模板，把一个页面重定向到另一个页面，由于我的网站上的链接基本都是.htm的结尾，我就配置了把所有的.html结尾的url重定向到对应的.htm结尾的页面。这个插件貌似不支持中文url。
  </li>
<li><span style="color: #0000ff;">Custom Permalinks</span><br />
    对你博客中的分类，tag，页面，博文进行任意的url自定义，与你的永久链接设置不冲突，非常好用。安装完成后，可以去分类，tag，页面，博文编辑页面，里面有一个“Custom Permalink”选项，可以自己输入任意你喜欢的url。你可以把你的url都弄成&#8221;.htm&#8221;或&#8221;.html&#8221;结尾，让搜索引擎更好的索引你的博客。
  </li>
<li><span style="color: #0000ff;">All in One <a href="http://ahei.info/t/seo" class="st_tag internal_tag" rel="tag" title="标签 seo 下的日志">SEO</a> Pack</span><br />
    对你的网站做SEO，挺好用的
  </li>
<li><span style="color: #0000ff;">Lightbox Gallery</span><br />
    这个插件可以让你网站上的图片有jQuery中的lightbox效果，<a href="emacs.htm" target="_blank">这里</a>有效果演示。安装好这个插件后，在文章你增加相册，会自动加上lightbox效果。但是默认的，弹出的图片是缩略图，把代码改成</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40617code62'); return false;">View Code</a> HTML4STRICT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4061762"><td class="code" id="p40617code62"><pre class="html4strict" style="font-family:monospace;">[ gallery lightboxsize=&quot;full&quot; ]</pre></td></tr></table></div>

<p>    弹出的图片就是原来的图片。<br />
    向文章中增加单个图片的时候，默认的没有lightbox效果，在a标签中增加rel=&#8221;lightbox&#8221;就可以使单个图片也有lightbox效果，就像下面这样：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40617code63'); return false;">View Code</a> HTML4STRICT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4061763"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p40617code63"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image.jpg&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lightbox&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this is a caption&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;thumbnail.jpg&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a>&gt;</span></pre></td></tr></table></div>

</li>
<li><span style="color: #0000ff;">Faster Image Insert</span><br />
    wordpress默认的在文章里增加图片的时候每次只能增加一张，非常麻烦，这个插件可以让你每次可以增加多张，非常方便。
  </li>
<li><span style="color: #0000ff;"><a href="http://ahei.info/t/google-analytics" class="st_tag internal_tag" rel="tag" title="标签 Google Analytics 下的日志">Google Analytics</a> for <a href="http://ahei.info/t/wordpress" class="st_tag internal_tag" rel="tag" title="标签 wordpress 下的日志">WordPress</a></span><br />
    这个插件只需要你填写你的google analytics帐号id，它会自动在你的每个页面增加google analytics的代码，非常不错。
  </li>
<li><span style="color: #0000ff;">WordPress Thread Comment</span><br />
    用树状显示评论，还可以直接在前台管理评论，挺好用的。<a href="auto-complete_yasnippet.htm#comment-72" target="_blank">这里</a>是效果图。
  </li>
<li><span style="color: #0000ff;">WP-CodeBox</span><br />
    非常强大的高亮代码的插件，基于GeSHi, <a href="http://wordpress.org/extend/plugins/wp-syntax/other_notes/" target="_blank">支持的语言</a>非常多，常见的就不用说了，还包括apt_sources，autoit，bash，cmake，diff，email，lisp等等等等，很猛吧。<a href="dea.htm" target="_blank">这里</a>是效果图。
  </li>
<li><span style="color: #0000ff;">WP-Syntax</span><br />
    也是一款基于GeSHi的高亮代码的插件.
  </li>
<li><span style="color: #0000ff;">WP-RecentComments</span><br />
    安装完这个插件后，你可以在你的“小工具”里面增加这个工具，用ajax显示最新的评论，还可以显示评论里面的表情。我的博客右侧栏的“最新评论”就是用这个插件生成的。
  </li>
<li><span style="color: #0000ff;">Twitter Tools</span><br />
    显然，这是一个twitter的插件，你可以在“小工具”里面增加显示最新twitter信息的小工具。我的博客右侧栏的“Twitter”就是用这个插件生成的。还有，当你在博客上发文章的时候，它会自动用你的twitter帐号发送一条tweet，非常不错。
  </li>
<li><span style="color: #0000ff;">Dagon Design Sitemap Generator</span><br />
    为你的博客产生博客地图，我的<a href="map" target="_blank">博客地图</a>就是用这个插件生成的。
  </li>
<li><span style="color: #0000ff;">KB robots.txt</span><br />
    可以直接在wp后台修改robots.txt，安装完之后，你访问你的robots.txt，发现原来的内容已经没了，不用担心，这个插件截获了http://yourblog/robots.txt的请求，把它的插件里面的robots.txt显示而已，你禁用掉这个插件后，你原来的robots.txt又能看到了。
  </li>
<li><span style="color: #0000ff;">My Link Order</span><br />
    对你博客中的链接进行排序，挺实用的。使用的时候，在“小工具”里面把“My Link Order”拖到sidebar里面即可。我这次搭博客的时候，我没有把“My Link Order”拖到sidebar里面，用的还是以前的“链接”，我定义的链接顺序一直没起作用，我还怀疑这个插件不能用了呢，最后才搞明白怎么回事。
  </li>
<li><span style="color: #0000ff;">My Page Order</span><br />
    对你博客中的页面进行排序，也挺实用的
  </li>
<li><span style="color: #0000ff;">Category Order</span><br />
    对你博客中的分类进行排序
  </li>
<li><span style="color: #0000ff;"><a href="http://ahei.info/t/top" class="st_tag internal_tag" rel="tag" title="标签 top 下的日志">Top</a> Level Categories</span><br />
    wordpress默认的分类url是/category/catname, 这个插件为你去掉category,直接变成/catname.
  </li>
<li><span style="color: #0000ff;">Autolink URI</span><br />
    对你的博文中的url自动加上链接，不错。
  </li>
<li><span style="color: #0000ff;">Most Commented Widget</span><br />
    安装完这个插件后，你可以在你的“小工具”里面增加这个工具，可以显示最多的评论。我的博客右侧栏的“最多评论”就是用这个插件生成的。
  </li>
<li><span style="color: #0000ff;">NextGEN Gallery</span><br />
    这是一个相册管理软件，可以批量导入相册。由于默认的wordpress批量导入图片很麻烦，我一般都是用这个插件批量扫描服务器上的图片目录，把它们增加到相册里，而且它也会给这些图片生成缩略图，然后我手工写图片的html。扫描图片之前，要先配置一下它使得它扫描图片时，不要压缩原来的图片。
  </li>
<li><span style="color: #0000ff;">Simple Tags</span><br />
    这是一个管理tag的插件，它可以自动根据你的关键词列表生成你文章中的tag，我文章中的tag就是根据这个插件自动生成的。
  </li>
<li><span style="color: #0000ff;">Permalink Finder</span><br />
    这个插件可以使得即使用户输入错了url，也可以正确到达用户所需要的页面。比如，我有一个页面url是blog-reborn.htm，用户输入blog-reborna.htm也能到达这个页面。挺好用的。
  </li>
<li><span style="color: #0000ff;">akismet</span><br />
    这是一个阻止垃圾评论的插件,可以根据你已有的垃圾评论进行学习,来更好的识别垃圾评论. 我的博客曾经有一段时间被人发大量垃圾评论, 加了这个插件后, 好多了, 自动把它们识别为垃圾评论.
  </li>
<li><span style="color: #0000ff;">WP-UserOnline</span><br />
    这个插件可以看到你博客上的在线用户和爬虫
  </li>
<li><span style="color: #0000ff;">Custom Smilies</span><br />
    为评论增加表情，我的博客的评论里面的表情就是这个插件的效果。
  </li>
<li><span style="color: #0000ff;">Shockingly Simple Favicon</span><br />
    在你的网站首页head标签内增加以下代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40617code64'); return false;">View Code</a> HTML4STRICT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4061764"><td class="code" id="p40617code64"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/link.html"><span style="color: #000000; font-weight: bold;">link</span></a> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;shortcut icon&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;PATH_TO_ICON/favicon.ico&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></td></tr></table></div>

<p>    就可以在地址栏看到你设定的图版。<br />
    Shockingly Simple Favicon这个插件很简单，就是它帮你增加这些代码。我博客的地址栏图标就是这个插件生成的。
  </li>
<li><span style="color: #0000ff;">WordPress Database Backup</span><br />
    如果你没有管理后台数据库的权限，可以用这个插件备份你的数据库，这个插件还可以定时备份，还可以把备份文件发到你的邮箱。
  </li>
<li><span style="color: #0000ff;">WP-PageNavi</span><br />
    wordpress默认的翻页链接只显示前一页和后一页，这个插件可以显示多个翻页链接。我博客最下面的翻页链接就是这个插件生成的。
  </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/wordpress-plugins.htm/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>我的博客重生了</title>
		<link>http://ahei.info/blog-reborn.htm</link>
		<comments>http://ahei.info/blog-reborn.htm#comments</comments>
		<pubDate>Sun, 17 Jan 2010 14:07:50 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[我的生活]]></category>
		<category><![CDATA[A record]]></category>
		<category><![CDATA[ahei]]></category>
		<category><![CDATA[bluehost]]></category>
		<category><![CDATA[byethost]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[emacser]]></category>
		<category><![CDATA[emacser.com]]></category>
		<category><![CDATA[godaddy]]></category>
		<category><![CDATA[se]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[vi]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[xtreemhost]]></category>
		<category><![CDATA[安装]]></category>
		<category><![CDATA[浏览器]]></category>

		<guid isPermaLink="false">http://emacser.com/?p=40508</guid>
		<description><![CDATA[由于伟大的&#8221;最大公约数&#8221;整治网络, 关闭yo2上的所有博客, 我的博客http://ahei.yo2.cn便不能再访问了. 之后试了godaddy的免费空间, 崩溃的是, 安装完WordPress后竟然只能用ie才能正确显示后台管理界面, 其他浏览器都不能正确显示, 貌似没有成功加载css(不过水木网友告诉了我解决办法). 无奈, 再去找了些免费的空间, byethost, xtreemhost, 很失望, 导入文章后, 都出现以下错误信息: ?View Code TEXTThis webpage appears to be infected with a virus. &#160; &#160; If you are the webmaster of this site you should log into your account and check / remove any hidden iframes from the page. Once this is [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="hugege" src="screenshots/hugege.jpg" width="100" height="100"/></p>
<p>由于伟大的&#8221;最大公约数&#8221;整治网络, 关闭yo2上的所有博客, 我的博客<a href="http://ahei.yo2.cn">http://ahei.yo2.cn</a>便不能再访问了. 之后试了godaddy的免费空间, 崩溃的是, 安装完WordPress后竟然只能用ie才能正确显示后台管理界面, 其他浏览器都不能正确显示, 貌似没有成功加载css(不过水木网友告诉了我<a href="http://fivebig.com/blog/2010/01/wordpress-on-godaddy/" target="_blank">解决办法</a>). 无奈, 再去找了些免费的空间, <a href="http://ahei.info/t/byethost" class="st_tag internal_tag" rel="tag" title="标签 byethost 下的日志">byethost</a>, <a href="http://ahei.info/t/xtreemhost" class="st_tag internal_tag" rel="tag" title="标签 xtreemhost 下的日志">xtreemhost</a>, 很失望, 导入文章后, 都出现以下错误信息:<span id="more-40508"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p40508code66'); return false;">View Code</a> TEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4050866"><td class="code" id="p40508code66"><pre class="text" style="font-family:monospace;">This webpage appears to be infected with a virus.
&nbsp;
&nbsp;
If you are the webmaster of this site you should log into your account and check / remove any hidden iframes from the page. Once this is completed the page will display.
&nbsp;
You should also change all passwords for your hosting account control panel, then scan your PC with a recent antivirus product / spyware checker, then update all php scripts on your hosting account to the most recent versions. Then you should view the following documents regarding security
&nbsp;
http://www.google.com/search?q=mysql+injection
&nbsp;
http://en.wikipedia.org/wiki/Cross-site_scripting
&nbsp;
http://www.google.com/search?q=php+script+vulnerabilities
&nbsp;
http://en.wikipedia.org/wiki/Remote_File_Inclusion
&nbsp;
http://en.wikipedia.org/wiki/SQL_injection</pre></td></tr></table></div>

<p>没办法, 还是自己去买个空间吧.</p>
<p>看了一些国外的空间, dreamhost, bluehost都比较贵, 不太适合我这种写点博客消遣的人. 最好在同事的推荐下, 去买了<a href="http://hugege.com/" target="_blank">胡戈戈</a>的空间, 主机在国外, 速度也很不错, 也挺便宜的, 600M硬盘空间/6G带宽每月/可绑定3个顶级域名 100元/年, 很快就把博客搭好了, 基本上没碰到啥问题. 后台管理cPanel也非常的方便. 绑定域名也特别的方便，godaddy的话只需要把你的A record修改为你空间所在的服务器的ip就可以了。</p>
<p>总算, 我的博客重生了. 请订阅了原博客<a href="http://ahei.yo2.cn">http://ahei.yo2.cn</a>的同志重新订阅一下<a href="http://emacser.com">http://emacser.com</a>, 你如果愿意的话。</p>
<p>在我的博客无法访问期间, 对那些关注我的博客的同志, 表示抱歉, 也表示感谢, 正是有了他们的支持, 我才更有动力来和大家一起分享获得知识的愉悦. </p>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/blog-reborn.htm/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>终于注册了一个独立域名</title>
		<link>http://ahei.info/emacser.htm</link>
		<comments>http://ahei.info/emacser.htm#comments</comments>
		<pubDate>Sat, 19 Dec 2009 11:10:07 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[我的生活]]></category>
		<category><![CDATA[ahei]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[emacser]]></category>
		<category><![CDATA[emacser.com]]></category>
		<category><![CDATA[godaddy]]></category>
		<category><![CDATA[paypal]]></category>
		<category><![CDATA[se]]></category>
		<category><![CDATA[域名转向]]></category>

		<guid isPermaLink="false">http://ahei.yo2.cn/?p=40499</guid>
		<description><![CDATA[前阵子, 有朋友建议我注册个独立域名, 买个空间, 那样会稳定些. 昨天同事说godaddy圣诞节前, 搞优惠活动, 申请.com域名时, 只要输入优惠码“BUYCOM99”, 就可以享受0.99美元的优惠价格. 遂也打算去注册一个域名. 先试了下emacs.com, 已被注册, 然后再试了下emacser.com, 没被注册, 太好了. 赶快注册! 注册的时候, 考虑支付方式的时候, 由于看到月光博客上说godaddy会不经本人同意直接刷信用卡, 心有余悸, 所以准备用paypal支付. 立马去注册了一个paypal账号, 刚注册完, 手机收到短信说我的尾数为****的信用卡消费1美元, nnd, 虽然已经听同事说过paypal会刷一点钱, 来验证一下信用卡的有效性, 但还是不爽. 算了, 继续注册我的域名. 等到选择支付方式的时候, 找了半天, 也没找到可以选择paypal进行支付, 我Google之, 无果. 据说现在godaddy也支持支付宝支付, 也Google了半天无果. 真个郁闷, 那我刚才注册的paypal岂不是没用? 白花了1美元. 算了, 狠狠心, 就刷信用卡吧. 刷完后, 域名搞到手, 开始域名转向, 转到我的博客. 据说域名转向要一两个小时, 我就等啊等. 一个下午过去了, 我通过我们公司的国外的服务器能访问我的域名了, 但是国内还是不行, 郁闷. 晚上回来, 就查了一下godaddy域名转向的问题, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="godaddy" src="screenshots/godaddy.gif" width="160" height="70"/></p>
<p>前阵子, 有朋友建议我注册个独立域名, 买个空间, 那样会稳定些. 昨天同事说<a href="https://www.godaddy.com/" target="_blank">godaddy</a>圣诞节前, 搞优惠活动, 申请.com域名时, 只要输入优惠码“BUYCOM99”, 就可以享受0.99美元的优惠价格. 遂也打算去注册一个域名. 先试了下emacs.com, 已被注册, 然后再试了下emacser.com, 没被注册, 太好了. 赶快注册!<span id="more-40499"></span></p>
<p>注册的时候, 考虑支付方式的时候, 由于看到月光博客上说<a href="http://www.williamlong.info/blog/archives/99.html" target="_blank">godaddy会不经本人同意直接刷信用卡</a>, 心有余悸, 所以准备用paypal支付. 立马去注册了一个paypal账号, 刚注册完, 手机收到短信说我的尾数为****的信用卡消费1美元, nnd, 虽然已经听同事说过paypal会刷一点钱, 来验证一下信用卡的有效性, 但还是不爽. 算了, 继续注册我的域名. 等到选择支付方式的时候, 找了半天, 也没找到可以选择paypal进行支付, 我Google之, 无果. 据说现在godaddy也支持支付宝支付, 也Google了半天无果. 真个郁闷, 那我刚才注册的paypal岂不是没用? 白花了1美元. 算了, 狠狠心, 就刷信用卡吧. 刷完后, 域名搞到手, 开始域名转向, 转到我的博客. 据说域名转向要一两个小时, 我就等啊等. 一个下午过去了, 我通过我们公司的国外的服务器能访问我的域名了, 但是国内还是不行, 郁闷.</p>
<p>晚上回来, 就查了一下godaddy域名转向的问题, 原来godaddy的域名转向被棺材店封了, 该死的棺材店! 不过棺材店永远也斗不过&#8221;有着雪亮的眼睛但却不明真相&#8221;的群众, 找到了一篇<a href="http://bleakhand.yo2.cn/articles/godaddy-yu-ming-ding-xiang-di-fang-fa.html" target="_blank">godaddy域名转向</a>的文章, 终于搞定了, 你现在已经能通过<a href="http://emacser.com" target="_blank">http://emacser.com</a>来访问我的博客了. </p>
<p>最后, 我不服气为啥paypal非要扣我一美元, 去Google了一把, 原来是<a href="http://www.google.cn/search?hl=zh-CN&#038;newwindow=1&#038;q=paypal+1.95&#038;btnG=Google+%E6%90%9C%E7%B4%A2&#038;aq=f&#038;oq=" target="_blank">这么回事</a>, 还是感觉不爽, 支付宝关联银行卡的时候, 给我们打几分钱过来, 它却倒好, 给我们扣个2.95美元, nnd. 不过今天去给paypal客服打电话, 客服mm态度不错, 不过有点像机器人, <img src='http://ahei.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/emacser.htm/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>用tsocks代替sockscap来转发网络请求</title>
		<link>http://ahei.info/tsocks.htm</link>
		<comments>http://ahei.info/tsocks.htm#comments</comments>
		<pubDate>Mon, 07 Dec 2009 13:19:30 +0000</pubDate>
		<dc:creator>ahei</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[中级]]></category>
		<category><![CDATA[ahei]]></category>
		<category><![CDATA[genproxy]]></category>
		<category><![CDATA[LD_PRELOAD]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[se]]></category>
		<category><![CDATA[sockscap]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[top]]></category>
		<category><![CDATA[tsocks]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[代理]]></category>
		<category><![CDATA[安装]]></category>
		<category><![CDATA[配置]]></category>
		<category><![CDATA[配置文件]]></category>

		<guid isPermaLink="false">http://ahei.yo2.cn/?p=39137</guid>
		<description><![CDATA[你有没有遇到过这种情况: 某一台机器A的网速特别快, 另外一台机器B和A机器在同一个局域网内, 但是B机器的带宽有限, 由于A机器和B机器由于是在局域网内, 传输速度很快, 所以如果能把B机器的网络请求先发到A, 再由A转发出去, 这样B机器的网速可以一样很快了. 那么怎样来转发请求呢? 很显然, 用代理软件就可以做到. 但是, 我在这里给大家提供一个更简便的架设socks代理的方法, 用ssh服务. ssh的功能巨强大, 大家可以通过它的man详细了解它的功能, 它的man非常详细. 利用ssh架设代理服务主要是利用它的&#8221;-D&#8221;选项, 这个选项后面跟一个ip地址和端口, 格式为ip:port, ip地址为你本机待绑定的ip, 是可选的, 加了这个选项后, 就表示在本机与目标机器之间建立一条ssh通道, 而且在本机监听一个你指定的端口. 我写了一个简单的小函数来开通ssh的代理: ?View Code BASH1 2 3 4 5 6 genproxy &#40;&#41; &#123; ip=&#34;$1&#34;; user=&#34;$2&#34;; ssh -o StrictHostKeyChecking=no &#34;$ip&#34; -l &#34;$user&#34; -D 8888 -N -f &#125; 这个函数的第一个参数是ip, 第二个参数是用户名, &#8220;-o StrictHostKeyChecking=no&#8221;表示目标主机的key未知或者改变过时, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="tsocks" src="screenshots/tsocks-logo.jpg"/></p>
<p>你有没有遇到过这种情况: 某一台机器A的网速特别快, 另外一台机器B和A机器在同一个局域网内, 但是B机器的带宽有限, 由于A机器和B机器由于是在局域网内, 传输速度很快, 所以如果能把B机器的网络请求先发到A, 再由A转发出去, 这样B机器的网速可以一样很快了. 那么怎样来转发请求呢? 很显然, 用代理软件就可以做到. 但是, 我在这里给大家提供一个更简便的架设socks代理的方法, 用ssh服务.<span id="more-39137"></span></p>
<p>ssh的功能巨强大, 大家可以通过它的man详细了解它的功能, 它的man非常详细. 利用ssh架设代理服务主要是利用它的&#8221;-D&#8221;选项, 这个选项后面跟一个ip地址和端口, 格式为ip:port, ip地址为你本机待绑定的ip, 是可选的, 加了这个选项后, 就表示在本机与目标机器之间建立一条ssh通道, 而且在本机监听一个你指定的端口.<br />
我写了一个简单的小函数来开通ssh的代理:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code77'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913777"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p39137code77"><pre class="bash" style="font-family:monospace;">genproxy <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: #007800;">ip</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>;
    <span style="color: #007800;">user</span>=<span style="color: #ff0000;">&quot;$2&quot;</span>;
    <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">StrictHostKeyChecking</span>=no <span style="color: #ff0000;">&quot;<span style="color: #007800;">$ip</span>&quot;</span> <span style="color: #660033;">-l</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$user</span>&quot;</span> <span style="color: #660033;">-D</span> <span style="color: #000000;">8888</span> <span style="color: #660033;">-N</span> <span style="color: #660033;">-f</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>这个函数的第一个参数是ip, 第二个参数是用户名, &#8220;-o StrictHostKeyChecking=no&#8221;表示目标主机的key未知或者改变过时, 不提示. 如果不加这个选项, 你可能会得到类似以下的提示:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code78'); return false;">View Code</a> TEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913778"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p39137code78"><pre class="text" style="font-family:monospace;">The authenticity of host '172.0.1.251 (172.0.1.251)' can't be established.
RSA key fingerprint is 51:18:fe:5f:de:a7:55:ef:7c:d4:6e:ba:bc:9e:a2:7c.
Are you sure you want to continue connecting (yes/no)?</pre></td></tr></table></div>

<p>&#8220;-N&#8221;表示ssh到目标机器后不执行任何命令, 一般不加这个选项的话, 会连接上目标机器后执行你指定的命令, 如果你没有指定任何命令的话, 就直接执行/bin/sh, 所以这个&#8221;-N&#8221;通常会用在这种只需要监听端口的场合.<br />
&#8220;-f&#8221;表示在执行任何命令之前转入到后台进行处理.<br />
上述命令如果执行成功的话, 用netstat能看到本机已经建立一个8888端口, 这时候, 只要有网络请求转发到8888这个端口, ssh会把这个请求通过刚才已经建立好的ssh通道发到目标机器上, 从而达到代理的作用. 那么怎么来把请求发到8888这个端口上呢? 有些软件, 比如qq, 有设置socks代理的功能, 但是有好多软件都没有设置socks代理的功能, 那么对于这些软件该怎么办呢? Windows下sockscap这样的软件, 你可以把一些软件的快捷方式加入到sockscap里面去, 然后要想使用代理的话, 就直接在sockscap里面来启动软件. 很方便. 那么linux下是否也有这类软件呢? 当然有, 而且更方便.</p>
<p>tsocks就是一款类似sockscap的网络请求转发的软件.<br />
使用很简单, 安装完tsocks后, 打开它的配置文件/etc/<a href="http://ahei.info/t/tsocks" class="st_tag internal_tag" rel="tag" title="标签 tsocks 下的日志">tsocks</a>.conf, 翻到文件末尾, 里面有一个server和server_port的选项, 这个就是socks server的ip和端口, 分别填上即可. 要注意的地方就是, 对于上面那个例子, 在B机器上配置tsocks的时候, server应该写127.0.0.1, 而不是A机器的ip, 因为8888端口是在B机器上开启的. 我今天配置的时候, 就犯了这个错误, tsocks提示&#8221;socks server is not on a local subnet local&#8221;, 很诡异, 弄了半天才明白. 切记切记.<br />
配置好后, 直接tsocks后面跟命令就可以了, 比如</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code79'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913779"><td class="code" id="p39137code79"><pre class="bash" style="font-family:monospace;">tsocks <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.g.cn</pre></td></tr></table></div>

<p>就可以使用代理来wget了. 但是这样的话, 就必须要在每个命令前都要加tsocks. 还有一个更简单的方法:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code80'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913780"><td class="code" id="p39137code80"><pre class="bash" style="font-family:monospace;">. tsocks <span style="color: #660033;">-on</span></pre></td></tr></table></div>

<p>注意了, 上面这个命令前面有一个点号, 必须要的, 我今天在配置的时候, 也是没加, 弄了半天都不行, 后来仔细看了tsocks的man才知道了. 那么为什么要加点号呢?<br />
shell里面,</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code81'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913781"><td class="code" id="p39137code81"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>test.sh</pre></td></tr></table></div>

<p>shell会开启一个子shell进程来执行test.sh, test.sh里面所有影响环境变量的语句对它的父shell进程都没有影响, 而</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code82'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913782"><td class="code" id="p39137code82"><pre class="bash" style="font-family:monospace;">. .<span style="color: #000000; font-weight: bold;">/</span>test.sh</pre></td></tr></table></div>

<p>不是单独开启一个shell进程, 而是在当前shell下执行test.sh, 这样test.sh里面对环境变量影响的语句在当前shell就起作用了.<br />
知道上面加与不加点号的区别后, 我们再来看看tsocks的源码:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code83'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913783"><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
37
38
39
40
41
42
43
</pre></td><td class="code" id="p39137code83"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
	-on<span style="color: #7a0874; font-weight: bold;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$LD_PRELOAD</span>&quot;</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;">export</span> <span style="color: #007800;">LD_PRELOAD</span>=<span style="color: #ff0000;">&quot;/usr/lib/libtsocks.so&quot;</span>
			<span style="color: #000000; font-weight: bold;">else</span>
				<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$LD_PRELOAD</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-q</span> <span style="color: #ff0000;">&quot;/usr/lib/libtsocks\.so&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> \
				<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LD_PRELOAD</span>=<span style="color: #ff0000;">&quot;/usr/lib/libtsocks.so <span style="color: #007800;">$LD_PRELOAD</span>&quot;</span>
		<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #000000; font-weight: bold;">;;</span>
	-off<span style="color: #7a0874; font-weight: bold;">&#41;</span>
		<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LD_PRELOAD</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #007800;">$LD_PRELOAD</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/\/usr\/lib\/libtsocks.so \?//'</span><span style="color: #000000; font-weight: bold;">`</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$LD_PRELOAD</span>&quot;</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;">unset</span> LD_PRELOAD
		<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #000000; font-weight: bold;">;;</span>
	-show<span style="color: #000000; font-weight: bold;">|</span>-sh<span style="color: #7a0874; font-weight: bold;">&#41;</span>
		<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;LD_PRELOAD=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$LD_PRELOAD</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>
	<span style="color: #000000; font-weight: bold;">;;</span>
	-h<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: #ff0000;">&quot;$0: Please see tsocks(1) or read comment at top of $0&quot;</span>
   <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: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$LD_PRELOAD</span>&quot;</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;">export</span> <span style="color: #007800;">LD_PRELOAD</span>=<span style="color: #ff0000;">&quot;/usr/lib/libtsocks.so&quot;</span>
		<span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$LD_PRELOAD</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-q</span> <span style="color: #ff0000;">&quot;/usr/lib/libtsocks\.so&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> \
			<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LD_PRELOAD</span>=<span style="color: #ff0000;">&quot;/usr/lib/libtsocks.so <span style="color: #007800;">$LD_PRELOAD</span>&quot;</span>
		<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> = <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
		<span style="color: #000000; font-weight: bold;">then</span>
			<span style="color: #800000;">${SHELL:-/bin/sh}</span>
		<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</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;">exec</span> <span style="color: #ff0000;">&quot;$@&quot;</span>
		<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></td></tr></table></div>

<p>从上面可以看出, tsocks这个脚本是通过修改LD_PRELOAD这个环境变量来达到它的转发网络请求的目的. 那为什么修改LD_PRELOAD这个环境变量就能达到他的转发网络请求的目的呢? 说简单点, LD_PRELOAD这个环境变量表示系统会把这个变量对应的共享库文件中的函数来覆盖目标程序中的函数, 详情请看<a href="http://blog.csdn.net/haoel/archive/2007/05/09/1602108.aspx" target="_blank">这里</a>.<br />
现在你应该明白了为什么tsoc -on前面为什么要加点号了吧? 你不加点号的话, tsocks脚本修改的LD_PRELOAD变量不对当前的shell进程起作用啊.<br />
把</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code84'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913784"><td class="code" id="p39137code84"><pre class="bash" style="font-family:monospace;">. tsocks <span style="color: #660033;">-on</span></pre></td></tr></table></div>

<p>加到你的.bashrc里面, 这样每次打开新的shell会话都可以使用代理. 如果想所有的软件都使用代理的话, 包括不是在shell里面启动的, 重启一下机器, 使得你的.bashrc里面的配置对全局生效.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code85'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913785"><td class="code" id="p39137code85"><pre class="bash" style="font-family:monospace;">. tsocks <span style="color: #660033;">-off</span></pre></td></tr></table></div>

<p>关闭tsocks,</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39137code86'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3913786"><td class="code" id="p39137code86"><pre class="bash" style="font-family:monospace;">. tsocks <span style="color: #660033;">-sh</span></pre></td></tr></table></div>

<p>显示LD_PRELOAD的值. </p>
]]></content:encoded>
			<wfw:commentRss>http://ahei.info/tsocks.htm/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
