zsh - associative array wich name is a variable -
i have setup :
typeset -a network network[interface]=eth0,eth1 typeset -a eth0 eth0[dhcp]=yes ... typeset -a eth1 eth1[dhcp]=yes ...
i want dhcp value each value of network[interface], have setup :
for interfacetocreate in $(echo ${network[interface]/,/ }) ; (some stuff) case ${interfacetocreate[dhcp]} in (some stuff)
it's don't work normal if try with
${!interfacetocreate[dhcp]} \${${interfacetocreate}[dhcp]}
i tried eval same result.
by default values of parameters not interpreted further parameter names. ${${foo}}
behaves ${foo}
(see nested substitutions). behavior can changed parameter expansion flag p
. example ${(p)${foo}}
evaluate ${foo}
, use value name parameter substitution.
so can achieve desired effect this:
typeset -a network eth0 eth1 network[interface]=eth0,eth1 eth0[dhcp]=yes eth1[dhcp]=no interfacetocreate in ${(s:,:)network[interface]} ; case ${${(p)interfacetocreate}[dhcp]} in yes) print $interfacetocreate dhcp ;; *) print $interfacetocreate no dhcp ;; esac done
this should show
eth0 dhcp eth1 no dhcp
i suggest using parameter expansion flag s:string:
split comma separated list instead of going roundabout way echo
, command substitutions.
Comments
Post a Comment