bug-bash
[Top][All Lists]
Advanced

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

Re: cond?true:false and division by 0


From: Chet Ramey
Subject: Re: cond?true:false and division by 0
Date: Tue, 02 Aug 2011 21:13:42 -0400
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.18) Gecko/20110616 Lightning/1.0b2 Thunderbird/3.1.11

On 8/1/11 8:47 PM, Chet Ramey wrote:
> On 8/1/11 4:41 AM, dnade.ext@orange-ftgroup.com wrote:
>>      Hello
>>
>> I wanted to check a variable before attempting to divide something by it, so 
>> I wrote :
>>
>> echo $(( foo==0?0:something/foo ))
>>
>> And bash 2, 3 and up to 4.2.10 version sent me a "division by 0" error.
>>
>> So, I've tested a static version of the statement :
>>
>> $ echo $(( 0==0?0:1/0 ))
>> -bash: 0==0?0:1/0 : division by 0 (error token is " ")
>>
>> It looks like the "division by 0" happens whatever the condition result.
>> Shouldn't it happen only if condition result make the computing go that far 
>> (like, FWIW, in zsh) ?
> 
> Hmm...this looks like a precedence problem.  I'll take a look.

It's not actually a precedence problem.  The check for division by 0
shouldn't take place when evaluation is suppressed.  (The bash
arithmetic evaluator, like most others of its type, is parse-and-
evaluate-on-the-fly, so all operands are evaluated.

Please try the attached patch and see if it works for you.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/

Attachment: expr-noeval.patch
Description: Text Data


reply via email to

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