#Verify if current working directory ends with '/xxx'
current_dir=`pwd`
if [ ${current_dir##*/} != 'xxx' ]
then
echo "Error: Current directory must be end with '/xxx'"
exit -1
fi
當(dāng)讀到這樣一段代碼的時候,我們很容易猜到${current_dir##*/}
是為了得到最后一級俄目錄名驱还,如全路徑為/a/b/c/d/e
, 則剛才那段代碼是為了得到最后一級目錄名e
.
可是##*/
是怎么濾出最后一級目錄名的呢?
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
這個網(wǎng)頁是shell script的規(guī)范随静,在其中搜索##囚企,可以看到:
${parameter##[word]}
Remove Largest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest portion of the prefix matched by the pattern deleted.
在這段文字的上方,不遠(yuǎn)處馍惹,可以看到Pattern Matching Notation躺率,從中可知,*/
是一種匹配方式万矾,等于‘以斜杠結(jié)尾的任何字符串’悼吱。
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13
這段文字的下方,給出了例子:
${parameter##word}
x=/one/two/three
echo ${x##*/}
three
${string##*/
}就是把一個字符串中最后一個斜杠后面的字符串過濾出來良狈。我們來做幾個試驗(yàn):
# VAR="/a/b/c/d/e"
# echo ${VAR##*/}
e
# echo ${VAR##*c}
/d/e
# echo ${VAR##*b}
/c/d/e
練習(xí)
現(xiàn)在有一個字符串 S=AAA.BBB.CCC.DDDD后添,要取出DDDD,該怎么寫薪丁?
答案:
# S=AAA.BBB.CCC.DDD
# o=${S##*.}
# echo $o
DDD