Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Loops and pipes



> Here is an alternate version of the second patch that emits a warning rather than an error when breaking/continuing beyond the outermost loop in the same subshell. This version also prevents "continue" from continuing the outermost loop when continuing beyond it. The following script will only print A (and a warning), while it currently prints A, B, and C.

I remain uncertain about this because both bash and ksh silently
continue the outer loop on "continue N" for N > number of enclosing
loops, that is, they print A B and C.

To me it looks utterly wrong that the "continue" in "for x in a b; do continue 2; done" continues the loop but POSIX states the following:

If n is specified, the continue utility shall return to the top of the nth enclosing for, while, or until loop. […] If n is greater than the number of enclosing loops, the outermost enclosing loop shall be used. If there is no enclosing loop, the behavior is unspecified.

In my opinion that doesn't make much sense but that's what it is. So, the outermost loop should be continued and there should be no warning message, neither for "continue", nor for "break" as POSIX has similar language for it stating that a too large number defaults to the outermost loop.

What about cases where the targeted loop is in a different subshell?

Thanks to the first patch, the "break" in "for x in a b; do ( break ) done" now triggers a warning, which can be justified by the fact that the user most likely overlooked the fact that the enclosing loop is in a different subshell. However, the "break 2" in "for x in a b; do ( for y in c d; do break 2; done ) done" doesn't trigger any warning even though here too it's likely that the user overlooked the fact that the outermost loop is in a different subshell. Wdyt? Wouldn't it make sense to warn whenever one tries to reach a loop in a different subshell, not just when the first enclosing loop is in a different subshell, even though bash only does the latter?

Philippe



Messages sorted by: Reverse Date, Date, Thread, Author