在shell脚本中创建动态关联数组时出现的问题

ru9i0ody  于 2022-12-19  发布在  Shell
关注(0)|答案(1)|浏览(124)

我在shell脚本中数组内创建字典时遇到问题
例如,对于某些命令,我有两列

record       zone                     

test07      cname.lan                
test07-ext  ca.us               
test07-t    ca.us               
test07      ca.net              
test-t      ca.net    

so i want to have  ass_array=([[test07]="cname.lan" [test07-ext]="ca.us" [test07-t]="ca.us".....

我用这个代码,但我,但它不工作.

rec_array=($(somecommand))  ## check and it has correct value
zone_array=($(somecommand))  ##check and it has correct value
declare -A ass_array

for(( i=0;i<= "${#rec_array[@]}";i+=1  ));

     do
             key="${rec_array[$i]}"
             val="${zone_array[$i]}"

             ass_array["$key"]+="$val"
done
fhg3lkii

fhg3lkii1#

在for循环的退出条件中有一个off-by-one错误;你正在迭代数组的末尾。

相关问题