[Top][All Lists]

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

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

From: L A Walsh
Subject: Re: Conditions with logical operators and nested groups execute "if" and "else"
Date: Wed, 30 May 2018 17:02:58 -0700
User-agent: Thunderbird


You didn't say what you expected the behavior to be in your
bottom conditional, but remember, you start with
you lop off the 1st -e (did you mean to use ## instead of #?)
so last statement of 1st indent was
! "-e-e--debug =~ ^-  so the regex matches (true) and then you
say 'not', meaning it is false.  The top indent exits with
a false condition, so the bottom indent would be executed.

Is that what you were expecting?

Greg Wooledge wrote:
On Mon, May 21, 2018 at 04:17:18PM -0500, Uriel wrote:
pf22 is wrong.
It says:

true && ((i++)) || ((i--))  # WRONG!
echo "$i" # Prints 0 # What happened here? It looks like i should be 1, but it ends up 0.
It *should* be 0.
true executes ((i++)) so i becomes 1 AFTER it's value is tested.
((0)) evaluates as 'false'.

You should change the above to "never use post- increment or decrement
if pre- increment or decrement will do.

You meant to increment 'i' so the expression would have been ((1))
(which is true), so:
  > true && ((++i)) || ((i--))
  > echo $i
evaluates to 1 as you expected.

Another example:

/bin/false && /bin/false || echo foo
 /bin/true  && /bin/false || echo foo
/bin/false && /bin/true || echo foo
 /bin/true  && /bin/true  || echo foo
 #( nothing echoed)

Also, FWIW, switching the logical ops in the for test works
as expected:

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=1) doAlt
for (x=1, y=0) doAlt
for (x=1, y=1) doAlt

Only in the case when both of the 1st two clauses are
false is the last clause NOT done, which is as one would expect.
I.e. precedence rules seem to work as expected.

reply via email to

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