使用 busybox 获取当前正在执行的 shell 脚本 运行 的路径

Get path of currently executing shell script run with busybox

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

# this resolves to path of the setuptex script
# We use [=12=] for determine the path to the script, except for:
# * bash where [=12=] 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="[=12=]"
fi

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

在 BusyBox 中(例如 Alpine Linux),$SCRIPTPATH/,获取路径的正确方法并不明显。将此行添加到脚本中:

echo "SCRIPTPATH [=13=] :  : "

产量:

SCRIPTPATH sh : :

类似地,envsetuptex 中不产生任何结果。

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

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

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

Standard POSIX sh has no way to reliably detect sourced invocation of a script. BusyBox uses POSIX sh underneath and thus suffers from the same limitation. See Whosebug for detail: