繁体   English   中英

如何将Array作为参数与其他两个字符串参数一起传递给Shell脚本函数?

[英]How to pass Array as a argument to shell script function along with other two string parameters?

#!/bin/bash  
myfunc() {  
local new_arr  
new_arr=("$@")  
echo "Updated value is: ${new_arr[*]}"  
}  
my_arr=(4 5 6)  
x="test1"  
y="test2"  
echo "Old array is ${my_arr[*]}"  
myfunc ${my_arr[*]} $x $y   

该程序的输出为:

Old array is 4 5 6
Updated value is: 4 5 6 test1 test2

我想在函数myfunc()中分别访问x,y和my_array,
但是我不知道数组的大小。
像$ 1这样的东西是my_array $ 2这样的东西是x,依此类推。
但是我不知道如何在shell脚本中执行此操作。
请注意,我的bash版本是:-版本4.1.2

#!/bin/bash  
myfunc() {  
local new_arr  
first_variable=$1 && shift 
second_variable=$2 && shift
new_arr=("$@")  
echo "Updated value is: ${new_arr[*]}"  
}  
my_arr=(4 5 6)  
x="test1"  
y="test2"  
echo "Old array is ${my_arr[*]}"  
myfunc "$x" "$y" "${my_arr[@]}"

输出Old array is 4 5 6 Updated value is: 4 5 6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM