[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question about arithmetic logic.
From: |
Steven W. Orr |
Subject: |
Re: Question about arithmetic logic. |
Date: |
Mon, 18 Apr 2011 10:57:00 -0400 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 |
On 4/18/2011 10:40 AM, Greg Wooledge wrote:
> On Mon, Apr 18, 2011 at 10:30:35AM -0400, Steven W. Orr wrote:
>> ss=1
>> (( ss ))
>> echo $? # Also says 1. Should this be 0 because it should be
>> the
>> # success result same as (( ss != 0 ))
>
> That's not what I get:
>
> imadev:~$ unset ss; ss=0; ((ss)); echo $?
> 1
> imadev:~$ unset ss; ss=1; ((ss)); echo $?
> 0
>
> I use flag variables like this all the time. The only difference is
> that I do not use "typeset -i" on them.
I get the same result as you when I do it your way. But if I add the typeset,
I still get the same result:
515 > unset ss; typeset -i ss=1; ((ss)); echo $?
0
516 > unset ss; typeset -i ss=0; ((ss)); echo $?
1
Interesting. So I redid my problem using unset:
521 > unset ss
522 > typeset -i ss=0
523 >
523 > (( ss ))
524 > echo $?
1
525 > unset ss
526 > typeset -i ss=1
527 > (( ss ))
528 > echo $?
0
Why did this make a difference? What did the unset do?
It gets worse:
529 > unset ss
530 > typeset -i ss # No initial value!
531 > ss=0
532 > (( ss ))
533 > echo $?
1
534 > ss=1
535 > (( ss ))
536 > echo $?
0
It printed the correct values!
Now let's try this differently. This time I initialize ss using a real integer
value instead of hoping that the string gets converted to an integer
537 > unset ss
538 > typeset -i ss=$(( 0 ))
539 > (( ss ))
540 > echo $?
1
541 > unset ss
542 > typeset -i ss=$(( 1 ))
543 > (( ss ))
544 > echo $?
0
# One last time but without using unset
545 > typeset -i ss=$(( 0 ))
546 > (( ss ))
547 > echo $?
1
*548 > typeset -i ss=$(( 1 ))
549 > (( ss ))
550 > echo $?
0
Does this mean that I have to go and check *all* of my code to ensure that
integer initializations are being done using integer arithmetic expressions?
I'd say this is a big deal. No?
--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net