How to exit all the calling scripts in bash?

萝らか妹 提交于 2019-12-11 02:28:14

问题


Lets say I have the following scripts

a.sh

echo in a
if test 1 -ne 2; then
        echo oops
        exit 1
fi

b.sh

echo in b
./a.sh
echo in b 2

When running b.sh, I want it to exit if a.sh exited. How do I do this?

(The current output is

in b
in a
oops
in b 2

And that's not what I want)

Thanks, Rivka


回答1:


check return status of a command, corresponding variable is $?. alternatively, you can short-circuit using command || exit




回答2:


echo in b
./a.sh && echo in b 2

This basically checks that the first script does not exit non-zero. If that is true, and only then will it run the second function.




回答3:


I don't think there's a way you can do it without explicitly checking the return status of the subshell, e.g.:

# This will run b.sh, and if that exits with a non-zero status, we will also
# exit with that same status; otherwise, we continue.
./b.sh || echo $?


来源:https://stackoverflow.com/questions/2216200/how-to-exit-all-the-calling-scripts-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!