当前执行shell脚本的路径使用busybox

问题描述 投票:0回答:2
在上下文中,文件

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
.

在BusyBox(例如在Alpine Linux上),the是
. path/tex/setuptex

,这并不明显是正确的方法是什么。 将此行添加到脚本:

$SCRIPTPATH
Yields:

ScriptPathSH ::

相似
/

echo "SCRIPTPATH $0 : $1 : $2"
没有任何东西可以用

env

所以我不确定从哪里开始。
如何复制一个通常用于获取当前执行脚本的路径的功能?
    

从Henri Menke(通过电子邮件) -

标准Posix SH无法可靠地检测到脚本的源调用。
  Busybox使用下面的Posix SH,因此受到相同的限制。看
  详细的stackoverflow:
如何在posix sh中获取脚本目录?

shell sh posix busybox
2个回答
1
投票
Busybox具有基本的

setuptex

,可实现采购和路径检测:

*sh 为脚本链。 这是一个与BusyBox和常规

lsof

0
投票
lsof -p $$ 2>/dev/null | grep -E '\.sh$' - | awk '{printf "%s\n",$NF}'

明显地,如果脚本并非全部以
lsof

的结尾,您必须更改正则。
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.