[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Shell case statements
From: |
Eric Blake |
Subject: |
Re: Shell case statements |
Date: |
Fri, 20 May 2011 10:10:52 -0600 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110428 Fedora/3.1.10-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.10 |
On 05/20/2011 09:33 AM, Chet Ramey wrote:
>> Well, that's rather all over the place, but yes, it does seem like bash
>> was the buggiest of the lot, compared to other shells. Interactively, I
>> tested:
>>
>> readonly x=1
>> case 1 in $((x++)) ) echo hi1 ;; *) echo hi2; esac
>> echo $x.$?
>>
>> bash 4.1 printed:
>> bash: x: readonly variable
>> hi1
>> 1.0
>> which means it matched '1' to $((x++)) before reporting the failure
>> assign to x, and the case statement succeeded. Changing the first "1"
>> to any other string printed hi2 (the * case).
>
> Thanks for the report. This was an easy fix. The variable assignment
> error was actually handled correctly, the expression evaluation code
> just didn't pay enough attention to the result.
How about the even simpler:
$ bash -c 'readonly x=5; echo $((x=5))'; echo $?
bash: x: readonly variable
5
0
$
Other shells abort rather than running echo:
$ ksh -c 'readonly x=5; echo $((x=5))'; echo $?
ksh: line 1: x: is read only
1
$ zsh -c 'readonly x=5; echo $((x=5))'; echo $?
zsh:1: read-only variable: x
1
$ dash -c 'readonly x=5; echo $((x=5))'; echo $?
dash: x: is read only
2
$
--
Eric Blake eblake@redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature