bash:如果内部命令以非零代码退出,则停止for循环

时间:2018-03-02 16:18:17

标签: bash

我有这段代码:

for x in 1 2 3;
do
  ./test.sh $x
done

test.sh看起来像这样

echo "$1"
if [ "$1" = "2" ]
then
  exit 1
else
  exit 0
fi

如何才能输出

1
2

而不是

1
2
3

1 个答案:

答案 0 :(得分:2)

如果test.sh失败,则

break

for x in 1 2 3;
do
  ./test.sh $x || break
done