题
可以说我有以下脚本
<强> 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
在运行b.sh,我希望它退出,如果退出a.sh。 我该怎么做呢?
(当前输出是
in b
in a
oops
in b 2
而这不是我想要的)
谢谢, Rivka
解决方案
的命令检查返回状态,对应的变量是$?
。
或者,也可以使用command || exit
短路
其他提示
echo in b
./a.sh && echo in b 2
这基本上检查该第一脚本不退出非零。如果这是真的,然后才将它运行第二个功能。
我不认为有一种方法可以做到这一点不明确检查子shell的返回状态,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 $?
不隶属于 StackOverflow