shell中有很多奇特的語法:
比方有下面一段腳本;
#!/bin/sh
files=`find -name *.conifg`
for i in $files
do
name=${i#*/}
dir=${name%/*}
done
name和dir都代表什么呢?
假如
i=this/is/a/path.config
那么
name=is/a/path.config
dir=this/is/a
也就是說%/*代表取從頭到最后一個slash之前的全部內容
#*/代表去取從第一個slash之后的全部內容
為了更好的理解。我們看一下詳細的文檔,可參考Advanced Bash-Scripting Guide一書:
${string#substring}Strip shortest match of $substring from front of $string
${string%substring}Strip shortest match of $substring from back of $string
也就是說
#代表刪除從前往后最小匹配的內容
%代表刪除從后往前最小匹配的內容
這樣明確了吧。
原文:http://blog.csdn.net/hongchangfirst/article/details/28436947
作者:hongchangfirst
hongchangfirst的主頁:http://blog.csdn.net/hongchangfirst
