在这个非常简单的示例中,我需要解决数组元素的键和值:
declare -A writer writer[H.P.]=Lovecraft writer[Stephen]=King writer[Clive]=Barker writer[Jack]=Ketchum for i in ${!writer[@]} do echo "$i ${writer[$i]}" done fullname() { pointer=$1[@] for i in "${!pointer}" do echo "? $i" done } fullname writer
该函数必须以与之前的示例循环相同的格式显示输出,并且它应该接收数组名称,键列表或值,所有这些都是我尝试过的,但没有成功.任何建议都非常感谢.
indir_keys() { eval "echo \${!$1[@]}" } indir_val() { eval "echo \${$1[$2]}" } fullname() { pointer=$1 for i in $(indir_keys $pointer) do echo "$i $(indir_val $pointer $i)" done }
得到:
Jack Ketchum Clive Barker Stephen King H.P. Lovecraft