我在阅读下面这个参数时遇到问题:
我不明白使用这个
$(basename "$0")
的目的是从哪里来的。${BINARY%/*}
似乎试图获取目录的路径,但究竟为什么需要这样。
DIR_NAME=$(dirname "$0")
FILE_NAME=$(basename "$0")
BINARY=`readlink ${ROOT_DIR}/${DIR_NAME}/${FILE_NAME} -f`
BIN_PATH=${BINARY%/*}
最佳答案
$0
是正在运行的脚本的路径名。所以$(dirname "$0")
返回脚本的目录,而$(basename "$0")
是文件名。${BINARY%/*}
在 Shell Parameter Expansion 中解释
${parameter%word}
${parameter%%word}
Theword
is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value ofparameter
, then the result of the expansion is the value ofparameter
with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted.
因此,这会找到 $BINARY
的尾部与 /*
匹配的部分并将其删除,从而返回目录部分。它等同于 $(dirname "$BINARY")
关于linux - 不理解 bash 参数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052242/