簡體   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