问题描述
如何传递一个数组作为参数传递给bash函数?
How can I pass an array as parameter to a bash function?
注意:不是堆栈溢出这里找到答案之后,我贴我有点粗的解决方案自己。它允许只有一个阵列被传递,并且它是参数表的最后一个元素。实际上,它不是通过在所有阵列,但其元素,通过called_function(),它被重新组装成一个阵列的列表,但它为我工作。如果有人知道更好的方法,欢迎随时添加。
Note: After not finding an answer here on Stack Overflow, I posted my somewhat crude solution myself. It allows for only one array being passed, and it being the last element of the parameter list. Actually, it is not passing the array at all, but a list of its elements, which are re-assembled into an array by called_function(), but it worked for me. If someone knows a better way, feel free to add it here.
推荐答案
您可以通过多阵列作为参数是这样的:
takes_ary_as_arg()
{
declare -a argAry1=("${!1}")
echo "${argAry1[@]}"
declare -a argAry2=("${!2}")
echo "${argAry2[@]}"
}
try_with_local_arys()
{
# array variables could have local scope
local descTable=(
"sli4-iread"
"sli4-iwrite"
"sli3-iread"
"sli3-iwrite"
)
local optsTable=(
"--msix --iread"
"--msix --iwrite"
"--msi --iread"
"--msi --iwrite"
)
takes_ary_as_arg descTable[@] optsTable[@]
}
try_with_local_arys
将回声:
sli4-IREAD sli4-IWRITE sli3-IREAD sli3-IWRITE结果
--msix --iread --msix --iwrite --msi --iread --msi --iwrite
will echo:
sli4-iread sli4-iwrite sli3-iread sli3-iwrite
--msix --iread --msix --iwrite --msi --iread --msi --iwrite
这篇关于带有数组在bash参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!