> > 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).
_______________________________________________________
Linux-HA-Dev: [email protected]
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/