On 8/1/11 8:47 PM, Chet Ramey wrote: > On 8/1/11 4:41 AM, dnade....@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 c...@case.edu http://cnswww.cns.cwru.edu/~chet/
*** ../bash-4.2-patched/expr.c 2010-12-21 11:12:13.000000000 -0500 --- expr.c 2011-08-02 20:58:28.000000000 -0400 *************** *** 477,480 **** --- 481,492 ---- if (special) { + if ((op == DIV || op == MOD) && value == 0) + { + if (noeval == 0) + evalerror (_("division by 0")); + else + value = 1; + } + switch (op) { *************** *** 483,493 **** break; case DIV: - if (value == 0) - evalerror (_("division by 0")); lvalue /= value; break; case MOD: - if (value == 0) - evalerror (_("division by 0")); lvalue %= value; break; --- 495,501 ---- *************** *** 805,809 **** if (((op == DIV) || (op == MOD)) && (val2 == 0)) ! evalerror (_("division by 0")); if (op == MUL) --- 813,822 ---- if (((op == DIV) || (op == MOD)) && (val2 == 0)) ! { ! if (noeval == 0) ! evalerror (_("division by 0")); ! else ! val2 = 1; ! } if (op == MUL)