Shell 使用地图或字典查找值

Shell 使用地图或字典查找值,shell,Shell,这是我的情况。我有一个设备id数组。我知道哪个设备id与哪个ip地址匹配。我想使用设备id查找正确的ip地址,并在命令中传递这两个地址。我正在使用一个shell脚本来实现这一点 这里有一些我想要的psuedo代码 这是我的字典类型的东西,匹配id和ip,没有特别的顺序 dictionary_thing = ( id:ip id:ip id:ip id:ip )

这是我的情况。我有一个设备id数组。我知道哪个设备id与哪个ip地址匹配。我想使用设备id查找正确的ip地址,并在命令中传递这两个地址。我正在使用一个shell脚本来实现这一点

这里有一些我想要的psuedo代码

这是我的字典类型的东西,匹配id和ip,没有特别的顺序

 dictionary_thing = ( id:ip
                      id:ip
                      id:ip
                      id:ip )
 array_of_used_ids = ( id
                       id
                       id )
这是我的id数组,我没有按特定顺序使用它

 dictionary_thing = ( id:ip
                      id:ip
                      id:ip
                      id:ip )
 array_of_used_ids = ( id
                       id
                       id )
可能为每个找到的id创建一个具有两个属性(id和ip)的对象数组

 array_object
 for(int i=0; i<count; i++)
 {
      array_object[i]= new object and set id to dictionary_thing id and ip
 }
array\u对象

对于(inti=0;i您已经构建了主数组,因此可以从使用的ID构建一个正则表达式

regex="^${array_of_used_ids[0]}:"
i=1
while [ $i -lt ${#array_of_used_ids[*]} ]; do
 regex="$regex|^${array_of_used_ids[$i]}:"
 i=$(( $i + 1))
done
这将为您提供一个类似“^id:^id:^id:^id:”的正则表达式: 然后迭代字典,检查每个正则表达式,当匹配时,用空格替换分隔的“:”

array_object=()
i=0
j=0
while [ $i -lt ${#dictionary_thing[*]} ]; do
 if [[ $dictionary_thing[$i] =~ $regex ]]; then 
   array_object[$j] = ${dictionary_thing[$i]/:/ } #if it is possible for an ID to contain a ':', you will need a different method for splitting here
   j=$(( $j + 1 ))
 fi
 i=$(( $i + 1 ))
done
最后,迭代结果对象并执行命令

i=0
while [ $i -lt ${#array_object[*]} ]; do
 command_to_execute ${array_object[$i]}
 i=$(( $i + 1 ))
done

什么操作系统和shell风格?Mac和3.2.48(1)-发布。这就是你对flavor的意思吗?shell flavor我指的是
bash
ksh
tcsh
etcbash版本4有关联数组,这会简化事情。有没有升级的可能性?事后看来,我想你可以在第二个循环中执行commmand,这样就不需要数组了_对象和第三个循环。