创建具有关联数组的列表 [英] Creating a List With Associative Arrays

查看:99
本文介绍了创建具有关联数组的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有几个关联数组的列表,以简化脚本并创建新的未来数组。

I'm trying to create a list with a couple of associative arrays, for script simplicity and creation of new future arrays.

问题是,在 python 中,我只需在<$中创建一个 list({},{}) c $ c> bash 我目前甚至无法创建指向字典的列表。

The thing is, where in python I would just create a list({}, {}), in bash I currently can't even create a list pointing to the dictionary.

programs=(foo bar)

for program in "${programs[@]}"; do # Declare all arrays in programs
    declare -A $program
done

foo[var]=`ls -al ~` # Declare some variables in each array
bar[var]=`ls -al /`

for program in "${programs[@]}"; do # Print each var key in each array
    if [ "${${program}[var]+True}" = True ]; then
        echo ${${program}[var]}
    fi
done

并且一直给我一个不好的替换错误。我知道在 bash 中嵌套数组是不可能的,但是指向数组也是不可能的?

And it just keeps giving me an bad substitution error. I'm aware that nested arrays is impossible in bash, but is pointing to an array also impossible ?

推荐答案

您必须使用间接参数扩展。

You have to use indirect parameter expansion.

for program in "${program[@]}"; do
  t=$program[var]   # This is a literal string, not an array expansion
  echo "${!t}"      # This is an indirect array expansion
done

这篇关于创建具有关联数组的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆