Subject: Re: sh arithmetic expression
To: Florian Stoehr <netbsd@wolfnode.de>
From: Simon Burge <simonb@wasabisystems.com>
List: netbsd-users
Date: 12/29/2004 13:20:24
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

Remove the "let".  With just

	i=$((i+1))

it works fine:

	thoreau 7> cat foo.sh
	#!/bin/sh
	i=1
	while [ $i -le $1 ]
	do
		echo "Run " $i
		i=$(($i+1))
	done
	thoreau 8> sh foo.sh 5
	Run  1
	Run  2
	Run  3
	Run  4
	Run  5
	thoreau 9> bash foo.sh 5
	Run  1
	Run  2
	Run  3
	Run  4
	Run  5

Simon.
--
Simon Burge                            <simonb@wasabisystems.com>
NetBSD Support and Service:         http://www.wasabisystems.com/