1
如何获得bash中关联数组的最后一个键?在这个例子中,我需要在$ last变量中有“lot”。我在这里找到了一种方法:How to get the keys and values of an associative array indirectly in Bash?。但正如预期在下面的例子不工作,返回此错误:如何获得关联数组(字典)的最后一个键
./test.sh: line 9: keys2: ${!$addict[@]}: must use subscript when assigning associative array
以下是本test.sh的内容:
declare -A addict=(
["foo"]="bar"
["few"]="baz"
["lot"]="pot"
)
index_last=$((${#addict[@]} - 1))
eval 'declare -A keys2=(${!$addict[@]})'
last="${keys2[$index_last]}"
echo "$keys2"
echo "$index_last"
echo "$last"
键是无序的,所以“最后一个键”的概念没有意义。相关:http://stackoverflow.com/q/29161323/2088135 –
你为什么使用eval? – 123