Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bash脚本能否区分作为脚本调用和作为";资料来源;?_Bash - Fatal编程技术网

bash脚本能否区分作为脚本调用和作为";资料来源;?

bash脚本能否区分作为脚本调用和作为";资料来源;?,bash,Bash,我有一个bash脚本,其中包含: exit 1 当我“源”这个脚本而不是运行它时,它会导致调用者退出 有没有一种方法可以让脚本确定它是以“源”运行的,而不是作为脚本运行的?您可以在脚本中使用此检查: [[ $0 = $BASH_SOURCE ]] && echo "normal run" || echo "sourced run" 或者使用if/else/fi无论您在哪里呼叫exit: if [[ $0 = $BASH_SOURCE ]]; then exit 1 el

我有一个bash脚本,其中包含:

exit 1
当我“源”这个脚本而不是运行它时,它会导致调用者退出


有没有一种方法可以让脚本确定它是以“源”运行的,而不是作为脚本运行的?

您可以在脚本中使用此检查:

[[ $0 = $BASH_SOURCE ]] && echo "normal run" || echo "sourced run"
或者使用
if/else/fi
无论您在哪里呼叫
exit

if [[ $0 = $BASH_SOURCE ]]; then
   exit 1
else
   # don't call exit
   echo "some error..."
fi
看见