On Fri, 2007-08-24 at 00:33 +0200, Natanael Copa wrote:
> I mentioned to the vserver list that i was interested to convert the
> scripts to POSIX. First he said that he was not against it until he
> realized he could no longer use arrays.
>
> http://www.paul.sladen.org/vserver/archives/200708/0025.html
>
> Does anyone have any suggestion to do dynamic variables cleanly without
> using arrays?
Well, you can still use arrays provided that /bin/sh is still bash. Of course,
this does mean Linux only systems and also rules out embedded.
net.lo ships with this function
# Credit to David Leverton for this function which handily maps a bash
# array structure to positional parameters so existing configs work :)
# We'll deprecate arrays at some point though.
_get_array() {
if [ -n "${BASH}" ] ; then
case "$(declare -p "$1" 2>/dev/null)" in
"declare -a "*)
echo "set -- \"[EMAIL PROTECTED]""
return
;;
esac
fi
echo "eval set -- \"\$$1\""
}
Which means you can then do this
eval "$(_get_array "config_${IFVAR}")"
With IFVAR as eth0 that will convert the bash array config_eth0 to positional
parameters
Here's how both can now be defined. The former is bash only, the latter is all
shells.
config_eth0=( "1.2.3.4/24" "5.6.7.8 netmask 255.255.255.0" )
config_eth0="'1.2.3.4/24' '5.6.7.8 netmask 255.255.255.0'"
If you pay attention to the quoting, you'll have no issues.
Thanks
Roy
--
[EMAIL PROTECTED] mailing list