#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)讀到這樣一段代碼的時(shí)候,我們很容易猜到${current_dir##*/}是為了得到最后一級俄目錄名,如全路徑為/a/b/c/d/e, 則剛才那段代碼是為了得到最后一級目錄名e.
可是##*/是怎么濾出最后一級目錄名的呢?
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
這個(gè)網(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##*/}就是把一個(gè)字符串中最后一個(gè)斜杠后面的字符串過濾出來。我們來做幾個(gè)試驗(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)在有一個(gè)字符串 S=AAA.BBB.CCC.DDDD,要取出DDDD,該怎么寫?
答案:
# S=AAA.BBB.CCC.DDD
# o=${S##*.}
# echo $o
DDD