linux - 不理解 bash 参数声明

标签 linux command

我在阅读下面这个参数时遇到问题:

  1. 我不明白使用这个 $(basename "$0") 的目的是从哪里来的。

  2. ${BINARY%/*} 似乎试图获取目录的路径,但究竟为什么需要这样。

DIR_NAME=$(dirname "$0")
FILE_NAME=$(basename "$0")
BINARY=`readlink ${ROOT_DIR}/${DIR_NAME}/${FILE_NAME} -f`
BIN_PATH=${BINARY%/*}

最佳答案

  1. $0 是正在运行的脚本的路径名。所以 $(dirname "$0") 返回脚本的目录,而 $(basename "$0") 是文件名。

  2. ${BINARY%/*}Shell Parameter Expansion 中解释

${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter 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/

相关文章:

c - fork 一个 child ,从控制台读取

linux - Word 未扩展为 bash 中的 shell 命令

linux - 如何添加 Nagios 自定义状态

Windows 从修改电源条件的命令提示符创建任务计划

linux - 命令说明

bash - `echo $?` 在 bash 中是什么意思?

mysql - 如何检测命令输出是否正在unix管道中使用

linux - 如何计算进程中可用的 "Max Contiguous Free Space"?

linux - 使用 SSH 提交时无法识别 R 命令

linux - Tcpdump 命令 - Linux - Unix