NetBSD-Bugs archive

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

Re: bin/50960: /bin/sh generates incorrect value for && and || inside $(( ))



The following reply was made to PR bin/50960; it has been noted by GNATS.

From: Robert Elz <kre%munnari.OZ.AU@localhost>
To: Paul_Koning%Dell.com@localhost
Cc: gnats-bugs%NetBSD.org@localhost, netbsd-bugs%netbsd.org@localhost
Subject: Re: bin/50960: /bin/sh generates incorrect value for && and || inside $(( ))
Date: Sun, 13 Mar 2016 05:10:47 +0700

     Date:        Sat, 12 Mar 2016 21:34:59 +0000
     From:        <Paul_Koning%Dell.com@localhost>
     Message-ID:  <7E5297F8-347F-4850-8AB2-4C087F6CEE8A%dell.com@localhost>
 
   | Actually, 0 for true and non-zero for false,
 
 It actually isn't, not for expressions in $(( ))
 
 You're thinking of the && and || command line operators, and exit
 statuses from commands, where non-zero is false and 0 is true.
 
 At that level, the kind of evaluation that $(( )) is doing
 is correct, that is, if you were (for some obscure reason) to do
 
 	(exit 3) || (exit 4)
 
 The result would (correctly) be "4" (that's what $? would be set to),
 which is "false" if the value is ever used that way.
 
 Generating 0 or 1 (whichever is to be true and false) in that situation
 would be wrong.
 
 It is currently very unlikely that anyone is actually currently
 using || or && in arithmetic expressions, as there is no way to use
 the value in any sensible way.   That is, there is (currently) no
 testing operator (see PR bin/50958)
 
 If one were to attempt
 	$(( x + ( y && z ) ))
 then in C (which is the model for these expressions) you'd get (the value
 of) x, or x+1
 
 But in shell (currently) you get x or x + z (never x + y) which isn't
 a very useful outcome, I don't think.
 
 In any case, in these expressions it is definitely 0 for false and != 0 for
 true (on input, when generated, the value is 1).
 
 And that is what $(( 6 > 4 )) gives (1) (or $(( 6 < 4 )) --> 0 ) and all
 the similar relational operators as well.
 
 kre
 


Home | Main Index | Thread Index | Old Index