arrays - 遍历 bash 中的数组列表

标签 arrays bash associative-array

我需要在 bash 中迭代几个键值数组(关联数组)。这是我最后一次尝试:

declare -A ARR1
ARR1[foo]=bar

declare -A ARR2
ARR2[foo]=baz

arrays=(ARR1 ARR2)

for idx in "${arrays[@]}"; do
    echo ${${idx}[foo]};
done 

这当然是错误的(语法错误),但此时此刻,我没有其他想法如何处理它。而在下面的示例中没有错误,但输出只是一个数组的名称。

for idx in "${array[@]}"; do
    echo "${idx[foo]}";
done

------- OUTPUT -------
ARR1
ARR2

编辑

好的,我可以使用 eval 来完成:

eval echo \${${idx}[foo]};

但是,我读到在 bash 脚本中使用 eval 并不是一个好主意。有没有更好的办法?

最佳答案

Bash 4.3-alpha 引入了 nameref attribute ,在这种情况下可以使用:

declare -A arr1=([foo]=bar)
declare -A arr2=([foo]=baz)
arrays=(arr1 arr2)

for idx in "${arrays[@]}"; do
    declare -n temp="$idx"
    echo "${temp[foo]}"
done

给出输出

bar
baz

正如 kojiro 指出的那样在他的评论中,只要名称具有共享前缀,实际上就不需要将数组名称存储在数组中以进行迭代。

arrays=(arr1 arr2)

for idx in "${arrays[@]}"; do

可以替换为

for idx in "${!arr@}"; do

请注意,尽管有感叹号,但这与间接扩展无关。


引用手册的相关摘录

Shell Parameters”部分:

A variable can be assigned the nameref attribute using the -n option to the declare or local builtin commands (see Bash Builtins) to create a nameref, or a reference to another variable. This allows variables to be manipulated indirectly. Whenever the nameref variable is referenced or assigned to, the operation is actually performed on the variable specified by the nameref variable's value. A nameref is commonly used within shell functions to refer to a variable whose name is passed as an argument to the function. For instance, if a variable name is passed to a shell function as its first argument, running

declare -n ref=$1

inside the function creates a nameref variable ref whose value is the variable name passed as the first argument. References and assignments to ref are treated as references and assignments to the variable whose name was passed as $1.

If the control variable in a for loop has the nameref attribute, the list of words can be a list of shell variables, and a name reference will be established for each word in the list, in turn, when the loop is executed. Array variables cannot be given the -n attribute. However, nameref variables can reference array variables and subscripted array variables. Namerefs can be unset using the -n option to the unset builtin (see Bourne Shell Builtins). Otherwise, if unset is executed with the name of a nameref variable as an argument, the variable referenced by the nameref variable will be unset.

Shell Parameter Expansion”部分:

${!prefix*}
${!prefix@}

Expands to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable. When @ is used and the expansion appears within double quotes, each variable name expands to a separate word.

关于arrays - 遍历 bash 中的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35543088/

相关文章:

c++ - 静态字符数组和指针内存

c - 感叹号出现在字符串数组 C 中最后一个值的末尾

Linux 上的 Java Runtime.exec 问题

bash - 根据 bash 脚本中的名称有选择地创建文件夹

oracle - 如何在 TOAD 中传递数组参数

c - C 中的 Snake - 整数被更改但没有更改 - 有什么问题?

java - 如何在保持元素均匀分布的java中将数组缩小到指定长度?

git - 您如何使用存储删除然后恢复非索引更改,而不会在 git 中发生 merge 冲突?

php - 关联 PHP 数组(包含对象)的非破坏性 JSON 编码/解码

awk - 如何使用 awk 命令使用关联数组来计算文件中特定字符的出现次数