gpt4 book ai didi

bash 参数扩展 ${parameter##word}

转载 作者:行者123 更新时间:2023-11-29 09:27:56 36 4
gpt4 key购买 nike

我正在通读 apache 启动脚本,试图解决我的服务器的一些问题,但在一开始有一个我不太明白的参数扩展。

SCRIPTNAME="${0##*/}"
SCRIPTNAME="${SCRIPTNAME##[KS][0-9][0-9]}"
if [ -n "$APACHE_CONFDIR" ] ; then
if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
DIR_SUFFIX="${APACHE_CONFDIR##/etc/apache2-}"
else
DIR_SUFFIX=
fi
elif [ "${SCRIPTNAME##apache2-}" != "$SCRIPTNAME" ] ; then
DIR_SUFFIX="-${SCRIPTNAME##apache2-}"
APACHE_CONFDIR=/etc/apache2$DIR_SUFFIX
else
DIR_SUFFIX=
APACHE_CONFDIR=/etc/apache2
fi

我只是想了解一下 ${parameter##word} 构造的作用,因为 bash 引用手册来自 gnu我不清楚。手册是这样定义的……

${parameter#word} ${parameter##word}

the word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning 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 member of the array in turn, and the expansion is the resultant list.

这是否意味着第一行将一个空字符串存储回 SCRIPTNAME 或者我只是偏离了基础?

最佳答案

第一行将当前文件的basename存储在SCRIPTNAME中。 $0 是(通常)当前脚本的名称。参见 this相关问题进行讨论。

第二行然后从名称中去除前缀​​ K##S##(假设 /etc/init.d链接命名约定。

关于bash 参数扩展 ${parameter##word},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30038647/

36 4 0