创建具有关联数组的列表

编程入门 行业动态 更新时间:2024-10-24 10:21:07
本文介绍了创建具有关联数组的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

创建具有关联数组的列表

本文发布于:2023-10-28 08:32:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!