我尝试创建一个带有几个关联数组的列表,以简化脚本并创建新的未来数组。
问题是,python
我只想在list({}, {})
中创建一个bash
,我目前甚至无法创建指向字典的列表。
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
它只是给我一个bad substitution
错误。我知道嵌套数组在bash
中是不可能的,但指向一个数组也是不可能的?
答案 0 :(得分:3)
您必须使用间接参数扩展。
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