Zsh 名称为变量的关联数组

Zsh 名称为变量的关联数组,zsh,Zsh,我有以下设置: typeset -A network network[interface]=eth0,eth1 typeset -A eth0 eth0[dhcp]=yes ... typeset -A eth1 eth1[dhcp]=yes ... for InterfaceToCreate in $(echo ${network[interface]/,/ }) ; do (some stuff) case ${InterfaceToCreate[dhcp]} in (some stuf

我有以下设置:

typeset -A network
network[interface]=eth0,eth1

typeset -A eth0
eth0[dhcp]=yes
...
typeset -A eth1
eth1[dhcp]=yes
...
for InterfaceToCreate in $(echo ${network[interface]/,/ }) ; do
(some stuff)
case ${InterfaceToCreate[dhcp]} in
(some stuff)
我想为网络[interface]的每个值获取dhcp值,我有以下设置:

typeset -A network
network[interface]=eth0,eth1

typeset -A eth0
eth0[dhcp]=yes
...
typeset -A eth1
eth1[dhcp]=yes
...
for InterfaceToCreate in $(echo ${network[interface]/,/ }) ; do
(some stuff)
case ${InterfaceToCreate[dhcp]} in
(some stuff)
如果我试着用它,它就不正常了

${!InterfaceToCreate[dhcp]}
\${${InterfaceToCreate}[dhcp]}

我甚至尝试了eval以获得相同的结果。

默认情况下,参数值不会被解释为进一步的参数名称。所以
${${foo}}
的行为就像
${foo}
(请参阅)。使用
P
可以更改此行为。例如,
${(P)${foo}}
将计算
${foo}
,并将其值用作参数替换的名称

因此,您可以实现如下所示的预期效果:

排版-网络eth0 eth1
网络[接口]=eth0,eth1
eth0[dhcp]=是
eth1[dhcp]=否
对于InterfaceToCreate在${(s:,:)网络[interface]};做
中的案例${${(P)InterfaceToCreate}[dhcp]}
是的)
打印$InterfaceToCreate DHCP
;;
*)  
打印$InterfaceToCreate无DHCP
;;
以撒
完成
这应该表明

eth0 DHCP
eth1 no DHCP

我还建议使用
s:string:
来拆分逗号分隔的列表,而不是使用
echo
和命令替换的迂回方式。

感谢«s:string:»!但是${(P)${foo}}将不起作用。。。这里是一个测试用例:
typeset-a eth0
eth0[dhcp]=yes
eth1[dhcp]=yes
`
InterfaceToCreate=eth0
`
echo${{(P)InterfaceToCreate}[dhcp]}
空白输出
这项工作如预期的那样:${(Pkv)InterfaceToCreate},返回键/值,仅返回[key]如果secifiedI只是再次测试它就不起作用了-我甚至复制粘贴了你评论中的行-而且它对我来说很好,即使使用默认设置(
zsh-f
)。我在手册页中也找不到任何可以表明此行为以任何方式可配置的内容。。。我将尝试找出导致这种奇怪行为的选项。。。无论如何,谢谢。只要调用
setopt
就会显示与默认设置不同的所有选项。在zsh的5.0版本中,无法工作。。。改用5.3,这很好
网络
数组中还有其他键吗?为什么不使用常规索引数组
interfaces=(eth0 eth1)
?网络中还有其他键(
dns1
dns2
代理
,…),所以我需要使用关联数组,因为索引的顺序可能会改变。