On Tue, Aug 12, 2008 at 03:52:45AM -0700, Joe Bill wrote:
> > > The "local" keyword is I think in the POSIX standard. Is there a
> > > platform with shell which doesn't comply with the POSIX at least?
>
> In posix sh, variables scope is always global.
>
> > Sadly, yes. For instance I have just tested 'sh' on Solaris 8, 9 and 10
> > (Sun's currently supported releases): 'local' fails on all.
>
> 'local' is bash specific. There is an equivalent in ksh known as 'typeset',
> depending on what you specified as your default shell, the one that executes
> shabadabanged (#!/bin/sh) scripts:
>
> - the old SysVr4 bourne defined as /usr/bin/sh,
> - or the ksh88 or ksh93, either defined as /usr/xpg4/bin/sh
>
> The bash like behavior in ksh88 where you would define a local variable in a
> function like this:
>
> fn_name () {
> typeset varname
> }
>
> was reverted in ksh93 to posix behavior, generates an error.
>
> To use variable scope in ksh93, you must define your functions like this:
>
> function fn_name () {
> typeset varname
> }
>
>
> Thoroughly explained here:
>
> usr/src/lib/libshell/common/COMPATIBILITY says about this issue:
> -- snip --
> Functions, defined with name() with ksh-93 are compatible with
> the POSIX standard, not with ksh-88. No local variables are
> permitted, and there is no separate scope. Functions defined
> with the function name syntax, maintain compatibility.
> This also affects function traces.
> -- snip --
> (this issue also affects /usr/xpg4/bin/sh in Solaris because it is based
> on ksh88).
>
> Or short: If you use function-local variables and "foo()" instead of
> "function foo" you'll hit the problem that your variables are no longer
> be "local" (and please don't complain... that's defined by the POSIX
> shell spec and one of the rare items where even the bash people fully
> agree that ksh93 is completely correct).
Hi Joe,
I'm not sure if this is the correct specification, but I've just taken a
quick look over the "Shell & Utilities" volume of "IEEE Std 1003.1, 2004
Edition".
http://www.opengroup.org/austin/papers/posix_faq.html
-> http://www.unix.org/single_unix_specification/
It seems to indicate that the for a function is:
fname() compound-command[io-redirect ...]
It does not seem to make any mention of the function keyword,
which incidentally is not supported by dash, the /bin/sh de jour of
Debian and Ubuntu.
As for local and typedef, neither seem to be mentioned in the
specification - though I'm prepared to accept that I need to look
harder.
If we assume for a moment that they aren't part of the specification,
its probably best just to remove local from our code. I think it is
already used quite sparingly and likely isn't necessary. (Though
personally I feel nauseous about all variables being scoped globally.)
_______________________________________________________
Linux-HA-Dev: [email protected]
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/