【问题标题】:Get path of currently executing shell script run with busybox获取使用busybox运行的当前执行shell脚本的路径
【发布时间】:2018-09-20 04:56:41
【问题描述】:

在独立的 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"

产量:

SCRIPTPATH sh : :

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

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

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

【问题讨论】:

  • 问题是您正在获取文件 (. /path/to/context) 而不是运行它 (sh /path/to/context)。我没有适合您的解决方案,但您可以考虑解决方法,例如:. /path/to/context /path/to/context 将在$1 中存储答案(可能相对于$PWD
  • 感谢@AdamKatz;使用 . 采购是在 ConTeXt 的说明中,很多人都会熟悉。
  • 是的,我假设您需要.source,因为导入了一些环境、函数和/或别名的组合,而您无法通过新的shell 引入这些组合。这就是为什么我能提供的只是那种尴尬的解决方法。这可能是一个busybox错误。

标签: shell sh posix busybox


【解决方案1】:

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

标准 POSIX sh 无法可靠地检测脚本的源调用。 BusyBox 在下面使用 POSIX sh,因此受到同样的限制。看 StackOverflow 详情:how to get script directory in POSIX sh?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    相关资源
    最近更新 更多