Subject: Re: sh arithmetic expression
To: Florian Stoehr <netbsd@wolfnode.de>
From: Greg A. Woods <woods@planix.com>
List: netbsd-users
Date: 12/29/2004 19:37:52
[ On Wednesday, December 29, 2004 at 03:28:27 (+0100), Florian Stoehr wrote: ]
> Subject: Re: sh arithmetic expression
>
> Hehe - maybe :)
>
> It works with Simon's "i=$(($1+1))". It does not with the script I
> posted.
Indeed.
Note the difference between "$(($i+1))" and "$((i+1))". (I assume "$1"
in the above was a typo for "$i")
The Korn Shell allows the latter, while POSIX (aka SuSv2 and beyond)
requires the former.
I make the same mistake all the time (especially when typing such
expressions on the command line!) and it's _really_ annoying because
I've somehow become very accustomed to the KSH "extension" of not
requiring the '$' for parameter expansion. It's especially annoying
when I've made the mistake of testing my ideas with a KSH command line
and then incorporated them into a /bin/sh script that subsequently
fails for reasons my eyes can never spot. :-)
I'm also very accustomed to using the ++ and -- operators, but SuSv2
sadly does not require that they be implemented, and NetBSD does not
implement them either, at least not as of 1.6.x:
$ i=1
$ j=3
$ while [ $i -le $j ];do echo $i; : $(($i++)); done
1
arithmetic expression: syntax error: "1++"
$
Maybe someday I'll get around to fixing this!
--
Greg A. Woods
Planix, Inc.
<woods@planix.com> +1 416 489-5852 x122 http://www.planix.com/