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


Groups > gnu.bash.bug > #14180 > unrolled thread

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

Started byL A Walsh <bash@tlinx.org>
First post2018-05-30 16:20 -0700
Last post2018-05-30 16:20 -0700
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

FromL A Walsh <bash@tlinx.org>
Date2018-05-30 16:20 -0700
SubjectRe: Conditions with logical operators and nested groups execute "if" and "else"
Message-ID<mailman.836.1527722444.1292.bug-bash@gnu.org>

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.



[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web