bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Ill positioned 'until' keyword


From: Dan Douglas
Subject: Re: Ill positioned 'until' keyword
Date: Wed, 14 Dec 2011 19:07:12 -0600
User-agent: KMail/4.7.3 (Linux/3.1.4-pf+; KDE/4.7.3; x86_64; ; )

On Wednesday, December 14, 2011 05:47:24 PM Peng Yu wrote:
> Hi,
> 
> I looks a little wired why 'until' is the way it is now. According to
> the manual until is before the do-done block.
> 
> until test-commands; do consequent-commands; done
> 
> A common design of until in other language is that it allows the loop
> body be executed at least once and test the condition at the end of
> the run of the first time. It seems that a better of bash should also
> follow the practice. If I understand it correctly, the above is exact
> the same as the following, in which case the do done block can be
> executed zero time. Because of this, I think that the current 'until'
> is not necessary, and probably better to change its definition so that
> it allows the execution of the loop at least once.
> 
> while ! test-commands; do consequent-commands; done
> 
> 
> In short, I'd expect the following code echo 9 (not working with the
> current bash).
> 
> COUNTER=9
> do
>              echo COUNTER $COUNTER
>              let COUNTER-=1
> done
> until [  $COUNTER -lt 10 ];
> 
> 
> I'd like to hear for what reason 'until' is designed in the way it is
> now. Shall we considered to at least allow an option in bash to change
> it meaning to the one I propose (or adding a different command, like
> until2, for what I proposed), which give us time to let the orignal
> until usage dies out.

You're right, most non-shell languages do it that way. The current behavior is 
consistent with most shells and the way it's specified by SUS: 
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_11

"until" in Bash is the same as:

    while ! { list 1; }; do
        list 2
    done

Here's one of the many possible workarounds:

    declare -i x=5

    until
        echo $x
        (( x-- < 1 ))
        do :
    done

The echo is guaranteed to execute at least once. The arithmetic is equivalent 
to list 2. Some additional work is needed to make the exit status of this 
pattern equivalent to that of the current until, but it's doable.

If it were changed there would need to be a separate behavior for POSIX mode.

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]