【发布时间】:2011-02-06 14:33:20
【问题描述】:
在 Bash 中,我可以使用这种通用结构创建一个 map(哈希表)
hput() {
eval "$1""$2"='$3'
}
hget() {
eval echo '${'"$1$2"'#hash}'
}
然后像这样使用它:
hput capitals France Paris
hput capitals Spain Madrid
echo "$(hget capitals France)"
但是我如何最好地迭代地图中的条目?例如,在 Java 中我会这样做:
for (Map.Entry<String, String> entry : capitals.entrySet()) {
System.out.println("Country " + entry.getKey() + " capital " + entry.getValue());
}
在 Bash 中是否有一种通用的方法来完成类似的事情?
【问题讨论】:
标签: bash map hashtable iteration