【发布时间】:2018-06-06 16:41:01
【问题描述】:
我想在 bash 中实现以下伪代码
function gen_items() {
dict=$1 # $1 is a name of a dictionary declared globally
for key in $dict[@]
do
echo $key ${dict[$key]}
# process the key and its value in the dictionary
done
}
我得到的最好的是
function gen_items() {
dict=$1
tmp="${dict}[@]"
for key in "${!tmp}"
do
echo $key
done
}
这实际上只从字典中获取值,但我也需要键。
【问题讨论】:
-
没有namerefs,间接变量构造是你能做的最好的。