我将一个 shell 脚本的一部分提取到另一个 shell 脚本中,并且由于某种原因在调用子脚本后,命令替换停止工作。我无法想象这是子脚本中的内容,但我也看不出我做错了什么。
我是否这样做:
dirname "$0" # works
echo "`dirname "$0"`" # works
echo "$(dirname "$0")" # works
cat <<< "`dirname "$0"`" # works
cat <<< "$(dirname "$0")" # works
"$(dirname "$0")"/setup_buildroot.sh "$BUILDROOT" # call the sub-script
dirname "$0" # works
echo "`dirname "$0"`" # empty string
echo "$(dirname "$0")" # empty string
cat <<< "`dirname "$0"`" # crash
cat <<< "$(dirname "$0")" # crash
或者我做:
dirname "$0" # works
echo "`dirname "$0"`" # works
echo "$(dirname "$0")" # works
cat <<< "`dirname "$0"`" # works
cat <<< "$(dirname "$0")" # works
"`dirname "$0"`"/setup_buildroot.sh "$BUILDROOT" # call the sub-script
dirname "$0" # works
echo "`dirname "$0"`" # empty string
echo "$(dirname "$0")" # empty string
cat <<< "`dirname "$0"`" # crash
cat <<< "$(dirname "$0")" # crash
dirname 似乎工作正常,但反引号并$()
停止工作。命令替换只返回一个空字符串。
编辑:删除所有双引号""
没有效果
EDIT2:调用子脚本时显式执行 bash 无效
EDIT3:保存"$(dirname "$0")"
到变量并使用它没有效果
EDIT4:向echo
子脚本添加相同的命令表明子脚本中的一切都很好
EDIT5:根据要求,脚本的输出
/home/jayen/rep
/home/jayen/rep
/home/jayen/rep
/home/jayen/rep
/home/jayen/rep
#setup_buildroot.sh output here
/home/jayen/rep
bash: syntax error near unexpected token `newline'
bash: syntax error near unexpected token `newline'