<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[Shell常用处理字符串方法(备查)]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 14 Jan 2010 05:12:46 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	一、构造字符串<br/><br/>直接构造<br/>STR_ZERO=hello<br/>STR_FIRST=&quot;i am a string&quot;<br/>STR_SECOND=&#039;success&#039;<br/><br/>重复多次<br/>#repeat the first parm($1) by $2 times<br/>strRepeat()<br/>{<br/>local x=$2<br/>if [ &quot;$x&quot; == &quot;&quot; ]; then<br/>x=0<br/>fi<br/><br/>local STR_TEMP=&quot;&quot;<br/>while [ $x -ge 1 ];<br/>do<br/>STR_TEMP=`printf &quot;%s%s&quot; &quot;$STR_TEMP&quot; &quot;$1&quot;`<br/>x=`expr $x - 1`<br/>done<br/>echo $STR_TEMP<br/>}<br/><br/>举例：<br/>STR_REPEAT=`strRepeat &quot;$USER_NAME&quot; 3`<br/>echo &quot;repeat = $STR_REPEAT&quot;<br/><br/><br/>二、赋值与拷贝<br/><br/>直接赋值<br/>与构造字符串一样<br/>USER_NAME=terry<br/><br/>从变量赋值<br/>ALIASE_NAME=$USER_NAME<br/><br/><br/>三、联接<br/><br/>直接联接两个字符串<br/>STR_TEMP=`printf &quot;%s%s&quot; &quot;$STR_ZERO&quot; &quot;$USER_NAME&quot;`<br/>使用printf可以进行更复杂的联接<br/><br/><br/>四、求长<br/><br/>求字符数(char)<br/>COUNT_CHAR=`echo &quot;$STR_FIRST&quot; &#124; wc -m`<br/>echo $COUNT_CHAR<br/><br/>求字节数(byte)<br/>COUNT_BYTE=`echo &quot;$STR_FIRST&quot; &#124; wc -c`<br/>echo $COUNT_BYTE<br/><br/>求字数(word)<br/>COUNT_WORD=`echo &quot;$STR_FIRST&quot; &#124; wc -w`<br/>echo $COUNT_WORD<br/><br/><br/>五、比较<br/><br/>相等比较<br/>str1 = str2<br/><br/>不等比较<br/>str1 != str2<br/><br/>举例：<br/>if [ &quot;$USER_NAME&quot; = &quot;terry&quot; ]; then<br/>echo &quot;I am terry&quot;<br/>fi<br/><br/>小于比较<br/>#return 0 if the two string is equal, return 1 if $1 &lt; $2, else 2strCompare() { local x=0 if [ &quot;$1&quot; != &quot;$2&quot; ]; then x=2 localTEMP=`printf &quot;%s&#92;n%s&quot; &quot;$1&quot; &quot;$2&quot;` local TEMP2=`(echo &quot;$1&quot;; echo &quot;$2&quot;) &#124;sort` if [ &quot;$TEMP&quot; = &quot;$TEMP2&quot; ]; then x=1 fi fi echo $x }<br/><br/><br/>六、测试<br/><br/>判空<br/>-z str<br/><br/>判非空<br/>-n str<br/><br/>是否为数字<br/># return 0 if the string is num, otherwise 1<br/>strIsNum()<br/>{<br/>local RET=1<br/>if [ -n &quot;$1&quot; ]; then<br/>local STR_TEMP=`echo &quot;$1&quot; &#124; sed &#039;s/[0-9]//g&#039;`<br/>if [ -z &quot;$STR_TEMP&quot; ]; then<br/>RET=0<br/>fi<br/>fi<br/>echo $RET<br/>}<br/><br/>举例：<br/>if [ -n &quot;$USER_NAME&quot; ]; then<br/>echo &quot;my name is NOT empty&quot;<br/>fi<br/><br/>echo `strIsNum &quot;9980&quot;`<br/><br/><br/>七、分割<br/><br/>以符号＋为准，将字符分割为左右两部分<br/>使用sed<br/>举例：<br/>命令 date --rfc-3339 seconds 的输出为<br/>2007-04-14 15:09:47+08:00<br/>取其＋左边的部分<br/>date --rfc-3339 seconds &#124; sed &#039;s/+[0-9][0-9]:[0-9][0-9]//g&#039;<br/>输出为<br/>2007-04-14 15:09:47<br/>取+右边的部分<br/>date --rfc-3339 seconds &#124; sed &#039;s/.*+//g&#039;<br/>输出为<br/>08:00<br/><br/>以空格为分割符的字符串分割<br/>使用awk<br/>举例：<br/>STR_FRUIT=&quot;Banana 0.89 100&quot;<br/>取第3字段<br/>echo $STR_FRUIT &#124; awk &#039;{ print $3; }&#039;<br/><br/><br/>八、子字符串<br/><br/>字符串1是否为字符串2的子字符串<br/># return 0 is $1 is substring of $2, otherwise 1<br/>strIsSubstring()<br/>{<br/>local x=1<br/>case &quot;$2&quot; in<br/>*$1*) x=0;;<br/>esac<br/>echo $x<br/>}<br/><br/>Shell字符串截取<br/>一、Linux shell 截取字符变量的前8位，有方法如下：<br/><br/>1.expr substr “$a” 1 8<br/>2.echo $a&#124;awk ‘{print substr(,1,8)}’<br/>3.echo $a&#124;cut -c1-8<br/>4.echo $<br/>5.expr $a : ‘&#92;(.&#92;&#92;).*’<br/>6.echo $a&#124;dd bs=1 count=8 2&gt;/dev/null<br/><br/><br/>二、按指定的字符串截取<br/><br/>1、第一种方法:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* ${varible##*string} 从左向右截取最后一个string后的字符串<br/>&nbsp;&nbsp;&nbsp;&nbsp;* ${varible#*string}从左向右截取第一个string后的字符串<br/>&nbsp;&nbsp;&nbsp;&nbsp;* ${varible%%string*}从右向左截取最后一个string后的字符串<br/>&nbsp;&nbsp;&nbsp;&nbsp;* ${varible%string*}从右向左截取第一个string后的字符串<br/><br/>“*”只是一个通配符可以不要<br/><br/>例子：<br/>$ MYVAR=foodforthought.jpg<br/>$ echo ${MYVAR##*fo}<br/>rthought.jpg<br/>$ echo ${MYVAR#*fo}<br/>odforthought.jpg<br/><br/>2、第二种方法：${varible:n1:n2}:截取变量varible从n1到n2之间的字符串。<br/><br/>可以根据特定字符偏移和长度，使用另一种形式的变量扩展，来选择特定子字符串。试着在 bash 中输入以下行：<br/>$ EXCLAIM=cowabunga<br/>$ echo ${EXCLAIM:0:3}<br/>cow<br/>$ echo ${EXCLAIM:3:7}<br/>abunga<br/><br/>这种形式的字符串截断非常简便，只需用冒号分开来指定起始字符和子字符串长度。<br/><br/>三、按照指定要求分割：<br/>比如获取后缀名<br/>ls -al &#124; cut -d “.” -f2<br/><br/><br/><br/>shell (bash) 比较运算符<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>运算符&nbsp;&nbsp;描述&nbsp;&nbsp;示例<br/>文件比较运算符<br/>-e filename&nbsp;&nbsp;如果 filename 存在，则为真&nbsp;&nbsp;[ -e /var/log/syslog ]<br/>-d filename&nbsp;&nbsp;如果 filename 为目录，则为真&nbsp;&nbsp;[ -d /tmp/mydir ]<br/>-f filename&nbsp;&nbsp;如果 filename 为常规文件，则为真&nbsp;&nbsp;[ -f /usr/bin/grep ]<br/>-L filename&nbsp;&nbsp;如果 filename 为符号链接，则为真&nbsp;&nbsp;[ -L /usr/bin/grep ]<br/>-r filename&nbsp;&nbsp;如果 filename 可读，则为真&nbsp;&nbsp;[ -r /var/log/syslog ]<br/>-w filename&nbsp;&nbsp;如果 filename 可写，则为真&nbsp;&nbsp;[ -w /var/mytmp.txt ]<br/>-x filename&nbsp;&nbsp;如果 filename 可执行，则为真&nbsp;&nbsp;[ -L /usr/bin/grep ]<br/>filename1 -nt filename2&nbsp;&nbsp;如果 filename1 比 filename2 新，则为真&nbsp;&nbsp;[ /tmp/install/etc/services -nt /etc/services ]<br/>filename1 -ot filename2&nbsp;&nbsp;如果 filename1 比 filename2 旧，则为真&nbsp;&nbsp;[ /boot/bzImage -ot arch/i386/boot/bzImage ]<br/>字符串比较运算符 [size=-1]（请注意引号的使用，这是防止空格扰乱代码的好方法）<br/>-z string&nbsp;&nbsp;如果 string 长度为零，则为真&nbsp;&nbsp;[ -z &quot;$myvar&quot; ]<br/>-n string&nbsp;&nbsp;如果 string 长度非零，则为真&nbsp;&nbsp;[ -n &quot;$myvar&quot; ]<br/>string1 = string2&nbsp;&nbsp;如果 string1 与 string2 相同，则为真&nbsp;&nbsp;[ &quot;$myvar&quot; = &quot;one two three&quot; ]<br/>string1 != string2&nbsp;&nbsp;如果 string1 与 string2 不同，则为真&nbsp;&nbsp;[ &quot;$myvar&quot; != &quot;one two three&quot; ]<br/>算术比较运算符<br/>num1 -eq num2&nbsp;&nbsp;等于&nbsp;&nbsp;[ 3 -eq $mynum ]<br/>num1 -ne num2&nbsp;&nbsp;不等于&nbsp;&nbsp;[ 3 -ne $mynum ]<br/>num1 -lt num2&nbsp;&nbsp;小于&nbsp;&nbsp;[ 3 -lt $mynum ]<br/>num1 -le num2&nbsp;&nbsp;小于或等于&nbsp;&nbsp;[ 3 -le $mynum ]<br/>num1 -gt num2&nbsp;&nbsp;大于&nbsp;&nbsp;[ 3 -gt $mynum ]<br/>num1 -ge num2&nbsp;&nbsp;大于或等于&nbsp;&nbsp;[ 3 -ge $mynum ]<br/>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] Shell常用处理字符串方法(备查)]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>