获取当前执行的 shell 脚本运行的路径 busybox



在ConTeXt独立中,文件tex/setuptex包含需要当前路径tex/setuptex的代码(通常例如 $HOME/context/opt/context等(。 代码如下所示:

# this resolves to path of the setuptex script
# We use $0 for determine the path to the script, except for:
# * bash where $0 always is bash; here we use BASH_SOURCE
# * ksh93 where we use ${.sh.file}
# Thanks to Vasile Gaburici and Alessandro Perucchi for reporting this
# * http://www.ntg.nl/pipermail/ntg-context/2008/033953.html
# * http://www.ntg.nl/pipermail/ntg-context/2012/068658.html
if [ z"$BASH_SOURCE" != z ]; then
        SCRIPTPATH="$BASH_SOURCE"
elif [ z"$KSH_VERSION" != z ]; then
        SCRIPTPATH="${.sh.file}"
else
        SCRIPTPATH="$0"
fi

通常运行它以使用运行 ConTeXt 所需的各种环境变量更新当前环境,并带有 . path/tex/setuptex .

在BusyBox(例如Alpine Linux上(,$SCRIPTPATH/的,并且不清楚获取路径的正确方法是什么。 将此行添加到脚本中:

echo "SCRIPTPATH $0 : $1 : $2"

收益 率:

脚本路径 sh : :

同样env setuptex也不会产生任何结果。

所以我不确定从哪里开始。

如何复制通常用于获取当前正在执行的脚本的路径的*sh功能?

来自 Henri Menke(通过电子邮件( -

标准 POSIX sh 无法可靠地检测脚本的源调用。 BusyBox 在下面使用 POSIX sh,因此受到同样的限制。看 StackOverflow详细介绍:如何在POSIX sh中获取脚本目录?

最新更新