暂时对此感到困惑,这是一个代表问题的简化脚本:
#start
# note that master does not exist, so this should fail, would like to exit on the next line
git branch -D master || (echo "no master branch" && exit 1);
git fetch origin &&
git checkout master &&
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH" != "master" ]]; then
echo 'Aborting script because you are not on the right git branch (master).';
exit 1;
fi
echo "done"
#end
当我运行上面的脚本时,我得到了这个输出:
error: branch 'master' not found.
no master branch
error: Your local changes to the following files would be overwritten by checkout:
publish-to-NPM.sh
Please, commit your changes or stash them before you can switch branches.
Aborting
Aborting script because you are not on the right git branch (master).
请注意,“done”不会被回显,因此脚本会在第二个出口1调用时退出。但是为什么脚本不会在第一个出口1调用时退出?对此感到困惑。
答案 0 :(得分:4)
git branch -D master || (echo "no master branch" && exit 1);
在子进程环境中运行条件的RHS。退出退出该子流程。如果要退出主脚本,请不要在子进程中运行它。也就是说,写:
git branch -D master || { echo "no master branch" && exit 1; }
答案 1 :(得分:0)
当然,你可以在if条件下调用exit。
退出将以任何语言结束当前进程,包括bash(尽管如果你是线程或分叉,它会比这更复杂,但它看起来不像你同时执行任何东西)。