在寫(xiě)一個(gè)簡(jiǎn)單的腳本的時(shí)候,想用到切片功能,在python里實(shí)現(xiàn)非常簡(jiǎn)單,也非常好用 。印象中shell 也可以實(shí)現(xiàn),查了下,發(fā)現(xiàn)自己之前就做過(guò)shell 字符串截?cái)嗪颓衅目偨Y(jié):shell字符串操作小結(jié) 。這里再細(xì)化下。
一、字符串切片
語(yǔ)法如下:
${variable_name:start_position:length}
指定字符串的起始位置及長(zhǎng)度進(jìn)行切片操作。
示例如下:
$ string=abcdefghijklmnopqrstuvwxyz
$ echo ${string:4}
efghijklmnopqrstuvwxyz
注意:起始字符的索引從0開(kāi)始計(jì)數(shù)。這點(diǎn)和python相同,上面的示例是從第五個(gè)字符開(kāi)始,打印到字符結(jié)束 。如果后面指定長(zhǎng)度結(jié)果如下:
$ echo ${string:4:8}
efghijkl
從第五個(gè)字符開(kāi)始,打印8個(gè)字符。
同python一樣,其也支持后向切片,即索引標(biāo)記為負(fù)值。這里和python不同的是,負(fù)值必須放在括號(hào)內(nèi),如下:
echo ${string:(-1)}
z
echo ${string:(-2):2}
yz
如果不加括號(hào),也可以按下面這種寫(xiě)法來(lái)用:
echo ${string:0-1}
z
echo ${string:0-2:2}
yz
二、字符串截取
截取字符串的方法有如下幾種:
${#parameter}獲得字符串的長(zhǎng)度
${parameter%word} 最小限度從后面截取word
${parameter%%word} 最大限度從后面截取word
${parameter#word} 最小限度從前面截取word
${parameter##word} 最大限度從前面截取word
示例執(zhí)行結(jié)果如下:
SUSEt:~# var=http://www.361way.com/donate
SUSEt:~# echo ${#var}
28
SUSEt:~# echo ${var%/}
輸出結(jié)果:http://www.361way.com/donate
SUSEt:~# echo ${var%/*}
輸出結(jié)果:http://www.361way.com
SUSEt:~# echo ${var%don}
輸出結(jié)果:http://www.361way.com/donate
SUSEt:~# echo ${var%don*}
輸出結(jié)果:http://www.361way.com/
SUSEt:~# echo ${var%%on}
輸出結(jié)果:http://www.361way.com/donate
SUSEt:~# echo ${var%%361way*}
輸出結(jié)果:http://www.
SUSEt:~# echo ${var##*/}
輸出結(jié)果:donate
SUSEt:~# echo ${var##/*}
輸出結(jié)果:http://www.361way.com/donate
SUSEt:~# echo ${var#*/}
/www.361way.com/donate
看見(jiàn)感覺(jué)有點(diǎn)暈了吧,記住這里*星號(hào)是必須的,不能省略。不然取到的是完整值 。
三、默認(rèn)值獲取
這里還有一個(gè)不太經(jīng)常用到的高級(jí)用法,用于獲取默認(rèn)值。
${parameter:-word}
${parameter:=word}
${parameter:?word}
${parameter:+word}
先來(lái)看看英文意思:
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
${parameter:=word}
If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.
${parameter:?word}
If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.
${parameter:+word}
If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
第一個(gè)用法,是給變量設(shè)置默認(rèn)值,如果該參數(shù)不存在,后面的值就是默認(rèn)值,如下示例:
SUSEt:~# u=${1:-root}
SUSEt:~# echo $u
root
$1如果不存在,$u的值是root,如果存在,$u的值是$1。
第二個(gè)也是設(shè)置默認(rèn)值 ,具體如下:
SUSEt:~# echo $USER
root
SUSEt:~# echo ${USER:=foo}
root
SUSEt:~# unset USER
SUSEt:~# echo ${USER:=foo}
foo
SUSEt:~# echo $USER
foo
第三個(gè)值主要是在不存在時(shí)報(bào)一個(gè)相應(yīng)的message信息,如下:
SUSEt:~# message=${varName?Error varName is not defined}
-bash: varName:Error varName is not defined
參考文檔:
Shell-Parameter-Expansion
bash-hackers
bash-shell-parameter-substitution(關(guān)于第三項(xiàng)默認(rèn)值講解的不錯(cuò))