﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-关注编辑器的制作-文章分类-原创</title><link>http://www.cppblog.com/megax/category/6331.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 23 May 2008 06:37:08 GMT</lastBuildDate><pubDate>Fri, 23 May 2008 06:37:08 GMT</pubDate><ttl>60</ttl><item><title>shell里面如何获取字符串长度</title><link>http://www.cppblog.com/megax/articles/44180.html</link><dc:creator>megax</dc:creator><author>megax</author><pubDate>Tue, 11 Mar 2008 06:11:00 GMT</pubDate><guid>http://www.cppblog.com/megax/articles/44180.html</guid><wfw:comment>http://www.cppblog.com/megax/comments/44180.html</wfw:comment><comments>http://www.cppblog.com/megax/articles/44180.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/megax/comments/commentRss/44180.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/megax/services/trackbacks/44180.html</trackback:ping><description><![CDATA[<p>在unixshell下，写有一个shell，测字符串长度？有这样的函数吗？</p>
<p>如果A变量为空的话，在if中怎么表达，直接写 if [[ $A ==&nbsp; ]]，&#8220;==&#8221;后边我怎么表示这个空值？</p>
<p><a id=more-42></a>&nbsp;</p>
<p>——————————————————&#8211;<br>shell里面如何获取字符串长度？</p>
<p>len=`expr length $str`<br>len=`echo $str|awk &#8216;{print length($0)}&#8217;`</p>
<p>if [&nbsp; X$A = X ]</p>
<p>——————————————————&#8211;<br>shell里面如何获取字符串长度？</p>
<p># 判断变量是否为空<br>if [ -z ${A} ]; then<br>&#8230;<br>fi</p>
<p># 得到字符串的长度<br>str=&#8221;hello&#8221;<br>echo ${str} | wc -L</p>
<p>——————————————————&#8211;<br>expr length &#8220;the string you want to get length!&#8221;</p>
<p>例如：输入命令：<br>expr length &#8220;abcd&#8221;<br>将得到结果 4<br>另外expr 还可以对字符串取substr ，index等，可以输入expr &#8211;help<br>查看详细用法</p>
<p>——————————————————&#8211;</p>
<p>echo ${#s}</p>
<p>shell里面如何获取字符串长度？<br>——————————————————&#8211;<br>&#8220;如果A变量为空的话，在if中怎么表达，直接写 if [[ $A ==&nbsp; ]]，&#8220;==&#8221;后边我怎么表示这个空值？&#8221;</p>
<p>if [ &#8220;$param&#8221; = &#8216;&#8217; ];then<br>&nbsp;&nbsp;&nbsp; command<br>fi</p>
<p>注意$param用双引号括起来，否则当为空的时候，会出错。</p>
<p>总结楼上各位</p>
<p>[size=5][size=3]计算字符串长度可用的三种方法：<br>echo &#8220;$str&#8221;|awk &#8216;{print length($0)}&#8217;<br>expr length &#8220;$str&#8221;<br>echo &#8220;$str&#8221;|wc -c<br>但是第三种得出的值会多1，可能是把结束符也计算在内了</p>
<p>判断字符串为空的方法有三种：<br>if [ &#8220;$str&#8221; =&nbsp; &#8220;&#8221; ]<br>if [ x&#8221;$str&#8221; = x ]<br>if [ -z &#8220;$str&#8221; ]<br>注意：都要代双引号，否则有些命令会报错，养成好习惯吧！<br>可能用AWK 稳定一些</p>
<p>以前在用的时候，发现AWK的测试最为稳定和准确<br>UNIX 其它命令有时会出现误差和意外，尤其是在测试汉字串时</p>
<img src ="http://www.cppblog.com/megax/aggbug/44180.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/megax/" target="_blank">megax</a> 2008-03-11 14:11 <a href="http://www.cppblog.com/megax/articles/44180.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>