这是另一种方式:您可以在将关联数组传递给函数时手动序列化它,然后将其反序列化回函数内的新关联数组:
1.关联数组的手动序列化/反序列化
这是来自我的eRCaGuy_hello_world 存储库的完整、可运行的示例:
array_pass_as_bash_parameter_2_associative.sh:
# Print an associative array using manual serialization/deserialization
# Usage:
# # General form:
# print_associative_array array_length array_keys array_values
# # Example
# print_associative_array "${#array1[@]}" "${!array1[@]}" "${array1[@]}"
print_associative_array() {
i=1
# read 1st argument, the array length
array_len="${@:$i:1}"
((i++))
# read all key:value pairs into a new associative array
declare -A array
for (( i_key="$i"; i_key<$(($i + "$array_len")); i_key++ )); do
i_value=$(($i_key + $array_len))
key="${@:$i_key:1}"
value="${@:$i_value:1}"
array["$key"]="$value"
done
# print the array by iterating through all of the keys now
for key in "${!array[@]}"; do
value="${array["$key"]}"
echo " $key: $value"
done
}
# Let's create and load up an associative array and print it
declare -A array1
array1["a"]="cat"
array1["b"]="dog"
array1["c"]="mouse"
# length indices (keys) values
print_associative_array "${#array1[@]}" "${!array1[@]}" "${array1[@]}"
样本输出:
a: cat
b: dog
c: mouse
解释:
对于名为 的给定函数print_associative_array
,以下是一般形式:
# general form
print_associative_array array_length array_keys array_values
对于名为 的数组array1
,以下是获取数组长度、索引(键)和值的方法:
- 数组长度:
"${#array1[@]}"
- 所有数组索引(在本例中为键,因为它是关联数组):
"${!array1[@]}"
- 所有数组值:
"${array1[@]}"
因此,一个示例调用print_associative_array
如下所示:
# example call
# length indices (keys) values
print_associative_array "${#array1[@]}" "${!array1[@]}" "${array1[@]}"
将数组的长度放在首位是必不可少的,因为它允许我们在传入的序列化数组到达所有传入参数print_associative_array
的魔术数组中的函数时对其进行解析。@
要解析@
数组,我们将使用数组切片,如下所述(此片段是从我的答案复制粘贴在这里):
# array slicing basic format 1: grab a certain length starting at a certain
# index
echo "${@:2:5}"
# │ │
# │ └────> slice length
# └──────> slice starting index (zero-based)
2. [比上面更好的技术!] 通过引用传递数组
...正如@Todd Lehman 在他的回答中解释的那样
# Print an associative array by passing the array by reference
# Usage:
# # General form:
# print_associative_array2 array
# # Example
# print_associative_array2 array1
print_associative_array2() {
# declare a local **reference variable** (hence `-n`) named `array_reference`
# which is a reference to the value stored in the first parameter
# passed in
local -n array_reference="$1"
# print the array by iterating through all of the keys now
for key in "${!array_reference[@]}"; do
value="${array_reference["$key"]}"
echo " $key: $value"
done
}
echo 'print_associative_array2 array1'
print_associative_array2 array1
echo ""
echo "OR (same thing--quotes don't matter in this case):"
echo 'print_associative_array2 "array1"'
print_associative_array2 "array1"
样本输出:
print_associative_array2 array1
a: cat
b: dog
c: mouse
OR (same thing--quotes don't matter in this case):
print_associative_array2 "array1"
a: cat
b: dog
c: mouse
也可以看看:
- [我的回答]我序列化/反序列化常规“索引”bash数组以便将其中一个或多个作为参数传递给函数的更广泛的演示:Passing arrays as parameters in bash
- [我的回答] 我通过引用传递常规“索引”bash 数组的演示:Passing arrays as parameters in bash
- [我的答案] 数组切片:Unix & Linux:Bash:位置参数切片
- [我的问题]为什么
man bash
页面声明declare
andlocal
-n
属性“不能应用于数组变量”,但它可以?