Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14180

Re: Conditions with logical operators and nested groups execute "if" and "else"

From L A Walsh <bash@tlinx.org>
Newsgroups gnu.bash.bug
Subject Re: Conditions with logical operators and nested groups execute "if" and "else"
Date 2018-05-30 16:20 -0700
Message-ID <mailman.836.1527722444.1292.bug-bash@gnu.org> (permalink)
References <CAM4RgKgOACdJtjBFpKMhfoXvbSwun=84pPdADNaJVKcd-Z5GFg@mail.gmail.com> <6e150f52-99b9-bea6-9548-2f9910772e42@iki.fi>

Show all headers | View raw



Ilkka Virta wrote:
> On 22.5. 00:17, Uriel wrote:
>   
>> As you know, a conditional is of the type:
>> if [[ EXPRESSION ]]; then TRUE CONDITION; else ALTERNATIVE RESULT; fi
>>
>> Or with logical operators and groups:
>> [[ EXPRESSION ]] && { TRUE CONDITION; } || { ALTERNATIVE RESULT; }
>>     
>
> No, those are not the same. In the latter, the `||` operator checks the 
> last exit status. That may be the one from the "expression" part, or the 
> one from the "true condition" part, depending on which one was the 
> previous to run.
>
> So, the `condition && if_true || if_false` shorthand only works as an 
> if-then-else as long as `if_true` never fails.
---
That doesn't seem to be what bash does:

for cond in "0 0" "0 1" "1 0" "1 1" ; do
x=${cond% *} y=${cond#* }
[[ 1 == $x ]] && { [[ 1 == $y ]]; } || { echo "for (x=$x, y=$y) doAlt" ; 
}   
done
for (x=0, y=0) doAlt
for (x=0, y=1) doAlt
for (x=1, y=0) doAlt

I.e. doAlt is done if either or both of the expressions
around the '&&' are not true.

If both conditions around '&&' are true, the 'or'(then) clause isn't done.

If the 1st expression is false, it would skip directly to
the '||' and would execute the 3rd clause.
If the 1st expresion is true then it does the 2nd clause.
If that is false, the '||' clause is done, but if true,
the '||' clause would not be done.



Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Conditions with logical operators and nested groups execute "if" and "else" L A Walsh <bash@tlinx.org> - 2018-05-30 16:20 -0700

csiph-web