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