英文:
Extracting values from arbitrary/named associative arrays and keys within them
问题
我使用 declare -A
来添加字典中的键值对。
APP_LIST=('app1' 'app2')
declare -A app1=([column_a]="111" [column_b]="222")
declare -A app2=([column_a]="333" [column_b]="444")
我尝试使用循环来打印从 app1 和 app2 中的 column_b
的值,但似乎不起作用。
for i in ${APP_LIST[*]}; do
echo ${${i}[column_b]}
done
错误消息:
${${i}[column_b]}: bad substitution
请帮忙,谢谢!
我期望使用循环来打印 app1 和 app2 中的 column_b
的值。
英文:
I use declare -A
to add key-value pairs in dictionary.
APP_LIST=('app1' 'app2')
declare -A app1=([column_a]="111" [column_b]="222")
declare -A app2=([column_a]="333" [column_b]="444")
I have tried to print the value of the column_b from app1 and app2 using for-loop, but it doesn't seem to work.
for i in ${APP_LIST[*]}; do
echo ${${i}[column_b]}
done
The error msg:
${${i}[column_b]}: bad substitution
Please help, thanks!
I expected to print the value of the column_b
from app1 and app2 using for-loop.
答案1
得分: 0
以下是您要翻译的内容:
$ cat tst.sh
#!/usr/bin/env bash
app_list=('app1' 'app2')
declare -A app1=([column_a]="111" [column_b]="222")
declare -A app2=([column_a]="333" [column_b]="444")
declare -n arr_name
for arr_name in "${app_list[@]}"; do
echo "${arr_name[column_b]}"
done
$ ./tst.sh
222
444
英文:
$ cat tst.sh
#!/usr/bin/env bash
app_list=('app1' 'app2')
declare -A app1=([column_a]="111" [column_b]="222")
declare -A app2=([column_a]="333" [column_b]="444")
declare -n arr_name
for arr_name in "${app_list[@]}"; do
echo "${arr_name[column_b]}"
done
<p>
$ ./tst.sh
222
444
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论