请解释以下语法:
i##*.
i%.*
我明白它在做什么,但我想知道一般模式(它为什么/如何这样做)。
出现的代码:
#!/bin/bash
recursive_name_change()
{
cd "$1"
for i in *
do
#echo "${i##*.}"
if [ -d "$i" ]
then
recursive_name_change "$i"
elif [ "${i##*.}" = "cpp" ]
then
new_name=${i%.*}".c"
mv "$i" "$new_name"
fi
done
cd ../
}
recursive_name_change .
还请大神指点,这些奇特的语法形式从哪里找?
请参阅 man bash
中的参数扩展:
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce a pattern just
as in pathname expansion. If the pattern matches the beginning of the value of
parameter, then the result of the expansion is the expanded value of parameter
with the shortest matching pattern (the #
case) or the longest matching pattern (the ##
case) deleted. If parameter is @ or *, the pattern removal
operation is applied to each positional parameter in turn, and the expansion is
the resultant list. If parameter is an array variable subscripted with @ or *,
the pattern removal operation is applied to each member of the array in turn,
and the expansion is the resultant list.
${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce a pattern just
as in pathname expansion. If the pattern matches a trailing portion of the
expanded value of parameter, then the result of the expansion is the expanded
value of parameter with the shortest matching pattern (the %
case) or the
longest matching pattern (the %%
case) deleted. If parameter is @ or *,
the pattern removal operation is applied to each positional parameter in turn,
and the expansion is the resultant list. If parameter is an array variable
subscripted with @ or *, the pattern removal operation is applied to each mem-
ber of the array in turn, and the expansion is the resultant list.
简而言之,#
去掉左边的模式,%
去掉右边的模式,加倍符号使匹配变得贪婪。 (助记:#
在大多数键盘上位于 %
的左侧)。