Re: [PATCHv2 4/6] t7510: exit for loop with test result
From: Jeff King <hidden>
Date: 2016-06-15 23:01:37
On Fri, Jun 13, 2014 at 02:33:02PM +0200, Michael J Gruber wrote:
quoted
With X && Y || exit 1 inside the loop, the loop statement will return false, but the loop will continue (if X returns false), which is exactly the problem that the exit avoids. Make your example iterate over false true instead in the inner loop and you'll see ;) Michael... with this loop, sorry: for X in true false; do for Y in false true; do ($X && $Y || exit 1) done echo "$X/last inner $Y: $?" done
I'm somewhat confused, as my loops were meant only to expand the truth
table, not to simulate a real loop in the code. That is why I have a
subshell in the loop around my exit, to make sure we keep looping. In
the real code, the subshell surrounds the whole loop (so that an "exit"
leaves the entire loop without leaving the whole script).
The actual code is more like:
(
for i in a b c; do
echo $i: got to first step &&
test $i != b &&
echo $i: got to second step || exit 1
done
)
echo overall status: $?
which should fail on the second loop iteration. And it does:
a: got to first step
a: got to second step
b: got to first step
overall status: 1
That is, we short-circuit to the "exit 1" as soon as "test $i != b"
fails. You can replace the use of "$?" above with more "&&"-chaining, of
course.
-Peff