<?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>字符串 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E5%AD%97%E7%AC%A6%E4%B8%B2/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/%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>
	</channel>
</rss>
