<?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>shell while read line &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/shell-while-read-line/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 28 Apr 2020 02:15:05 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]一些未归类的命令、操作方法或问题总结（4）</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%b8%80%e4%ba%9b%e6%9c%aa%e5%bd%92%e7%b1%bb%e7%9a%84%e5%91%bd%e4%bb%a4%e3%80%81%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95%e6%88%96%e9%97%ae%e9%a2%98%e6%80%bb%e7%bb%93%ef%bc%884/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%b8%80%e4%ba%9b%e6%9c%aa%e5%bd%92%e7%b1%bb%e7%9a%84%e5%91%bd%e4%bb%a4%e3%80%81%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95%e6%88%96%e9%97%ae%e9%a2%98%e6%80%bb%e7%bb%93%ef%bc%884/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 27 Dec 2011 07:42:51 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[shell while read line]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=4048</guid>

					<description><![CDATA[<p>下面是一些未归类的命令及操作方法，专门记在此文中，以便将来查询。由于文章较长，请用Ctrl+F查询关键字来定位到你需要的内容。</p>
<p><span style="background-color:#00ff00;">（1）</span>用shell对某个数据文件的第二列求和</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">cat&#160;a.txt&#160;&#124;&#160;awk&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;{print&#160;$2}&#39;</span>&#160;&#124;&#160;awk&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;BEGIN{sum=0}{sum+=$1}END{print&#160;sum}&#39;</span></code></pre>
</section>
<p>其中，a.txt即为数据文件。</p>
<p><span style="background-color:#00ff00;">（2）</span>echo输出制表符(tab，\t)<br />
要用echo语句输出制表符\t到文件中，需要加 -e 参数：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">A=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;mine&#34;</span>;
B=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;yours&#34;</span>;
<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&#160;-e&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$A</span>\t<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$B</span>&#34;</span>&#160;&#62;&#160;1.txt
</code></pre>
</section>
<p>如果不加 -e 参数，则输出文件中会有一个 \t 字符串，而不是一个制表符。<br />
<span id="more-4048"></span><br />
<span style="background-color:#00ff00;">（3）</span>在Xshell中鼠标选中文字时自动换行的问题<br />
如果你用Xshell，并且遇到了&#8220;用鼠标选中一段文字时自动换行&#8221;的问题，那么你有可能是使用了网易的&#8220;有道词典&#8221;导致的。当你不小心按了某组合快捷键的时候，就有可能触发有道词典的某个功能，从而导致你在Xshell中选中文字时自动换行。这个现象非常令人讨厌，使用别的词典软件都没有这个问题。<br />
退出有道词典就可以解决这个问题了。<br />
我想知道的是：网易的有道词典开发人员中没有使用Xshell的？</p>
<p><span style="background-color:#00ff00;">（4）</span>一个在shell中向一批手机号发送报警短信的例子<br />
假设你要在shell中向一批手机号发送报警短信，可以如下例操作：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">PHONES=(136XXXXXXXX&#160;137XXXXXXXX&#160;138XXXXXXXX)

<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&#160;PHONE_NUMBER&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&#160;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">${PHONES[*]}</span>
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">do</span>
&#160;&#160;&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">TODO:</span>send&#160;SMS&#160;message&#160;to&#160;$PHONE_NUMBER</span>

&#160;&#160;&#160;&#160;sleep&#160;2
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">done</span></code></pre>
</section>
<p><span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%b8%80%e4%ba%9b%e6%9c%aa%e5%bd%92%e7%b1%bb%e7%9a%84%e5%91%bd%e4%bb%a4%e3%80%81%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95%e6%88%96%e9%97%ae%e9%a2%98%e6%80%bb%e7%bb%93%ef%bc%884/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>下面是一些未归类的命令及操作方法，专门记在此文中，以便将来查询。由于文章较长，请用Ctrl+F查询关键字来定位到你需要的内容。</p>
<p><span style="background-color:#00ff00;">（1）</span>用shell对某个数据文件的第二列求和</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">cat&nbsp;a.txt&nbsp;|&nbsp;awk&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;{print&nbsp;$2}&#39;</span>&nbsp;|&nbsp;awk&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;BEGIN{sum=0}{sum+=$1}END{print&nbsp;sum}&#39;</span></code></pre>
</section>
<p>其中，a.txt即为数据文件。</p>
<p><span style="background-color:#00ff00;">（2）</span>echo输出制表符(tab，\t)<br />
要用echo语句输出制表符\t到文件中，需要加 -e 参数：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">A=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;mine&quot;</span>;
B=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;yours&quot;</span>;
<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;-e&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$A</span>\t<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$B</span>&quot;</span>&nbsp;&gt;&nbsp;1.txt
</code></pre>
</section>
<p>如果不加 -e 参数，则输出文件中会有一个 \t 字符串，而不是一个制表符。<br />
<span id="more-4048"></span><br />
<span style="background-color:#00ff00;">（3）</span>在Xshell中鼠标选中文字时自动换行的问题<br />
如果你用Xshell，并且遇到了&ldquo;用鼠标选中一段文字时自动换行&rdquo;的问题，那么你有可能是使用了网易的&ldquo;有道词典&rdquo;导致的。当你不小心按了某组合快捷键的时候，就有可能触发有道词典的某个功能，从而导致你在Xshell中选中文字时自动换行。这个现象非常令人讨厌，使用别的词典软件都没有这个问题。<br />
退出有道词典就可以解决这个问题了。<br />
我想知道的是：网易的有道词典开发人员中没有使用Xshell的？</p>
<p><span style="background-color:#00ff00;">（4）</span>一个在shell中向一批手机号发送报警短信的例子<br />
假设你要在shell中向一批手机号发送报警短信，可以如下例操作：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">PHONES=(136XXXXXXXX&nbsp;137XXXXXXXX&nbsp;138XXXXXXXX)

<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;PHONE_NUMBER&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">${PHONES[*]}</span>
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">do</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">TODO:</span>send&nbsp;SMS&nbsp;message&nbsp;to&nbsp;$PHONE_NUMBER</span>

&nbsp;&nbsp;&nbsp;&nbsp;sleep&nbsp;2
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">done</span></code></pre>
</section>
<p><span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
其中，&ldquo;TODO&rdquo;的那部分是调用你的短信发送接口（通常是一个URL），这里无法举例。</p>
<p><span style="background-color:#00ff00;">（5）</span>去掉WordPress的Auto SyntaxHighlighter插件右上角的问号<br />
使用Auto SyntaxHighlighter插件的人都知道，3.0.83版本的这个插件会在高亮语法块的右上角生成一个小问号，颜色特别难看，要去掉它的话，需要修改其源码。按网上的说法，修改&nbsp;<span style="color:#0000ff;">wp-content/plugins/auto-syntaxhighlighter/SyntaxHighlighter/src/styles/shCore.css</span> 文件，找到：</p>
<blockquote>
<div>
		.syntaxhighlighter .toolbar {</div>
<div>
		&nbsp; position: absolute !important;</div>
<div>
		&nbsp; right: 1px !important;</div>
<div>
		&nbsp; top: 1px !important;</div>
<div>
		&nbsp; width: 11px !important;</div>
<div>
		&nbsp; height: 11px !important;</div>
<div>
		&nbsp; font-size: 10px !important;</div>
<div>
		&nbsp; z-index: 10 !important;</div>
<div>
		}</div>
</blockquote>
<p>加一行 <span style="color:#ff0000;">display: none;</span> 变成：</p>
<blockquote>
<div>
		.syntaxhighlighter .toolbar {</div>
<div>
		&nbsp; position: absolute !important;</div>
<div>
		&nbsp; right: 1px !important;</div>
<div>
		&nbsp; top: 1px !important;</div>
<div>
		&nbsp; width: 11px !important;</div>
<div>
		&nbsp; height: 11px !important;</div>
<div>
		&nbsp; font-size: 10px !important;</div>
<div>
		&nbsp; z-index: 10 !important;</div>
<div>
		&nbsp; display: none;</div>
<div>
		}</div>
</blockquote>
<p>就行了，但是在我这里试验的结果是：<span style="color:#ff0000;">无效</span>。究其原因，是由&nbsp;<span style="color:#0000ff;">wp-content/plugins/auto-syntaxhighlighter/auto-syntaxhighlighter.php</span> 文件定义的：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="php language-php hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">wp_register_style(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;ash_core&#39;</span>,&nbsp;ASH_PLUGIN_URL.<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;SyntaxHighlighter/build/styles/shCore-min.css&#39;</span>,&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">array</span>(),&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">$this</span>-&gt;_shlver);
</code></pre>
</section>
<p>所以，有效的方法是，修改文件&nbsp;<span style="color:#0000ff;">wp-content/plugins/auto-syntaxhighlighter/SyntaxHighlighter/build/styles/shCore-min.css</span>，按上面的方法做同样的修改，就搞定了。这个文件是&ldquo;紧凑版&rdquo;的shCore.css，里面的代码非常不适合阅读，但是其小体积显然节约了网络流量。</p>
<p><span style="background-color:#00ff00;">（6）</span>shell中循环读取文本文件的每一行<br />
假设有如下数据文件：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">[root@localhost&nbsp;~]$&nbsp;cat&nbsp;1.txt&nbsp;
123
abc
def
</code></pre>
</section>
<p>如果要读取此文件的每一行，并进行一些处理，怎么做？这里把情况设想得简单一点，我们就统计一下行数（当然 wc -l 可以直接统计行数，不用这么麻烦，此处只是为了说明问题才这样做）。<br />
方法A：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">[root@localhost&nbsp;~]$&nbsp;cat&nbsp;wrong.sh&nbsp;
<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">#!/bin/bash
</span>
COUNT=0
cat&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;1.txt&#39;</span>&nbsp;|&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">read</span>&nbsp;LINE
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">do</span>
&nbsp;&nbsp;COUNT=$((COUNT+1))
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">done</span>

<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$COUNT</span></code></pre>
</section>
<p>输出结果：0<br />
<span style="color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; text-align: left; background-color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
方法B：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">[root@localhost&nbsp;~]$&nbsp;cat&nbsp;right.sh&nbsp;
<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">#!/bin/bash
</span>
COUNT=0
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">read</span>&nbsp;LINE
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">do</span>
&nbsp;&nbsp;COUNT=$((COUNT+1))
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">done</span>&nbsp;&lt;&nbsp;&nbsp;1.txt

<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$COUNT</span></code></pre>
</section>
<p>输出结果：3<br />
显然，方法B是正确的，方法A是错误的。但是为什么呢？方法A看上去同样无比正确，但是却得到了错误的结果。这是因为方法A采用管道的方式，使得脚本在做do ... done循环的时候，实际上是另启了一个进程，从而在里面的计数不会反映到外面去，所以无法计到数。<br />
这个例子说明了，shell中的各种写法可能有不同的陷阱，如果你不知道，那么就会浪费很多时间去调试。</p>
<p><span style="background-color:#00ff00;">（7）</span>shell中换行符(\n)的字符串拼接<br />
如果要在shell中将换行符 \n 拼接到一个字符串中，可以这样做：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">B=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;str&quot;</span>
A=<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$B</span>$<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;\n&#39;</span></code></pre>
</section>
<p>这样变量A中得到的就是一个含有换行符的字符串。</p>
<p><span style="background-color:#00ff00;">（8）</span>VS 2005中出现&ldquo;<span style="color:#0000ff;">fatal error RC1107: invalid usage; use RC /? for Help</span>&rdquo;错误的解决办法<br />
如果你打开一个VC++ 2005项目的资源视图时，出现了这个错误的话，那么，一个很可能有用的解决办法就是：在<span style="color:#0000ff;">&ldquo;工具&rdquo;&rarr;&ldquo;选项&rdquo;&rarr;&ldquo;项目和解决方案&rdquo;&rarr;&ldquo;VC++目录&rdquo;</span>中，把&ldquo;包含文件&rdquo;、&ldquo;引用文件&rdquo;、&ldquo;库文件&rdquo;等所有路径中，最后的&ldquo;<strong><span style="color:#ff0000;">\</span></strong>&rdquo;都去掉，然后再重新打开工程，应该就没有问题了。<br />
这应该是一个VS 2005的bug。<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">（9）</span>如何配置log4j，使得可以将 INFO、ERROR 的日志输出到一个文件，其中 ERROR 的日志还同时会输出到另一个文件<br />
请看<a href="http://www.codelast.com/?p=3081" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">此链接</span></a>的第(48)条。</p>
<p><span style="background-color:#00ff00;">（10）</span>在Unicode字符集下，将CString转成const char*的方法</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="cpp language-cpp hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">CString&nbsp;strCS&nbsp;=&nbsp;_T(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;This&nbsp;is&nbsp;a&nbsp;test&nbsp;string.&quot;</span>);
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">char</span>&nbsp;szStr[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">256</span>]&nbsp;=&nbsp;{<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">0</span>};
wcstombs(szStr,&nbsp;strCS,&nbsp;strCS.GetLength());
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">const</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">char</span>&nbsp;*p&nbsp;=&nbsp;szStr;
</code></pre>
</section>
<p>网上找到的方法，有用。</p>
<p><span style="background-color:#00ff00;">（11）</span>正则匹配TAB（制表符）<br />
假设要在Linux命令行下使用grep正则匹配TAB键（制表符），那么直接输入一个TAB就可以了：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">grep&nbsp;-o&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;abc&nbsp;&nbsp;&nbsp;&nbsp;123&quot;</span>&nbsp;file.txt
</code></pre>
</section>
<p>其中，&ldquo;abc&rdquo;和&ldquo;123&rdquo;之间是一个TAB，但是要注意，你在Linux命令行下是无法通过按键盘上的Tab键来输入一个TAB的，因为TAB已经被定义为补全命令的快捷键了。这时候，你需要先按<span style="color:#0000ff;">Ctrl+V</span>，再按<span style="color:#0000ff;">Ctrl+I</span>，就可以输入一个TAB了。</p>
<p><span style="background-color:#00ff00;">（12）</span>在shell中判断：当不是以某用户身份执行脚本时，退出</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">#!/bin/bash
</span>
ROLE=`whoami`
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;[[&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$ROLE</span>&quot;</span>x&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;root&quot;</span>x&nbsp;]];&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">then</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;You&nbsp;are&nbsp;running&nbsp;as&nbsp;root.&quot;</span>
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">else</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;You&nbsp;are&nbsp;running&nbsp;as&nbsp;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$ROLE</span>,&nbsp;please&nbsp;switch&nbsp;to&nbsp;root&nbsp;first!&quot;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">exit</span>
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">fi</span>

<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Now&nbsp;you&nbsp;can&nbsp;run&nbsp;below&nbsp;program.&quot;</span></code></pre>
</section>
<p>上面的脚本做的事：当不是以root身份运行此脚本时，提示用户并退出。其实相当简单，没什么好说的，关键就在字符串相等的判断上。</p>
<p><span style="background-color:#00ff00;">（13）</span>在Emacs中显示UTF-8编码的文件内容/显示中文<br />
对UTF-8编码、含中文的文本文件，要用Emacs打开它时能正常显示，需要做以下设置：<br />
①在你的Linux用户所对应的Emacs配置文件 .emacs 中，添加以下内容：</p>
<blockquote>
<div>
		(prefer-coding-system&nbsp; &nbsp; &nbsp; &nbsp;&#39;utf-8)</div>
<div>
		(set-default-coding-systems &#39;utf-8)</div>
<div>
		(set-terminal-coding-system &#39;utf-8)</div>
<div>
		(set-keyboard-coding-system &#39;utf-8)</div>
<div>
		(setq default-buffer-file-coding-system &#39;utf-8)</div>
<div>
		(setq x-select-request-type &#39;(UTF8_STRING COMPOUND_TEXT TEXT STRING))</div>
</blockquote>
<p>这一段我是从网上抄来的，之后拿一个UTF-8(无BOM)编码、含中文的文件测试，发现在一台Linux server上有效，在另一台Linux server上则无效(仍然乱码)。最后找到原因是：跟Linux系统的locale设置有关，于是就要继续设置如下。<br />
②在.bash_profile中添加：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
LANG=zh_CN.UTF-8
</pre>
<p>然后重新登录终端，就可以用Emacs打开、显示含中文的文件了。当然，前提是：你的终端软件（例如Xshell）也必须要把encoding设置成Unicode(UTF-8)的。<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
以上整个过程都必须配合设置好，而很多人可能没发现缺少了一个环节，就导致了显示乱码，确实会比较烦人。</p>
<p><span style="background-color:#00ff00;">（14）</span>在Xshell终端里启用Emacs的Meta key<br />
Xshell设置选项如下：<br />
<a href="http://www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="xshell emacs meta key" src="http://www.codelast.com/wp-content/uploads/2012/03/xshell_emacs_meta_key.png" style="width: 486px; height: 171px; " /></a><br />
不设置的话，就没有办法在Emacs中使用 Alt+x 来触发一些功能，例如在Emacs中使用gdb调试的时候运行gdb的快捷键 M-x gdb。</p>
<p><span style="background-color:#00ff00;">（15）</span>gdb调试C++程序，设置断点时提示&ldquo;<span style="color:#b22222;">No source file named XXX</span>&rdquo;错误的原因<br />
原因可能有多种，我遇到的一种是：编译C++程序时没有添加-g参数导致编译出来的程序中不含调试信息，从而在gdb命令行下无论怎么设置断点（绝对路径或相对路径），它都提示&ldquo;<span style="color: rgb(178, 34, 34); ">No source file named XXX</span>&rdquo;错误，让我以为是路径的原因。所以大家千万要注意了，不要找了半天是这个低级错误啊！<br />
另外，你也可以参考一下<a href="http://stackoverflow.com/questions/1735539/gdb-debugger-problems-no-source-file-named" rel="noopener noreferrer" target="_blank">这个</a>链接。</p>
<p><span style="background-color:#00ff00;">（16）</span>grep高亮导致的字符串长度计算错误问题<br />
在Linux命令行下，你可以配置grep的结果高亮显示（参看<a href="http://www.codelast.com/?p=3645" rel="noopener noreferrer" target="_blank">这个</a>链接的第26条），效果如下：<br />
<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2012/03/grep_highlight.png" style="width: 320px; height: 54px; " /><br />
这个非常有帮助的功能，却有可能给你的shell脚本造成致命的隐患&mdash;&mdash;有时会导致你在计算字符串长度时出错。<br />
例如，有一个数据文件 a.txt 只有一行内容：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">[root@localhost&nbsp;~]<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;cat&nbsp;a.txt&nbsp;</span>
1234567890
</code></pre>
</section>
<p>另外一个脚本 a.sh 想要统计 a.txt 的第一行的字符数有多少：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">#!/bin/bash&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>
A=`cat&nbsp;a.txt`
LEN=`expr&nbsp;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">${#A}</span>`

<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$LEN</span></code></pre>
</section>
<p>结果如你所想的一样，输出为：10<br />
但是，假设我们要求第一行必须是含&ldquo;12&rdquo;的，则应该添加上 grep 来过滤一下：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">#!/bin/bash&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>
A=`cat&nbsp;a.txt&nbsp;|&nbsp;grep&nbsp;12`
LEN=`expr&nbsp;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">${#A}</span>`

<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$LEN</span></code></pre>
</section>
<p>再执行，这时候你会发现，结果很&ldquo;神奇&rdquo;地变成了：25<br />
怎么会有25个字符呢？这明明是&ldquo;睁眼说瞎话&rdquo;啊！<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
这就是按前面所说的方法添加了 grep 高亮显示的之后的结果。不信，你可以去掉.bash_profile中的grep高亮显示代码，再重新登录终端再执行脚本，一定就不会有这样的问题了。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（17）</span>删除文件之后，df -h显示的磁盘空间占用没有刷新的问题<br />
有时候你会发现，刚删除了一个很大的文件，而用 df -h 查看的磁盘空间占用还是那么多，没有释放，这可能是由于被删除的文件仍然被其他进程占用导致的。例如，用 <span style="color:#ff0000;">nohup 程序名 &amp;</span> 启动了一个程序，会生成一个 nohup.out 文件，程序的输出都会写入这个文件中，这个文件越写越大，当大到一定程度时，你删除了这个文件，然后马上用 df -h 查看，磁盘空间并没有释放。这是因为你的程序还在占用 nohup.out 文件，如果你把你的进程kill掉，会发现df -h的结果马上就刷新了。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（18）</span>如何在不切换用户的情况以，以指定的用户运行某一命令</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">su&nbsp;abc&nbsp;-c&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;touch&nbsp;1.txt&#39;</span></code></pre>
</section>
<p>表示以用户&ldquo;abc&rdquo;的身份运行后面引号中的命令。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（19）</span>在vim中如何显示特殊字符<br />
用vim打开一个文件，要显示其中的特殊字符（例如TAB）/取消显示，分别可以用如下两条命令：</p>
<blockquote>
<div>
		:set list</div>
<div>
		:set nolist</div>
</blockquote>
<p>在显示特殊字符的情况下，TAB键将被标识为&ldquo;<span style="font-family:comic sans ms,cursive;"><span style="color:#ff0000;">^I</span></span>&rdquo;（不包含引号）。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（20）</span>在shell中判断当前登录的Linux用户</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">USER=`whoami`
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;[[&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;<span class="hljs-variable" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(98, 151, 85); word-wrap: inherit !important; word-break: inherit !important;">$USER</span>&quot;</span>x&nbsp;!=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;root&quot;</span>x&nbsp;]];&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">then</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Must&nbsp;run&nbsp;as&nbsp;root.&quot;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">exit</span>&nbsp;1
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">fi</span></code></pre>
</section>
<p>以上代码的作用是：当执行此脚本的用户不是root时，退出。如果要判断&ldquo;是root&rdquo;，则把 != 改为 = 即可。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（21）</span>已知某项目SVN地址，如何查看其所有提交日志<br />
使用如下命令：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">svn&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">log</span>&nbsp;http://android-wifi-tether.googlecode.com/svn/trunk
</code></pre>
</section>
<p>其中，后面的URL是项目的SVN地址。只要你有权限，那么就可以输出所有人提交代码时写的日志。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（22）</span>PHP输出某一文件的内容<br />
连这样弱爆了的内容都给记下来了：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="php language-php hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">&lt;?php</span>
$xml_contents&nbsp;=&nbsp;file_get_contents(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;./1.xml&#39;</span>);
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">echo</span>&nbsp;$xml_contents;
<span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">?&gt;</span></code></pre>
</section>
<p>这样，在访问这个PHP页面的时候，就会看到页面输出与该PHP文件处于同一目录下的 1.xml 文件的内容了。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（23）</span>svn diff 命令的输出重定向到文件时，里面包含中文的问题<br />
<span style="font-size: 13.63636302947998px; background-color: rgb(255, 255, 255);">①</span>对Windows:如果你在Windows下安装了SlikSVN作为你的subversion命令行工具，那么你可能会使用svn diff命令来输出代码修改的差异。但是，如果你的Windows是中文版的话，你会发现输出的diff文件是内含中文的（例如&ldquo;工作副本&rdquo;之类的词语），在配合Review Board系统使用的时候，如果你提交了这个内含中文的diff文件，Review Board系统会出问题&mdash;&mdash;提示你文件损坏之类的，所以，要避免这个问题，就要保证diff文件不含中文，方法就是：到SlikSVN安装目录下的 Share\Locale 目录下，将里面的文件全部删除，这样，SlikSVN就只能输出英文语言的内容了。<br />
<span style="font-size: 13.600000381469727px; background-color: rgb(255, 255, 255);">②</span>对Ubuntu，也可能遇到svn diff命令输出中文的问题。这时你需要修改一个环境变量：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
export LANGUAGE=en_US.UTF-8
</pre>
<p>通常我是把这句话写在登录用户的.bashrc文件里，让它自动执行。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（24）</span>Xshell的Sessions/会话/连接配置文件保存在哪里<br />
如果是在Win7下，它可能在这个位置：<br />
<span style="color:#0000ff;">C:\Users\用户名\AppData\Roaming\NetSarang\Xshell\Sessions</span><br />
要确定具体的位置，可以打开Xshell，菜单&ldquo;File&rdquo;&rarr;&ldquo;Open&rdquo;，在打开的对话框中点击&ldquo;Open Session Folder&rdquo;按钮，就会打开一个文件夹，这个文件夹就是保存了所有连接信息的文件夹，拷贝到别的计算机上就可以不用重新输入那些繁琐的服务器信息啦。</p>
<p><span style="background-color: rgb(0, 255, 0); ">（24）</span>awk的rshift函数<br />
详见<a href="http://www.gnu.org/software/gawk/manual/html_node/Bitwise-Functions.html" rel="noopener noreferrer" target="_blank">此处</a>。</p>
<blockquote>
<div>
		rshift(val, count)</div>
<div>
		Return the value of val, shifted right by count bits.</div>
</blockquote>
<div>
	即：将val右移count位。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（25）</span>Win7的&ldquo;未能连接一个Windows服务&rdquo;错误的解决办法<br />
	在系统托盘处弹出一个图标，提示&ldquo;未能连接一个Windows服务&rdquo;，错误的具体内容为：</div>
<blockquote>
<div>
		Windows无法连接到Syetem Event Notification Service服务。此问题阻止标准用户登录系统。作为管理员用户，您可以复查系统事件日志，以获得有关此服务未响应原因的详细信息。</div>
</blockquote>
<div>
	这个错误会导致无法连接网络。<br />
	我Google了一下，查到一个解决办法，经试验有效（但不保证对所有此问题的系统都有效）&mdash;&mdash;<br />
	在命令行下执行命令：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
NETSH WINSOCK RESET CATALOG
</pre>
<p>	无论系统提示或不提示错误，重启系统看看是不是好了。如果仍然不行，那么请移步Google查找其他解决方案。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（26）</span>运行shell脚本时提示&ldquo;/bin/sh^M: bad interpreter: XXX&rdquo;问题的解决办法<br />
	原因是换行符不对，用Emacs修改很简单：<br />
	Ctrl+x &rarr; ENTER &rarr; f &rarr; ENTER &rarr; 保存退出</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（27）</span>在CentOS 5.8上安装git<br />
	在CentOS 5.8上，如果你想yum install git，会提示找不到软件包，为了能yum install git，你需要先安装一个rpm包：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
rpm -ivh http://mirror.chpc.utah.edu/pub/epel/5/i386/epel-release-5-4.noarch.rpm
</pre>
<p>	如果是64位的系统，那么就用：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
rpm -ivh http://mirror.chpc.utah.edu/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
</pre>
<p>	然后就可以用yum install来安装啦。<br />
	有人会问，EPEL是什么？请看<a href="https://fedoraproject.org/wiki/EPEL/zh-cn" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这个</span></a>链接。</p>
<blockquote>
<p>
			企业版 Linux 附加软件包(以下简称 EPEL)是一个由特别兴趣小组创建、维护并管理的，针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS、 Scientific Linux)的一个高质量附加软件包项目。</p>
</blockquote>
<p>
	<span style="background-color: rgb(0, 255, 0); ">（28）</span>Android连接上VPN之后还是无法打开被block的webpage的问题<br />
	原因可能有千万种，解决办法也可能不相同，这里有一法，你可以试验一下，如果无效，请移步Goolge另寻其他方法。<br />
	使用Root Explorer软件将 /etc/ppp/ip-up-vpn 文件的权限改为777即可。注意在修改之前要点击&ldquo;Mount R/W&rdquo;，否则不能修改权限。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（29）</span>Nginx的error log中记录&ldquo;<span style="color:#0000ff;">FastCGI sent in stderr: &quot;Primary script unknown&quot; while reading response header from upstream</span>&rdquo;错误的解决办法<br />
	按照<a href="http://serverfault.com/questions/418983/nginx-fastcgi-primary-script-unknown-when-configuring-userdir" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章的说法，应该把fastcgi.conf配置文件中的&ldquo;<span style="color:#a52a2a;">fastcgi_param &nbsp;SCRIPT_FILENAME &nbsp; &nbsp;$document_root$fastcgi_script_name</span>&rdquo;改为&ldquo;<span style="color:#0000ff;">fastcgi_param &nbsp;SCRIPT_FILENAME &nbsp; &nbsp;$request_filename</span>&rdquo;。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（30）</span>Win7更新了最新的Nvidia显卡驱动后，系统颜色变得过于鲜艳的问题<br />
	要调整回一个&ldquo;正常&rdquo;的颜色，需要打开&ldquo;<span style="color:#0000ff;">Nvidia控制面板</span>&rdquo;（即<span style="color:#0000ff;">nvcplui.exe</span>程序，可能不同显卡对应的程序名不同），在&ldquo;<span style="color:#b22222;">显示</span>&rdquo;&rarr;&ldquo;<span style="color:#b22222;">调整桌面颜色设置</span>&rdquo;&rarr;&ldquo;<span style="color:#b22222;">3. 应用以下增强功能</span>&rdquo;中，将&ldquo;数字振动控制&rdquo;的值设低一些，例如，我感到颜色过于鲜艳时，该值为70%多，而我调到了30%，才感到回到了正常状态。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（31）</span>tar打包目录时，排除掉一些指定的文件<br />
	如下命令：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
tar zcf a.tar.gz 2012* --exclude=*_*_*
</pre>
<p>	将会把名字符合 2012* 的目录打包成a.tar.gz，其中，只要任何目录下含有 pattern 为 *_*_* 的文件，都会被排除掉，不会被打进tar包中，例如，一个名为&ldquo;1_2_3&rdquo;的文件，就会被排除掉。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（32）</span>Visual Studio 2008中设置制表符(Tab)的缩进<br />
	这里仅针对VC项目来说。在&ldquo;工具&rdquo;&rarr;&ldquo;选项&rdquo;&rarr;&ldquo;C/C++&rdquo;&rarr;&ldquo;制表符&rdquo;中可以设置。如果想用空格代替制表符，选择&ldquo;插入空格&rdquo;即可。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（33）</span>命令行创建两个版本的SVN diff文件<br />
	命令：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
svn diff -r 3001:3000 &gt; 1.diff
</pre>
<p>	表示创建版本3001和3000之间的diff，并保存为1.diff文件。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（34）</span>在Linux下查看一个进程占用的端口<br />
	先用如下命令找到进程的PID：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -ef | grep process-name
</pre>
<p>	其中，process-name是你的进程名。找到PID（这里假设是26382）后，再用如下命令查看其占用的端口：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
netstat -anlp | grep 26382
</pre>
<p>
	<span style="background-color: rgb(0, 255, 0); ">（35）</span>修改Linux默认的SSH 22端口<br />
	SSH的默认22端口容易受到攻击，为了安全，可以改为其他端口，以下是修改方法：<br />
	<span style="background-color:#ffff00;">①</span>修改文件&nbsp;/etc/ssh/sshd_config，找到注释掉的这一行：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#Port 22
</pre>
<p>	改为：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
Port 22
Port 34567
</pre>
<p>	这表示同时使用22和34567作为SSH端口。<br />
	有人会说：为什么要设置两个端口？直接使用端口34567不就行了吗？因为如果你最后设置失败，22端口可能不再起作用，你就无法通过SSH连接服务器了，后果就是你只能通过远程控制卡或直接到服务器前连接显示器来重新配置正确，那样的话就悲剧了。所以，为了留个保险，先保留原来的22端口。<br />
	<span style="background-color:#ffff00;">②</span>重启两个服务：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
/etc/init.d/sshd restart
/etc/init.d/iptables restart
</pre>
<p>	<span style="background-color:#ffff00;">③</span>通过新端口34567来SSH连接服务器，成功的话，就把前面保留的默认22端口给删掉，再重启上述两个服务，就OK了。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	<span style="background-color: rgb(0, 255, 0); ">（36）</span>在GNOME终端中的Emacs里打开Emacs的菜单<br />
	默认情况下，按F10是可以打开Emacs的菜单的，不过GNOME的终端可能会截获F10这个快捷键，你按F10的时候会变成打开GNOME终端的菜单，这非常让人讨厌。你可以在Emacs中执行以下命令：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
M-X menu-bar-open
</pre>
<p>	这样就可以打开Emacs的菜单了。</p>
<p>	<span style="background-color: rgb(0, 255, 0);">（37）</span>Linux下用命令删除一个以文件名以减号(-)开头的文件<br />
	先cd到文件所在的目录，然后：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
rm -f ./-filename
</pre>
<p>
	<span style="background-color: rgb(0, 255, 0);">（38）</span>Linux下用命令行查看进程监听的端口</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
netstat -anp | grep 程序名
</pre>
<p>
	<span style="background-color: rgb(0, 255, 0);">（39）</span>CentOS的网络安装地址<br />
	这里以CentOS 6.4为例。先下载net install的ISO（例如CentOS-6.4-i386-netinstall.iso），刻录到光盘上，用该光盘引导安装，在选择操作系统所在的介质（例如光驱，硬盘，NFS，URL）的时候，选择URL，然后输入用下面的方法找到的URL，即可开始安装：<br />
	到<a href="http://www.centos.org/modules/tinycontent/index.php?id=30" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这里</span></a>去找一个mirror，例如在中国大陆可以选择网易的服务器，对安装32位版本的CentOS 6.4，就是：<a href="http://mirrors.163.com/centos/6.4/os/i386/" rel="noopener noreferrer" target="_blank">http://mirrors.163.com/centos/6.4/os/i386/</a><br />
	如果想看更具体的安装过程截图，可参考<a href="http://www.if-not-true-then-false.com/2011/centos-6-netinstall-network-installation/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">此文</span></a>。</p>
<p>	<span style="background-color: rgb(0, 255, 0); ">（40）</span>在Emacs中将文件转换为UTF-8编码<br />
	如何将一个在Emacs中打开的文本文件转换为UTF-8编码？下面的Emacs命令可以做到：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
C-x C-m f utf-8-unix
</pre>
<p>	然后回车即可。当然最后别忘了保存文件！</p>
<p>	<span style="background-color: rgb(0, 255, 0);">（41）</span>用密钥文件登录的两台Linux如何scp文件<br />
	假设要从server A向server B上传文件（scp），那么只需要把私钥文件放到server A，你的用户根目录下的.ssh目录下，即 ~/.ssh/，然后把文件的权限改为600，再把它的用户改为你的用户，就可以scp文件到server B了（假设私钥文件名为identity）：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
chmod 600 identity
chown your_user:your_user identity
</pre>
<p>	这样做虽然可以达到目的，不过显然不安全，因为只要有权限在server A上访问此私钥文件的人，都可以用此私钥文件在其他地方用你的身份登录server A和server B了。<br />
	安全的做法是：在ssh客户端软件中设置允许forward agent，从而可以在你自己的计算机上ssh到server A，再scp文件，而不用直接修改server A。假设你使用Ubuntu的ssh客户端，那么修改&nbsp;/etc/ssh/ssh_config 文件，将这一行：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#   ForwardAgent no
</pre>
<p>	改为：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
   ForwardAgent yes
</pre>
<p>	即可。</p>
<p>	<span style="background-color: rgb(0, 255, 0);">（42）</span>Linux shell命令生成一个随机数</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
echo -n $RANDOM | md5sum | awk &#39;{print $1}&#39;
</pre>
</div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);">（43）</span>用shell命令删除文件中的指定行<br />
如果你要删除一个文本文件中，含有你指定的字符串的那些行，可以用sed像下面这样操作：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
sed -i &#39;/abc/d&#39; 1.txt
</pre>
<p>这样就会把1.txt中，含有&ldquo;abc&rdquo;字符串的行都删掉。</p>
<p><span style="background-color: rgb(0, 255, 0);">（44）</span>Linux shell获取文件大小<br />
还在用&nbsp;<span style="color:#0000ff;">ls -l your-file-name | awk &#39;{print $5}&#39;</span> 的方式来获取文件大小？太麻烦啦。下面方式不是更好一些么：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
stat -c%s your-file-name
</pre>
<p>直接得到文件大小。</p>
<p><span style="background-color: rgb(0, 255, 0);">（45）</span>Linux shell获取0~9之间的随机数（方法来自《鸟哥的Linux私房菜》一书）</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
declare -i NUM=$RANDOM*10/32768
echo $NUM
</pre>
<p>
<span style="background-color: rgb(0, 255, 0);">（46）</span>正则表达式排除指定的字符串<br />
标题的含义是：你想写一个正则表达式，它要匹配一个字符串，但是这个字符串中不能含有你指定的另一个字符串（也就是把它排除掉），可以这样写：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
^(?!.*string).*$
</pre>
<p>其中，string就是你要排除掉的字符串。<br />
在上面的正则表达式的基础上改一改，就可以为你所用。举个例子，假设有字符串：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
http://news.qq.com/1.html
http://video.qq.com/2.html
http://sports.qq.com/3.html
</pre>
<p>现在你想匹配所有&ldquo;http://*.qq.com*&rdquo;模式的字符串，但是，又想去掉&ldquo;news.qq.com&rdquo;这样的，那么可以这样写：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
^(?!.*news).*\.qq\.com.*$
</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
结果就是：第一行不会匹配上，后两行会匹配上。<br />
再延伸一点：如果不仅想排除掉news，还想排除掉video，那么可以这样写：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
^http:\/\/(?!news|video).*\.qq\.com.*$
</pre>
<p>再延伸一点：<br />
如果不仅想排除掉news和video，而且还想指定xxx.qq.com中的&ldquo;xxx&rdquo;必须是<span style="color:#0000ff;">0～9</span>，<span style="color:#0000ff;">a～z</span>，或<span style="color:#0000ff;">-</span>（减号）中的任何字符，那么可以在上面的基础上再加一点匹配条件：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
^http:\/\/(?!news|video)[a-z0-9\-]+\.qq\.com.*$
</pre>
<p>此正则是不会匹配上URL&ldquo;http://a_b.qq.com/1.html&rdquo;的，因为&ldquo;a_b&rdquo;中含有下划线，这不在上面规定的字符范围内。</p>
<p><span style="background-color: rgb(0, 255, 0);">（47）</span><span style="font-family: Ubuntu;">在Emacs中直接查看当前文件的svn diff<br />
我们知道，在Linux命令行下可以用 <span style="color:#0000ff;">svn diff file.txt</span> 命令来查看file.txt的当前文件与版本库中的最新版本的差异，但是这样输出的文本是没有语法高亮的（当内容很多时，看起来眼都花了），我们只能把它重定向到一个diff文件中，然后再用Emacs或VIM之类的软件打开diff文件，这个操作过程非常麻烦。那么，如果已经用Emacs打开了file.txt，能否在Emacs中直接显示出它的SVN diff，从而可以用高亮方式来查看呢？强大的Emacs必然有这个功能。在Emacs 24中，不需要任何插件就可以实现，在打开file.txt的情况下，输入命令：</span></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
C-x v =
</pre>
<p><span style="font-family: Ubuntu;">并回车，屏幕会自动一分为二，左边是file.txt的buffer，右边是它的svn diff结果，看起来太舒服了。如果跟版本库中的文件相比，file.txt没有任何修改的话，则在Emacs的mini buffer那里会显示出一句话：</span></p>
<blockquote>
<p>
		No changes between working revision and workfile</p>
</blockquote>
<p>
<span style="background-color: rgb(0, 255, 0);">（48）</span><span style="font-family: Ubuntu;">Ubuntu下Chrome字体太小的解决办法<br />
在Ubuntu下，Chrome默认设置时，在我的PC上，某些网页字体在100％显示时非常小，看起来眼都要花了，但是如果单纯地靠放大网页来放大字体，有时候又会造成页面结构的错乱，因此，把Chrome的最小字体改大一点是个好方法，如下：<br />
退出Chrome，备份Chrome配置文件：</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
cp ~/.config/google-chrome/Default/Preferences ~/.config/google-chrome/Default/Preferences.bak
</pre>
<p><span style="font-family: Ubuntu;">修改其内容，找到&ldquo;</span>webkit<span style="font-family: Ubuntu;">&rdquo;这一段，在类似于</span></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
&quot;default_font_size&quot;: 16,
</pre>
<p><span style="font-family: Ubuntu;">的这一行下面，添加一行：</span></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
&quot;minimum_font_size&quot;: 15,
</pre>
<p><span style="font-family: Ubuntu;">重新打开Chrome，发现字体很小的网页在100%显示时字体变大了。</span></p>
<p><span style="background-color: rgb(0, 255, 0);">（49）</span><span style="font-family: Ubuntu;">crontab中不支持嵌套命令<br />
假设你在crontab中写了如下的命令：</span></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
31 14 * * * /home/codelast/a.sh `date -d yesterday +%Y-%m-%d` &gt;&gt; /home/codelast/a.log 2&gt;&amp;1
</pre>
<p><span style="font-family: Ubuntu;">该命令的本意是向a.sh脚本传入一个类似于&ldquo;2013-06-01&rdquo;这样的字符串参数，但实际上该crontab不会执行成功，因为crontab中不支持嵌套命令（反引号中的命令），因此，如果你要达到这样的效果，可以把嵌套命令写在一个脚本中，只在crontab中调用那个脚本即可。</span></p>
<p><span style="background-color: rgb(0, 255, 0);">（50）</span><span style="font-family: Ubuntu;">Linux命令行下进行Base64解码</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
echo xxx | base64 --decode
</pre>
<p><span style="font-family: Ubuntu;">其中，xxx是你要解码的字符串。</span></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushPlain.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%b8%80%e4%ba%9b%e6%9c%aa%e5%bd%92%e7%b1%bb%e7%9a%84%e5%91%bd%e4%bb%a4%e3%80%81%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95%e6%88%96%e9%97%ae%e9%a2%98%e6%80%bb%e7%bb%93%ef%bc%884/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
