On Sat, 17 Dec 2011 11:47:51 +0000, Stephane CHAZELAS wrote:
> 2011-12-17, 08:54(+00), James Waldby:
>> [...scripts] run ok on Fedora Linux systems, where /bin/sh is linked
>> to /bin/bash, but not on Ubuntu, where /bin/sh is linked to /bin/dash.
>> Under dash, export -f fname gives "Illegal option -f" . [...]
>> Dash man page says "unset [-fv] name ..." will unset and unexport
>> functions and variables, as -f or -v is specified [...]
>>
>> However, I don't see anything in the man page about how to export a
>> function. I'd like to use a method that allow scripts that start with
>> #!/bin/sh to run with /bin/sh linked to either of /bin/bash or
>> /bin/dash. Any ideas?
...
> If you want your script to be run by any POSIX sh, stick to the POSIX
> syntax for your script. You can find the details at
>
http://pubs.opengroup.org/onlinepubs/9699919799/
>
> unset -f is to unset a function, not to unexport it, that's probably a
> typo in dash man page. POSIX functions can't be exported. If you want
> functions to be available to more than one script, you can write their
> definition in a file and have that file sourced by those scripts, but
> again if you need to go that far, chances are that you'd be better of
> using a programming language.
On this new ubuntu system, for the moment I've changed #!/bin/sh in a
handful of scripts to #!/bin/bash. For these scripts, I control them
and the scripts they start, so could modify them to source a file;
but in other cases won't have control.
It appears that for simple functions with single-level quoting,
export + eval is lighter-weight than writing a file and sourcing it.
For example, in dash:
tini > export g='f() { t=$1; printf "%.0f squared is %.0f\n" $t $((t*t))
> }'
tini > echo "$g"
f() { t=$1; printf "%.0f squared is %.0f
" $t $((t*t))
}
tini > dash
tini > eval "$g"
tini > f 7
7 squared is 49
--
jiw