如何将关联数组传递给 Gnu Parallel
How to pass associative array to Gnu Parallel
就像标题一样,我已经声明了一堆变量和一个函数。当我通过一个函数传递它们时,我得到了我所期望的。但是 运行 通过并行执行的相同代码没有...如何解决?
#!/bin/bash
declare -xA MAP # export associative array
declare -x str="ing" # export variable
MAP[bar]="baz"
MAP[bar2]="baz2"
foo() {
echo "$@"
echo "variable: ${str}"
echo "map: ${MAP[@]}"
}
export -f foo
foo "call function directly:"
直接调用函数:
变量:ing
地图:baz2 baz
parallel foo ::: "call function through parallel" ::: 1 2 3
通过并行1调用函数
变量:ing
地图:
通过并行调用函数 2
变量:ing
地图:
通过并行调用函数 3
变量:ing
地图:
评论后编辑
看起来 accepted answer 这个问题是:没有真正好的方法将数组变量编码到环境中。
有点难过……;)
从问题下的评论看来,接受的答案是:没有真正好的方法将数组变量编码到环境中。
仅此而已……;)
看来还有一个:Accessing Associative Arrays in GNU Parallel 方法相当复杂。但是...
经过一些修改(正如 Socovi 建议的那样,特定问题可以有解决方案),对于我的情况来说,似乎足够好的解决方法是 "serialize" 临时文件中的数组并在函数中反序列化它。
#!/bin/bash
declare -A MAP # export associative array
declare -x str="ing" # export variable
MAP[bar]="baz"
MAP[bar2]="baz2"
declare -x serialized_array=$(mktemp)
# declare -p can be used to dump the definition
# of a variable as shell code ready to be interpreted
declare -p MAP > "${serialized_array}"
# perform cleanup after finishing script
cleanup() {
rm "${serialized_array}"
}
trap cleanup EXIT
foo() {
echo "$@"
echo "variable: ${str}"
source "${serialized_array}" # deserialize an array
echo "map: ${MAP[@]}"
}
export -f foo
foo "call function directly:"
直接调用函数:
变量:ing
地图:baz2 baz
parallel foo ::: "call function through parallel" ::: 1 2 3
通过并行1调用函数
变量:ing
地图:baz2 baz
通过并行调用函数 2
变量:ing
地图:baz2 baz
通过并行调用函数 3
变量:ing
地图:baz2 baz
就像标题一样,我已经声明了一堆变量和一个函数。当我通过一个函数传递它们时,我得到了我所期望的。但是 运行 通过并行执行的相同代码没有...如何解决?
#!/bin/bash
declare -xA MAP # export associative array
declare -x str="ing" # export variable
MAP[bar]="baz"
MAP[bar2]="baz2"
foo() {
echo "$@"
echo "variable: ${str}"
echo "map: ${MAP[@]}"
}
export -f foo
foo "call function directly:"
直接调用函数:
变量:ing
地图:baz2 baz
parallel foo ::: "call function through parallel" ::: 1 2 3
通过并行1调用函数
变量:ing
地图:
通过并行调用函数 2
变量:ing
地图:
通过并行调用函数 3
变量:ing
地图:
评论后编辑
看起来 accepted answer 这个问题是:没有真正好的方法将数组变量编码到环境中。
有点难过……;)
从问题下的评论看来,接受的答案是:没有真正好的方法将数组变量编码到环境中。
仅此而已……;)
看来还有一个:Accessing Associative Arrays in GNU Parallel 方法相当复杂。但是...
经过一些修改(正如 Socovi 建议的那样,特定问题可以有解决方案),对于我的情况来说,似乎足够好的解决方法是 "serialize" 临时文件中的数组并在函数中反序列化它。
#!/bin/bash
declare -A MAP # export associative array
declare -x str="ing" # export variable
MAP[bar]="baz"
MAP[bar2]="baz2"
declare -x serialized_array=$(mktemp)
# declare -p can be used to dump the definition
# of a variable as shell code ready to be interpreted
declare -p MAP > "${serialized_array}"
# perform cleanup after finishing script
cleanup() {
rm "${serialized_array}"
}
trap cleanup EXIT
foo() {
echo "$@"
echo "variable: ${str}"
source "${serialized_array}" # deserialize an array
echo "map: ${MAP[@]}"
}
export -f foo
foo "call function directly:"
直接调用函数:
变量:ing
地图:baz2 baz
parallel foo ::: "call function through parallel" ::: 1 2 3
通过并行1调用函数
变量:ing
地图:baz2 baz
通过并行调用函数 2
变量:ing
地图:baz2 baz
通过并行调用函数 3
变量:ing
地图:baz2 baz