<?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 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Wed, 17 Mar 2021 07:02:31 +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>[原创]后台运行的shell脚本对文件锁的影响</title>
		<link>https://www.codelast.com/%e5%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%af%b9%e6%96%87%e4%bb%b6%e9%94%81%e7%9a%84%e5%bd%b1%e5%93%8d/</link>
					<comments>https://www.codelast.com/%e5%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%af%b9%e6%96%87%e4%bb%b6%e9%94%81%e7%9a%84%e5%bd%b1%e5%93%8d/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 24 Jan 2011 08:53:26 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[flock]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[后台]]></category>
		<category><![CDATA[影响]]></category>
		<category><![CDATA[文件锁]]></category>
		<category><![CDATA[脚本]]></category>
		<category><![CDATA[运行]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=1101</guid>

					<description><![CDATA[<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">假设shell脚本A中的一段代码设置有文件锁，在此文件锁的后面（外部）以后台运行的方式调用了脚本B（此脚本中也有文件锁，但是与脚本A的锁文件不是同一个文件），并且脚本B执行时间相当之长，那么，在后台运行的脚本B在结束之后，是否会导致脚本A无法启动第二个进程呢？</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">下面就来做一个简单的试验，以确定上面的问题。</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本A（<span style="color:#00f;">parent.sh</span>）内容如下：</span></span></p>
<p></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/sh
SHELL_DIR=&#34;/root/&#34;
LOCKFILE_1=$SHELL_DIR&#34;.lock1&#34;
{
    echo &#34;Go into lock1.&#34;
    if ! flock -n 4; then
        echo &#34;*** Shell is running, quit.&#34;
        exit 1
    fi
} 4&#60;&#62;$LOCKFILE_1

$SHELL_DIR&#34;child.sh&#34; &#38;      # run shell B on background 
</pre>
<p>
<span id="more-1101"></span></p>
<p>
	<span style="font-size:14px;"><strong style="font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(46, 46, 46); border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(0, 0, 255); "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(255, 255, 255); font-weight: normal; ">文章来源：http://www.codelast.com/</span></span></span></strong></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本B</span>（<span style="color: rgb(0, 0, 255); ">child.sh</span></span>&#8230; <a href="https://www.codelast.com/%e5%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%af%b9%e6%96%87%e4%bb%b6%e9%94%81%e7%9a%84%e5%bd%b1%e5%93%8d/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">假设shell脚本A中的一段代码设置有文件锁，在此文件锁的后面（外部）以后台运行的方式调用了脚本B（此脚本中也有文件锁，但是与脚本A的锁文件不是同一个文件），并且脚本B执行时间相当之长，那么，在后台运行的脚本B在结束之后，是否会导致脚本A无法启动第二个进程呢？</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">下面就来做一个简单的试验，以确定上面的问题。</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本A（<span style="color:#00f;">parent.sh</span>）内容如下：</span></span></p>
<p></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/sh
SHELL_DIR=&quot;/root/&quot;
LOCKFILE_1=$SHELL_DIR&quot;.lock1&quot;
{
    echo &quot;Go into lock1.&quot;
    if ! flock -n 4; then
        echo &quot;*** Shell is running, quit.&quot;
        exit 1
    fi
} 4&lt;&gt;$LOCKFILE_1

$SHELL_DIR&quot;child.sh&quot; &amp;      # run shell B on background 
</pre>
<p>
<span id="more-1101"></span></p>
<p>
	<span style="font-size:14px;"><strong style="font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(46, 46, 46); border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(0, 0, 255); "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(255, 255, 255); font-weight: normal; ">文章来源：http://www.codelast.com/</span></span></span></strong></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本B</span>（<span style="color: rgb(0, 0, 255); ">child.sh</span>）内容如下：</span></p>
<p></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/sh
SHELL_DIR=&quot;/root/&quot;
LOCKFILE_2=$SHELL_DIR&quot;.lock2&quot;
{
    echo &quot;Go into lock2.&quot;
    sleep 1000
    echo &quot;Sleep done.&quot;
} 5&lt;&gt;$LOCKFILE_2
</pre>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><font class="Apple-style-span">然后运行脚本A：</font></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="color:#00f;"><span style="font-size:14px;">./parent.sh</span></span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本B运行中，会暂停（sleep）1000秒，因此它会卡在那里，但是它是以后台方式运行的，并不会影响脚本A的终止。就算脚本B没有结束，你也可以再启动任意多个脚本A，并且你不会看到&ldquo;Shell is running, quit&rdquo;的提示，此外，你还可以查看到进程中有N个child.sh在运行（<span style="color:#00f;">ps -ef | grep child.sh</span>）。这说明在脚本B使用另一个文件锁、并且以后台方式被脚本A调用的情况下，不会对脚本A的文件锁造成影响。</span><br />
	</span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;"><span style="color:#f00;">但是</span>，如果你把脚本A中的&ldquo;$SHELL_DIR&quot;child.sh&quot; &amp;&rdquo;这句调用脚本B的语句放到锁的内部（即两个大括号中间）呢？答案是与上面<span style="color:#f00;">相反</span>的，即：脚本B没有结束之前，脚本A会被锁住，你将会看到打印出&ldquo;Shell is running, quit&rdquo;的提示。</span></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>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(46, 46, 46); border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(0, 0, 255); "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(255, 255, 255); font-weight: normal; line-height: 20px; ">文章来源：http://www.codelast.com/</span></span></span></strong></span></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/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%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%af%b9%e6%96%87%e4%bb%b6%e9%94%81%e7%9a%84%e5%bd%b1%e5%93%8d/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>shell脚本因文件编码问题不能正确执行的问题</title>
		<link>https://www.codelast.com/shell%e8%84%9a%e6%9c%ac%e5%9b%a0%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e4%b8%8d%e8%83%bd%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.codelast.com/shell%e8%84%9a%e6%9c%ac%e5%9b%a0%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e4%b8%8d%e8%83%bd%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 14 Dec 2010 12:26:44 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[ANSI]]></category>
		<category><![CDATA[BOM]]></category>
		<category><![CDATA[cannot execute binary file]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[ps]]></category>
		<category><![CDATA[SecureCRT]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[UltraEdit]]></category>
		<category><![CDATA[UTF-8]]></category>
		<category><![CDATA[UTF8]]></category>
		<category><![CDATA[vi]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[去除]]></category>
		<category><![CDATA[文件]]></category>
		<category><![CDATA[汉字]]></category>
		<category><![CDATA[编码]]></category>
		<category><![CDATA[错误]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=912</guid>

					<description><![CDATA[<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">遇到一例因shell文件的编码问题导致执行后不能得到正确结果的问题。一般来说脚本中不会带有中文，不过有时候例外，例如你要查找一个文本中的字符串，cat test.txt &#124; grep &#34;汽车&#34; &#160;这样的命令，如果文件编码不对，就可能得不到正确结果。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span id="more-912"></span></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">我的问题是：有一个脚本，无论怎么执行结果都不对，但是我把它里面的关键的几句话单独拿出来，在SecureCRT终端下依次执行，最后得到的结果却正确，奇了。于是我终于想起来可能有中文的影响，在终端里查看shell脚本的内容，发现不能正确显示出一处中文（乱码了），可见正是这处乱码，导致了grep输出的结果不正确。但是在SecureCRT终端下，无论是用vi还是emacs，我都无法向文件中输入中文（上屏的中文全都乱码），于是怀疑是文件编码问题。</span></span></p>
<p>
	<span style="font-size: 14px; color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" style="font-size: 14px; " target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">然后我就将脚本下载到Windows下，用UltraEdit将其另存为UTF-8格式，再上传到Linux中，再执行脚本，竟然报错：<strong><span style="color:#f00;">cannot execute binary file</span></strong></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">这种问题的原因之一是脚本中有不可识别字符。于是我再用UltraEdit，将文件重新存为&#8220;<strong><span style="color:#00f;">UTF-8 无 BOM</span></strong>&#8221;格式（如下图），再上传到Linux中，就可以正确执行了。</span></span></p>
<p style="text-align: center; ">
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><img decoding="async" alt="另存为UTF-8 无BOM格式" src="http://www.codelast.com/wp-content/uploads/2011/11/utf-8_no_bom_file_format.jpg" style="width: 353px; height: 103px; " /><br />
	</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">网上还有很多文章，介绍了如何用emacs，vi 或 vim更改文件编码的方法，应该也是可以的，但用UltraEdit最方便。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">另外再记一个小技巧：用 ps -ef &#124; grep XXX 命令查看系统中是否存在某个进程的时候，输出结果中会包含有grep命令这一行，例如：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -ef &#124; grep httpd
</pre>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">部分输出为：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
root     27154 27129  0 20:35 pts/3    00:00:00 grep httpd
daemon   28834  2955  3 13:35 ?</pre>&#8230; <a href="https://www.codelast.com/shell%e8%84%9a%e6%9c%ac%e5%9b%a0%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e4%b8%8d%e8%83%bd%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">遇到一例因shell文件的编码问题导致执行后不能得到正确结果的问题。一般来说脚本中不会带有中文，不过有时候例外，例如你要查找一个文本中的字符串，cat test.txt | grep &quot;汽车&quot; &nbsp;这样的命令，如果文件编码不对，就可能得不到正确结果。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span id="more-912"></span></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">我的问题是：有一个脚本，无论怎么执行结果都不对，但是我把它里面的关键的几句话单独拿出来，在SecureCRT终端下依次执行，最后得到的结果却正确，奇了。于是我终于想起来可能有中文的影响，在终端里查看shell脚本的内容，发现不能正确显示出一处中文（乱码了），可见正是这处乱码，导致了grep输出的结果不正确。但是在SecureCRT终端下，无论是用vi还是emacs，我都无法向文件中输入中文（上屏的中文全都乱码），于是怀疑是文件编码问题。</span></span></p>
<p>
	<span style="font-size: 14px; color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" style="font-size: 14px; " target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">然后我就将脚本下载到Windows下，用UltraEdit将其另存为UTF-8格式，再上传到Linux中，再执行脚本，竟然报错：<strong><span style="color:#f00;">cannot execute binary file</span></strong></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">这种问题的原因之一是脚本中有不可识别字符。于是我再用UltraEdit，将文件重新存为&ldquo;<strong><span style="color:#00f;">UTF-8 无 BOM</span></strong>&rdquo;格式（如下图），再上传到Linux中，就可以正确执行了。</span></span></p>
<p style="text-align: center; ">
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><img decoding="async" alt="另存为UTF-8 无BOM格式" src="http://www.codelast.com/wp-content/uploads/2011/11/utf-8_no_bom_file_format.jpg" style="width: 353px; height: 103px; " /><br />
	</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">网上还有很多文章，介绍了如何用emacs，vi 或 vim更改文件编码的方法，应该也是可以的，但用UltraEdit最方便。</span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></span></span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">另外再记一个小技巧：用 ps -ef | grep XXX 命令查看系统中是否存在某个进程的时候，输出结果中会包含有grep命令这一行，例如：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -ef | grep httpd
</pre>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;">部分输出为：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
root     27154 27129  0 20:35 pts/3    00:00:00 grep httpd
daemon   28834  2955  3 13:35 ?        00:16:23 /usr/local/apache2/bin/httpd -k start
</pre>
<p>
	<span style="font-size: 14px; ">可见第一行是干扰，我们不想看到它，那么可以有两种方法来过滤掉这一行：</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -ef | grep httpd | grep -v grep
</pre>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><font class="Apple-style-span">或：</font></span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ps -Ao pid,comm,args | grep httpd
</pre>
<p>
	<span style="font-size:14px;"><span style="font-family:微软雅黑;"><font class="Apple-style-span">这种方法输出的内容中只包含进程号、进程名、调用参数，具体使用可man ps。</font></span></span></p>
<p>
	<br />
	<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/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/shell%e8%84%9a%e6%9c%ac%e5%9b%a0%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e4%b8%8d%e8%83%bd%e6%ad%a3%e7%a1%ae%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>用shell删除一个文件的前N行</title>
		<link>https://www.codelast.com/%e7%94%a8shell%e5%88%a0%e9%99%a4%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9a%84%e5%89%8dn%e8%a1%8c/</link>
					<comments>https://www.codelast.com/%e7%94%a8shell%e5%88%a0%e9%99%a4%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9a%84%e5%89%8dn%e8%a1%8c/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 25 Oct 2010 05:32:33 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[删除]]></category>
		<category><![CDATA[前N行]]></category>
		<category><![CDATA[前几行]]></category>
		<category><![CDATA[文件]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=780</guid>

					<description><![CDATA[<p>
	<span style="font-size:14px;">假设你想删除一个文本文件的前N行，并且你不想先将后面要保留的内容先保存到其他临时文件中，那么可以在shell中这样写（此处假设要删除的是 /root/file.txt 的前10行）：</span></p>
<p>
<span id="more-780"></span></p>
<p>
	<span style="color:#00f;"><span style="font-size:14px;">ex /root/file.txt &#60;&#60; EOF</span></span></p>
<p>
	<span style="color:#00f;"><span style="font-size:14px;">1,10d</span></span></p>
<p>
	<span style="color:#00f;"><span style="font-size:14px;">wq</span></span></p>
<p>
	<span style="color:#00f;"><span style="font-size:14px;">EOF</span></span></p>
<p>
	&#160;</p>
<p>
	<span style="font-size:14px;">注意第二行的写法：<span style="color:#00f;">1,10d</span> 表示删除第1到第10行。同理，你要删除第3到第7行，可以写成 3,7d ，依此类推。</span></p>
<p>
	&#160;</p>
<p>
	<span style="font-size:14px;">这种方法倒是有一个弊端：当<span style="color:#f00;">你的文件行数&#160;</span>&#60;&#160;<span style="color:#f00;">要删除的行数</span>时（此处所说的&#8220;你的文件行数&#8221;不包括文件的最后一个空行，即：如果你的文件最后一行是一个空行，那么么此处所说的&#8220;你的文件行数&#8221;就不包括它），那么用这种方法就会一行都删不掉（而不是我们想像中的&#8220;会删除文件中的所有行&#8221;）。</span></p>
<p>
	<span style="font-size:14px;">例如，你的文件内容为（5行+一个空行）：</span></p>
<p>
	<span style="color:#00f;">11</span></p>
<p>
	<span style="color:#00f;">22</span></p>
<p>
	<span style="color:#00f;">33</span></p>
<p>
	<span style="color:#00f;">44</span></p>
<p>
	<span style="color:#00f;">55</span></p>
<p>
	<span style="color:#00f;">（此处为一个空行，即：只有一个换行符 \n 的行）</span></p>
<p>
	&#160;</p>
<p>
	<span style="font-size:14px;">那么使用 1,6d 就会删除不掉任何内容，只有使用 1,5d 才能删除所有内容。</span></p>
<p>
	<br />
	<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>&#160;版权声明&#160;<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;<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>&#8230; <a href="https://www.codelast.com/%e7%94%a8shell%e5%88%a0%e9%99%a4%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9a%84%e5%89%8dn%e8%a1%8c/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
	<span style="font-size:14px;">假设你想删除一个文本文件的前N行，并且你不想先将后面要保留的内容先保存到其他临时文件中，那么可以在shell中这样写（此处假设要删除的是 /root/file.txt 的前10行）：</span></p>
<p>
<span id="more-780"></span></p>
<p>
	<span style="color:#00f;"><span style="font-size:14px;">ex /root/file.txt &lt;&lt; EOF</span></span></p>
<p>
	<span style="color:#00f;"><span style="font-size:14px;">1,10d</span></span></p>
<p>
	<span style="color:#00f;"><span style="font-size:14px;">wq</span></span></p>
<p>
	<span style="color:#00f;"><span style="font-size:14px;">EOF</span></span></p>
<p>
	&nbsp;</p>
<p>
	<span style="font-size:14px;">注意第二行的写法：<span style="color:#00f;">1,10d</span> 表示删除第1到第10行。同理，你要删除第3到第7行，可以写成 3,7d ，依此类推。</span></p>
<p>
	&nbsp;</p>
<p>
	<span style="font-size:14px;">这种方法倒是有一个弊端：当<span style="color:#f00;">你的文件行数&nbsp;</span>&lt;&nbsp;<span style="color:#f00;">要删除的行数</span>时（此处所说的&ldquo;你的文件行数&rdquo;不包括文件的最后一个空行，即：如果你的文件最后一行是一个空行，那么么此处所说的&ldquo;你的文件行数&rdquo;就不包括它），那么用这种方法就会一行都删不掉（而不是我们想像中的&ldquo;会删除文件中的所有行&rdquo;）。</span></p>
<p>
	<span style="font-size:14px;">例如，你的文件内容为（5行+一个空行）：</span></p>
<p>
	<span style="color:#00f;">11</span></p>
<p>
	<span style="color:#00f;">22</span></p>
<p>
	<span style="color:#00f;">33</span></p>
<p>
	<span style="color:#00f;">44</span></p>
<p>
	<span style="color:#00f;">55</span></p>
<p>
	<span style="color:#00f;">（此处为一个空行，即：只有一个换行符 \n 的行）</span></p>
<p>
	&nbsp;</p>
<p>
	<span style="font-size:14px;">那么使用 1,6d 就会删除不掉任何内容，只有使用 1,5d 才能删除所有内容。</span></p>
<p>
	<br />
	<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>
<p>
	<span style="color:#fff;">NULL</span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e7%94%a8shell%e5%88%a0%e9%99%a4%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9a%84%e5%89%8dn%e8%a1%8c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>用shell将时间字符串与时间戳互转</title>
		<link>https://www.codelast.com/%e7%94%a8shell%e5%b0%86%e6%97%b6%e9%97%b4%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%97%b6%e9%97%b4%e6%88%b3/</link>
					<comments>https://www.codelast.com/%e7%94%a8shell%e5%b0%86%e6%97%b6%e9%97%b4%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%97%b6%e9%97%b4%e6%88%b3/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 18 Oct 2010 06:11:56 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[互转]]></category>
		<category><![CDATA[字符串]]></category>
		<category><![CDATA[时间戳]]></category>
		<category><![CDATA[转换]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=775</guid>

					<description><![CDATA[<div>
	在shell中，字符串转换为时间戳可以这样做：</div>
<blockquote>
<div>
		date -d &#34;2010-10-18 00:00:00&#34; +%s</div>
</blockquote>
<div>
	不需要指定时、分、秒的话，直接写日期就可以了：</div>
<blockquote>
<div>
		date -d &#34;2010-10-18&#34; +%s</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		1287331200</div>
</blockquote>
<div>
	&#160;</div>
<div>
	其中，-d参数表示显示指定的字符串所表示的时间，+%s表示输出时间戳。</div>
<div>
<span id="more-775"></span></div>
<div>
	而时间戳转换为字符串可以这样做：</div>
<blockquote>
<div>
		date -d @1287331200</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		Mon Oct 18 00:00:00 CST 2010</div>
</blockquote>
<div>
	<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></div>
<div>
	如果要转换成&#8220;2010-10-18 00:00:00&#8221;这种形式的日期时间字符串，则这样做：</div>
<blockquote>
<div>
		date -d &#34;1970-01-01 UTC 1287331200 seconds&#34; &#34;+%F %T&#34;</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		2010-10-18 00:00:10</div>
</blockquote>
<div>
	&#160;</div>
<div>
	再多说一句，如果只要输出日期，不要时间，则去掉 %T 就可以了：</div>
<blockquote>
<div>
		date -d &#34;1970-01-01 UTC 1287331200 seconds&#34; +%F</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		2010-10-18</div>
</blockquote>
<div>
	<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></div>&#8230; <a href="https://www.codelast.com/%e7%94%a8shell%e5%b0%86%e6%97%b6%e9%97%b4%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%97%b6%e9%97%b4%e6%88%b3/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<div>
	在shell中，字符串转换为时间戳可以这样做：</div>
<blockquote>
<div>
		date -d &quot;2010-10-18 00:00:00&quot; +%s</div>
</blockquote>
<div>
	不需要指定时、分、秒的话，直接写日期就可以了：</div>
<blockquote>
<div>
		date -d &quot;2010-10-18&quot; +%s</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		1287331200</div>
</blockquote>
<div>
	&nbsp;</div>
<div>
	其中，-d参数表示显示指定的字符串所表示的时间，+%s表示输出时间戳。</div>
<div>
<span id="more-775"></span></div>
<div>
	而时间戳转换为字符串可以这样做：</div>
<blockquote>
<div>
		date -d @1287331200</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		Mon Oct 18 00:00:00 CST 2010</div>
</blockquote>
<div>
	<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></div>
<div>
	如果要转换成&ldquo;2010-10-18 00:00:00&rdquo;这种形式的日期时间字符串，则这样做：</div>
<blockquote>
<div>
		date -d &quot;1970-01-01 UTC 1287331200 seconds&quot; &quot;+%F %T&quot;</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		2010-10-18 00:00:10</div>
</blockquote>
<div>
	&nbsp;</div>
<div>
	再多说一句，如果只要输出日期，不要时间，则去掉 %T 就可以了：</div>
<blockquote>
<div>
		date -d &quot;1970-01-01 UTC 1287331200 seconds&quot; +%F</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		2010-10-18</div>
</blockquote>
<div>
	<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></div>
<div>
	顺便再记一下取昨天日期的方法：</div>
<blockquote>
<div>
		date -d &quot;yesterday&quot; +&quot;%Y-%m-%d&quot;</div>
</blockquote>
<div>
	输出形如：</div>
<blockquote>
<div>
		2010-08-22</div>
</blockquote>
<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>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e7%94%a8shell%e5%b0%86%e6%97%b6%e9%97%b4%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%97%b6%e9%97%b4%e6%88%b3/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 用shell判断一个字符串是不是全为数字 &#038; 截取字符串的若干示例</title>
		<link>https://www.codelast.com/%e7%94%a8shell%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%98%af%e4%b8%8d%e6%98%af%e5%85%a8%e4%b8%ba%e6%95%b0%e5%ad%97/</link>
					<comments>https://www.codelast.com/%e7%94%a8shell%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%98%af%e4%b8%8d%e6%98%af%e5%85%a8%e4%b8%ba%e6%95%b0%e5%ad%97/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 14 Sep 2010 03:48:17 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[判断]]></category>
		<category><![CDATA[取字符串的第N个字符]]></category>
		<category><![CDATA[字符]]></category>
		<category><![CDATA[字符串]]></category>
		<category><![CDATA[截取]]></category>
		<category><![CDATA[截取字符串]]></category>
		<category><![CDATA[数字]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=211</guid>

					<description><![CDATA[<p>
	<span style="color:#000;"><span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">文章来源：<a href="http://www.codelast.com/" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: rgb(0, 77, 153); " target="_blank">http://www.codelast.com/</a></span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="color:#000;"><span style="background-color:#00ff00;">（1）</span>判断一个字符串是不是全为数字</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/sh

# 该变量就是要判断的变量
VAR=&#34;1234908&#34;

RET=`expr match $VAR &#34;[0-9]*$&#34;`
if [ ${RET} -gt 0 ]; then
	echo &#34;It is a Number!&#34;
else
	echo &#34;Not a Number!&#34;
fi

</pre>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
	<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">另外要注意：<span style="color:#f00;">用这种方式来判断负数的时候，也认为不是全为数字的，因为负数中带了一个负号，就不是数字</span>。</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
<span id="more-211"></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="color:#000;"><span style="background-color:#00ff00;">（2）</span>取字符串的第1个字符</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
VAR=&#34;[Sun Feb 20 03:10:51 2011]&#34;
echo ${VAR:0:1}
</pre>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这样输出的就是第一个字符&#8220;[&#8221;了。VAR:0:1表示从变量VAR中的字符串的第0个字符开始（也就是从头开始），截取到第1个字符为止，所以就相当于取字符串的第一个字符了。<br />
	同理，如果要取某个字符中的第6个字符到第10个字符的子串，可以用类似于 VAR:6:10 这样的写法。<br />
	详细用法请看<a href="http://www.linuxidc.com/Linux/2010-12/30625.htm" target="_blank">这个链接</a></span>。</span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e7%94%a8shell%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%98%af%e4%b8%8d%e6%98%af%e5%85%a8%e4%b8%ba%e6%95%b0%e5%ad%97/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
	<span style="color:#000;"><span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">文章来源：<a href="http://www.codelast.com/" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: rgb(0, 77, 153); " target="_blank">http://www.codelast.com/</a></span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="color:#000;"><span style="background-color:#00ff00;">（1）</span>判断一个字符串是不是全为数字</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/sh

# 该变量就是要判断的变量
VAR=&quot;1234908&quot;

RET=`expr match $VAR &quot;[0-9]*$&quot;`
if [ ${RET} -gt 0 ]; then
	echo &quot;It is a Number!&quot;
else
	echo &quot;Not a Number!&quot;
fi

</pre>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
	<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">另外要注意：<span style="color:#f00;">用这种方式来判断负数的时候，也认为不是全为数字的，因为负数中带了一个负号，就不是数字</span>。</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
<span id="more-211"></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="color:#000;"><span style="background-color:#00ff00;">（2）</span>取字符串的第1个字符</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
VAR=&quot;[Sun Feb 20 03:10:51 2011]&quot;
echo ${VAR:0:1}
</pre>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这样输出的就是第一个字符&ldquo;[&rdquo;了。VAR:0:1表示从变量VAR中的字符串的第0个字符开始（也就是从头开始），截取到第1个字符为止，所以就相当于取字符串的第一个字符了。<br />
	同理，如果要取某个字符中的第6个字符到第10个字符的子串，可以用类似于 VAR:6:10 这样的写法。<br />
	详细用法请看<a href="http://www.linuxidc.com/Linux/2010-12/30625.htm" target="_blank">这个链接</a></span>。</span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); font-family: Simsun; ">
	<span style="font-family:微软雅黑;"><span style="color: rgb(0, 0, 0); "><span style="background-color:#00ff00;">（3）</span>从一个字符串的尾部开始，删除从指定字符开始直到最后一个字符</span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); font-family: Simsun; ">
	<span style="font-family:微软雅黑;"><span style="color: rgb(0, 0, 0); ">如下命令：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
STR=`ethtool eth0 | grep Speed | awk &#39;{print $2}&#39;`
</pre>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); font-family: Simsun; ">
	<span style="font-family:微软雅黑;"><span style="color: rgb(0, 0, 0); ">执行之后，变量 STR 中保存的是字符串&ldquo;<span style="color:#00f;">1000Mb/s</span>&rdquo;（不含引号），现在我要删除&ldquo;<span style="color:#b22222;">Mb/s</span>&rdquo;，只剩下&ldquo;<span style="color:#00f;">1000</span>&rdquo;，可以这样做：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
echo ${STR%M*}
</pre>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<span style="font-family: 微软雅黑;"><span style="color: rgb(0, 0, 0); ">这表示从右边开始，删除第一个 M 及右边的字符。</p>
<p>	同理，如果要删除一个字符串中的最右边的一个子字符串（例如，要删除&ldquo;file.checksum&rdquo;中的&ldquo;.checksum&rdquo;，可以这样做：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
A=file.checksum
B=`echo ${A%.checksum}`
echo $B
</pre>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	输出即为&ldquo;file&rdquo;（不含引号）。</p>
<p>	<span style="color:#000000;"><span style="font-family: 微软雅黑; background-color: rgb(0, 255, 0);">（4）</span></span><span style="font-family: 微软雅黑;"><span style="color:#000000;">截取字段串，可以考虑用删除掉多余字符串的方法来实现，例如：<br />
	截取字符串</span><span style="color:#0000ff;"> [192.168.3.11] </span><span style="color:#000000;">中的方括号里的IP地址， 用将方括号删除的方法，即可实现同样的效果：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
A=&quot;[192.168.3.11]&quot;
B=`echo $A | sed &#39;s/\ [//&#39; | sed &#39;s/]//&#39;`
echo $B
</pre>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<span style="font-family: 微软雅黑; color: rgb(0, 0, 0);">输出即为：192.168.3.11<br />
	注意：上面的第2行代码中，\ 和 [ 之间特意多加了一个空格（其实不应该有空格的），是因为本站安装的<a href="http://www.codelast.com/?p=6199" target="_blank">LaTex插件</a>的影响。</span></p>
<p><span style="font-size:14px;"><span style="font-family:微软雅黑;"><span style="color:#000000;">其中，两个sed命令分别将左括号[及右括号]替换为了空字符串，相当于删除了它们。左方括号在sed中需要转义，所以要以 \ 开头。</span></span></span><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="font-size:14px;"><span style="font-family:微软雅黑;"><span style="color:#000000;"> <span style="background-color:#00ff00;">（5）</span>截取字符串&mdash;&mdash;删除字符串末尾的子字符串</span></span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
[root@ codelast]$ INPUT_STR=&quot;abc.txt&quot;
[root@ codelast]$ echo ${INPUT_STR%.txt}
abc
</pre>
<p>其中，一个百分号表示从 INPUT_STR 的右边开始，去掉最短匹配子串。</p>
<p><span style="font-family: 微软雅黑; font-size: 14px;"><span style="background-color:#00ff00;">（6）</span>截取字符串&mdash;&mdash;从字符串的开头开始，删除最短匹配字符串<br />
假设有字符串&ldquo;part-r-00000&rdquo;，要从左边开始，删除最短的、匹配&ldquo;part&rdquo;的字符串，可以这样：</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
[codelast@ ~]$ A=&quot;part-r-00000&quot;
[codelast@ ~]$ B=${A#part}
[codelast@ ~]$ echo $B
-r-00000
</pre>
<p>
<span style="font-family: 微软雅黑; font-size: 14px; background-color: rgb(0, 255, 0);">（7）</span><span style="font-family: 微软雅黑; font-size: 14px;">截取字符串&mdash;&mdash;从字符串的右边第几个字符开始，一直截取到字符串的末尾<br />
假设要截取一个字符串最右边的3个字符，可以像下面这样：</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
[codelast@alarmpi ~]$ A=&quot;testStr&quot;
[codelast@alarmpi ~]$ B=`echo ${A:0-3}`
[codelast@alarmpi ~]$ echo $B
Str
</pre>
<p><span style="font-family: 微软雅黑; font-size: 14px;">在这里，<span style="color:#0000ff;">0-3</span>表示从右边第3个字符开始（<span style="color:#0000ff;">0-</span>表示从右边开始算），一直截取到字符串的末尾。</span></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/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/%e7%94%a8shell%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%98%af%e4%b8%8d%e6%98%af%e5%85%a8%e4%b8%ba%e6%95%b0%e5%ad%97/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>SHELL下的数字比较及计算</title>
		<link>https://www.codelast.com/shell%e4%b8%8b%e7%9a%84%e6%95%b0%e5%ad%97%e6%af%94%e8%be%83%e5%8f%8a%e8%ae%a1%e7%ae%97/</link>
					<comments>https://www.codelast.com/shell%e4%b8%8b%e7%9a%84%e6%95%b0%e5%ad%97%e6%af%94%e8%be%83%e5%8f%8a%e8%ae%a1%e7%ae%97/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 14 Sep 2010 03:36:29 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[转帖]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[数字]]></category>
		<category><![CDATA[比较]]></category>
		<category><![CDATA[计算]]></category>
		<category><![CDATA[除法]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=194</guid>

					<description><![CDATA[<p>
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">比较：</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法一： if [ ${A} -lt ${B} ]; then &#8230;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这是最基本的比较方法，使用lt(小于),gt(大于),le(小于等于),ge(大于等于)，优点：还没发现；缺点：只能比较整数，使用lt,gt等不直观</span></span></span><br />
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法二： if ((${A} &#60; ${B})) then &#8230;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span id="more-194"></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这是CShell风格比较，优点：不用使用lt,gt等难记的字符串；缺点：还是只能比较整数</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法三： if (echo ${A} ${B} &#124; awk &#8216;!($1&#62;$2){exit 1}&#8217;) then &#8230;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这是使用awk比较，优点：可以比较小数；缺点：表达式太复杂，难记</span></span></span><br />
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法四： if (echo ${A} &#8211; ${B} &#124; bc -q &#124; grep -q &#8220;^-&#8221;); then &#8230;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这是使用bc计算比较，优点：可以比较小数；缺点：表达式更复杂，难记</span></span></span><br />
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">计算：</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法一：typeset C=$(expr ${A} + ${B});</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">SHELL中的基本工具，优点：方便检测变量是否为数字；缺点：只能计算整数，且只能计算加减法，不能计算乘除法</span></span></span><br />
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法二：let &#8220;C=${A}+${B}&#8221;; 或 let &#8220;C=A+B&#8221;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">内嵌命令计算，优点：能计算乘除法及位运算等；缺点：只能计算整数</span></span></span><br />
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法三：typeset C=$((A+B))</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">CShell风格的计算，优点：能计算乘除法及位运算等，简介，编写方便；缺点：不能计算小数</span></span></span><br />
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法四：typeset C=${echo ${A} ${B} &#124; awk &#8216;{print $1+$2}&#8217;)</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">使用awk计算，优点：能计算小数，可以实现多种计算方式，计算灵活；缺点：表达式太复杂</span></span></span><br />
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法五：typeset C=${echo ${A} + ${B} &#124; bc -q)</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">使用awk计算，优点：能计算小数，计算方式比awk还多，计算灵活；缺点：表达式太复杂，小数点后面的位数必须使用scale=N来设置，否则可能会将结果截断为整数</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#f00;"><span style="font-size:14px;">【以上是转帖，正确性未经验证】</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	&#160;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#800080;">【以下是自己亲自验证过的】</span>：</span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#0000ff;"><span style="font-size:14px;">（1）除法运算</span></span><br />
	<span style="color:#b22222;"><span style="font-size:14px;">经过验证可行的除法运算①（含小数）：</span></span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
RESULT=`echo &#34;scale=1; $A/$B&#34; &#124; bc`
</pre>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#006400;">表示变量A除以变量B，结果保留1位小数，最终结果保存在RESULT中。如果系统中没有安装&#8220;bc&#8221;程序，这条命令就行不通了，那么就可以使用下面的命令。</span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
	<span style="font-family:微软雅黑;"><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></span>&#8230; <a href="https://www.codelast.com/shell%e4%b8%8b%e7%9a%84%e6%95%b0%e5%ad%97%e6%af%94%e8%be%83%e5%8f%8a%e8%ae%a1%e7%ae%97/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">比较：</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法一： if [ ${A} -lt ${B} ]; then &hellip;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这是最基本的比较方法，使用lt(小于),gt(大于),le(小于等于),ge(大于等于)，优点：还没发现；缺点：只能比较整数，使用lt,gt等不直观</span></span></span><br />
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法二： if ((${A} &lt; ${B})) then &hellip;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span id="more-194"></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这是CShell风格比较，优点：不用使用lt,gt等难记的字符串；缺点：还是只能比较整数</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法三： if (echo ${A} ${B} | awk &lsquo;!($1&gt;$2){exit 1}&rsquo;) then &hellip;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这是使用awk比较，优点：可以比较小数；缺点：表达式太复杂，难记</span></span></span><br />
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法四： if (echo ${A} &ndash; ${B} | bc -q | grep -q &ldquo;^-&rdquo;); then &hellip;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">这是使用bc计算比较，优点：可以比较小数；缺点：表达式更复杂，难记</span></span></span><br />
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">计算：</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法一：typeset C=$(expr ${A} + ${B});</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">SHELL中的基本工具，优点：方便检测变量是否为数字；缺点：只能计算整数，且只能计算加减法，不能计算乘除法</span></span></span><br />
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法二：let &ldquo;C=${A}+${B}&rdquo;; 或 let &ldquo;C=A+B&rdquo;</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">内嵌命令计算，优点：能计算乘除法及位运算等；缺点：只能计算整数</span></span></span><br />
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法三：typeset C=$((A+B))</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">CShell风格的计算，优点：能计算乘除法及位运算等，简介，编写方便；缺点：不能计算小数</span></span></span><br />
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法四：typeset C=${echo ${A} ${B} | awk &lsquo;{print $1+$2}&rsquo;)</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">使用awk计算，优点：能计算小数，可以实现多种计算方式，计算灵活；缺点：表达式太复杂</span></span></span><br />
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">方法五：typeset C=${echo ${A} + ${B} | bc -q)</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#000;"><span style="font-size:14px;">使用awk计算，优点：能计算小数，计算方式比awk还多，计算灵活；缺点：表达式太复杂，小数点后面的位数必须使用scale=N来设置，否则可能会将结果截断为整数</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#f00;"><span style="font-size:14px;">【以上是转帖，正确性未经验证】</span></span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	&nbsp;</p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#800080;">【以下是自己亲自验证过的】</span>：</span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#0000ff;"><span style="font-size:14px;">（1）除法运算</span></span><br />
	<span style="color:#b22222;"><span style="font-size:14px;">经过验证可行的除法运算①（含小数）：</span></span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
RESULT=`echo &quot;scale=1; $A/$B&quot; | bc`
</pre>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;"><span style="color:#006400;">表示变量A除以变量B，结果保留1位小数，最终结果保存在RESULT中。如果系统中没有安装&ldquo;bc&rdquo;程序，这条命令就行不通了，那么就可以使用下面的命令。</span></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
	<span style="font-family:微软雅黑;"><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a></span></p>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="color:#b22222;"><span style="font-family:微软雅黑;">经过验证可行的除法运算②（含小数）：</span></span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
RESULT=`awk &#39;BEGIN{printf &quot;%.1f&quot;, &#39;$A&#39;/&#39;$B&#39;}&#39;`
</pre>
<p style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 9px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(77, 77, 77); ">
	<span style="font-family:微软雅黑;">实现的效果与上面的那条命令完全相同。</p>
<p>	<span style="color:#0000ff;">（2）两个小数的大小比较</span><br />
	不能用 gt，lt 等来判断，因为它们只支持整数。小数的大小比较可以用下面的方法：</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
expr $A \&gt; $B
</pre>
<p>
	返回1表示A&gt;B, 返回0表示A&lt;B。</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/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/shell%e4%b8%8b%e7%9a%84%e6%95%b0%e5%ad%97%e6%af%94%e8%be%83%e5%8f%8a%e8%ae%a1%e7%ae%97/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>shell中数组的基本用法</title>
		<link>https://www.codelast.com/shell%e4%b8%ad%e5%88%9d%e5%a7%8b%e5%8c%96%e6%95%b0%e7%bb%84%e5%b9%b6%e9%81%8d%e5%8e%86%e6%95%b0%e7%bb%84/</link>
					<comments>https://www.codelast.com/shell%e4%b8%ad%e5%88%9d%e5%a7%8b%e5%8c%96%e6%95%b0%e7%bb%84%e5%b9%b6%e9%81%8d%e5%8e%86%e6%95%b0%e7%bb%84/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 14 Sep 2010 03:33:42 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[shell数组使用]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=186</guid>

					<description><![CDATA[<p>
<span id="more-186"></span>	<br />
	<span style="background-color:#00ff00;">➤</span> 数组初始化<br />
	假设数组名为ARR，则初始化的一种方法是：</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;">ARR=(2&#160;4&#160;6&#160;8)
</code></pre>
</section>
<p>
	其中，括号里的各项使用空格来分隔。</p>
<p>
	初始化数组的另一种方法是，用其他命令输出的结果来填充到数组中，例如把 ls 命令输出的文件列表填充到数组中：</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;">ARR=(`ls&#160;/opt/`)
<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;FILE&#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;">${ARR[@]}</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-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;<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;">$FILE</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;">done</span></code></pre>
</section>
<p>
<span style="background-color: rgb(0, 255, 0);">➤</span>&#160;遍历数组方法为：</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;"><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;NUM&#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;">${ARR[*]}
</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-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;<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;">$NUM
</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;">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 />
其中，每一次循环过程中，数组里的当前元素均被保存到NUM变量中，使用即可。<br />
如果要定义一个空的数组，直接unset即可：</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;"><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;">unset</span>&#160;ARR
</code></pre>
</section>
<p>
<span style="background-color: rgb(0, 255, 0);">➤</span>&#160;向数组中添加成员</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;">ARR[1]=<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;item_1&#34;</span>
ARR[2]=<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;item_2&#34;</span></code></pre>
</section>
<p>上面的方法使用了指定数组索引的方式来向数组添加多个成员，但如果不想显式指定数组索引，则可以用下面的方法来添加：</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;">ARR+=(<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;item_1&#34;</span>)
ARR+=(<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;item_2&#34;</span>)
</code></pre>
</section>
<p>
<span style="background-color: rgb(0, 255, 0);">➤</span>&#160;取出数组中的一个成员</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;">[root@localhost&#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;">#&#160;echo&#160;${ARR[2]}</span>
item_2
</code></pre>
</section>
<p>
<span style="background-color: rgb(0, 255, 0);">➤</span>&#160;判断一个指定的字符串是否在该数组中：</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;"><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>&#160;<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;<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;">${ARR[@]}</span>&#34;</span>&#160;&#124;&#160;grep&#160;-w&#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;item_1&#34;</span>&#160;&#38;&#62;/dev/null;&#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;">then</span>
&#160;&#160;&#160;&#160;<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;<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;Found&#34;
</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></code></pre>
</section>
<p>其中，ARR是你的数组名，item_1是你要查找的字符串。</p>
<p><span style="background-color: rgb(0, 255, 0);">➤</span>&#160;统计一个文件中某一列出现的频次<br />
这算是数组的一个应用。<br />
假设有文件 1.txt：&#8230; <a href="https://www.codelast.com/shell%e4%b8%ad%e5%88%9d%e5%a7%8b%e5%8c%96%e6%95%b0%e7%bb%84%e5%b9%b6%e9%81%8d%e5%8e%86%e6%95%b0%e7%bb%84/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
<span id="more-186"></span>	<br />
	<span style="background-color:#00ff00;">➤</span> 数组初始化<br />
	假设数组名为ARR，则初始化的一种方法是：</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;">ARR=(2&nbsp;4&nbsp;6&nbsp;8)
</code></pre>
</section>
<p>
	其中，括号里的各项使用空格来分隔。</p>
<p>
	初始化数组的另一种方法是，用其他命令输出的结果来填充到数组中，例如把 ls 命令输出的文件列表填充到数组中：</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;">ARR=(`ls&nbsp;/opt/`)
<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;FILE&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;">${ARR[@]}</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-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;">$FILE</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;">done</span></code></pre>
</section>
<p>
<span style="background-color: rgb(0, 255, 0);">➤</span>&nbsp;遍历数组方法为：</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-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;NUM&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;">${ARR[*]}
</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-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;">$NUM
</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;">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 />
其中，每一次循环过程中，数组里的当前元素均被保存到NUM变量中，使用即可。<br />
如果要定义一个空的数组，直接unset即可：</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-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;">unset</span>&nbsp;ARR
</code></pre>
</section>
<p>
<span style="background-color: rgb(0, 255, 0);">➤</span>&nbsp;向数组中添加成员</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;">ARR[1]=<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;item_1&quot;</span>
ARR[2]=<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;item_2&quot;</span></code></pre>
</section>
<p>上面的方法使用了指定数组索引的方式来向数组添加多个成员，但如果不想显式指定数组索引，则可以用下面的方法来添加：</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;">ARR+=(<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;item_1&quot;</span>)
ARR+=(<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;item_2&quot;</span>)
</code></pre>
</section>
<p>
<span style="background-color: rgb(0, 255, 0);">➤</span>&nbsp;取出数组中的一个成员</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;echo&nbsp;${ARR[2]}</span>
item_2
</code></pre>
</section>
<p>
<span style="background-color: rgb(0, 255, 0);">➤</span>&nbsp;判断一个指定的字符串是否在该数组中：</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-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;<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;<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;">${ARR[@]}</span>&quot;</span>&nbsp;|&nbsp;grep&nbsp;-w&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;item_1&quot;</span>&nbsp;&amp;&gt;/dev/null;&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;Found&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;">fi</span></code></pre>
</section>
<p>其中，ARR是你的数组名，item_1是你要查找的字符串。</p>
<p><span style="background-color: rgb(0, 255, 0);">➤</span>&nbsp;统计一个文件中某一列出现的频次<br />
这算是数组的一个应用。<br />
假设有文件 1.txt：</p>
<blockquote>
<div>
		[codelast@ ~]$ cat 1.txt&nbsp;</div>
<div>
		a</div>
<div>
		a</div>
<div>
		b</div>
<div>
		c</div>
<div>
		a</div>
<div>
		d</div>
<div>
		e</div>
<div>
		e</div>
<div>
		f</div>
<div>
		a</div>
<div>
		a</div>
</blockquote>
<p>现在要统计出 a，b，c，...等分别出现了多少次，可以这样做：</p>
<blockquote>
<p>
		cat 1.txt | awk &#39;{print $1&quot;\t&quot;1}&#39; | awk &#39;{arr[$1]++}END{for (a in arr) print a, arr[a]}&#39;</p>
</blockquote>
<p>解释：<br />
● awk &#39;{print $1&quot;\t&quot;1}&#39; 是给原来只有一列的文件加上一列固定的数字 1，表示每一行的字母出现了1次。<br />
● arr[$1]++ 是累加以第1列为key的数组的value，例如 arr[&quot;a&quot;]++ 会使得 arr 这个数组中，key为 a 的那个元素的value加1。<br />
● END{} 块在处理完文件之后才会被执行。<br />
● for (a in arr) print a, arr[a] 遍历数组 arr 中的每一个元素，打印出其 key，以及 key 对应的value。<br />
执行结果：</p>
<blockquote>
<div>
		a 5</div>
<div>
		b 1</div>
<div>
		c 1</div>
<div>
		d 1</div>
<div>
		e 2</div>
<div>
		f 1</div>
</blockquote>
<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>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/shell%e4%b8%ad%e5%88%9d%e5%a7%8b%e5%8c%96%e6%95%b0%e7%bb%84%e5%b9%b6%e9%81%8d%e5%8e%86%e6%95%b0%e7%bb%84/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
