Subject: Re: sh arithmetic expression
To: Jason M. Leonard <fuzz@ldc.upenn.edu>
From: Florian Stoehr <netbsd@wolfnode.de>
List: netbsd-users
Date: 12/29/2004 03:28:27
On Tue, 28 Dec 2004, Jason M. Leonard wrote:
>
>
> On Wed, 29 Dec 2004, Florian Stoehr wrote:
>
>> On Tue, 28 Dec 2004, Jason M. Leonard wrote:
>>
>>>
>>>
>>> On Wed, 29 Dec 2004, Florian Stoehr wrote:
>>>
>>>> Hi,
>>>>
>>>> the follwing will run with bash, but not with /bin/sh. How to write the
>>>> increment statement so that /bin/sh will like it? I played around with
>>>> "let" but without success.
>>>>
>>>> #!/bin/sh
>>>> i=1
>>>> while [ $i -le $1 ]
>>>> do
>>>> echo "Run " $i
>>>> i=$[$i+1] <--- bash can do that, not /bin/sh
>>>> done
>>>
>>> let i=$((i+1))
>>>
>>>
>>
>> Hm ... as I said, I tried that with let and I did it as in sh(1) with
>> $((expr)), but it doesn't seem to work:
>>
>> #!/bin/sh
>> i=1
>> while [ $i -le $1 ]
>> do
>> echo "Run " $i
>> let i=$((i+1))
>> done
>>
>>
>> Gives:
>>
>> flo@irina [/tmp] # ./test.sh 5
>> Run 1
>> ./test.sh: arith: syntax error: "i+1"
>>
>> ???
>>
>> -Flo
>
> Something hates you? The same code, copy and pasted from your email:
>
> lorax:~ fuzz$ ./test.sh 5
> Run 1
> 2
> Run 2
> 3
> Run 3
> 4
> Run 4
> 5
> Run 5
> 6
>
>
> :Fuzz
>
Hehe - maybe :)
It works with Simon's "i=$(($1+1))". It does not with the script I
posted. Perhaps you symlinked some other shell to /bin/sh ???
-Florian