修改数组函数参数Bash

时间:2022-02-06 21:49:22

I am trying modify an array that I have passed as a parameter to a function. So far, I have an empty array outside of the function:

我正在尝试修改作为参数传递给函数的数组。到目前为止,函数外有一个空数组:

buckets=()

Then I have the function which takes in 2 arguments. The first argument is the empty array that I want to fill. The second argument is the name of the file that contains the data I want to use to fill the array.

然后我有一个包含两个参数的函数。第一个参数是我要填充的空数组。第二个参数是包含要用来填充数组的数据的文件的名称。

So far, what I have done is create a temporary array. Then fill the temporary array with the contents of the file. This is how I do that:

到目前为止,我所做的是创建一个临时数组。然后用文件的内容填充临时数组。我就是这样做的:

fillarray ()
{
# Declare the paramater as a temporary array
declare -a tempArray=("${!1}")

# Fill it up
while IFS= read -r entry; do
  tempArray+=("$entry")
done < <(jq -r '.data | .[].name' $2)

The final step is to set the parameter array(aka buckets) to be the contents of the temporary array which we just filled up. Any suggestions on how to go about doing this?

最后一步是将参数数组(又名bucket)设置为我们刚刚填充的临时数组的内容。有什么建议吗?

2 个解决方案

#1


5  

In BASH 4.3+ you can just pass an array by named reference. So your function can be simplified to:

在BASH 4.3+中,您可以通过命名引用传递一个数组。所以你的函数可以简化为:

fillarray() {
   # tempArray is a reference to array name in $1
   local -n tempArray="$1"
   while IFS= read -r entry; do
      tempArray+=("$entry")
   done < <(jq -r '.data | .[].name' "$2")
}

Then call it as:

然后调用它:

buckets=()
fillarray buckets file.json

And test it as:

和测试它:

declare -p buckets

EDIT: To make it work on BASH 3.2 use below snippet:

编辑:要使它在BASH 3.2上工作,请使用以下代码片段:

fillarray() {
   # $2 is current length of the array
   i=$2
   while IFS= read -r entry; do
      read ${1}"[$i]" <<< "$entry"
      ((i++))
   done < <(jq -r '.data | .[].name' "$3")
}

Then call it as:

然后调用它:

buckets=()
fillarray buckets ${#buckets[@]} file.json

#2


0  

Another way via eval,

另一种方法通过eval,

fillarray () {
 local tempArray;

 while IFS= read -r entry; do
    tempArray[${#tempArray[@]}]="$entry";
 done < <(jq -r '.data | .[].name' $2);

 eval $1=\('"${tempArray[@]}"'\);
}

#1


5  

In BASH 4.3+ you can just pass an array by named reference. So your function can be simplified to:

在BASH 4.3+中,您可以通过命名引用传递一个数组。所以你的函数可以简化为:

fillarray() {
   # tempArray is a reference to array name in $1
   local -n tempArray="$1"
   while IFS= read -r entry; do
      tempArray+=("$entry")
   done < <(jq -r '.data | .[].name' "$2")
}

Then call it as:

然后调用它:

buckets=()
fillarray buckets file.json

And test it as:

和测试它:

declare -p buckets

EDIT: To make it work on BASH 3.2 use below snippet:

编辑:要使它在BASH 3.2上工作,请使用以下代码片段:

fillarray() {
   # $2 is current length of the array
   i=$2
   while IFS= read -r entry; do
      read ${1}"[$i]" <<< "$entry"
      ((i++))
   done < <(jq -r '.data | .[].name' "$3")
}

Then call it as:

然后调用它:

buckets=()
fillarray buckets ${#buckets[@]} file.json

#2


0  

Another way via eval,

另一种方法通过eval,

fillarray () {
 local tempArray;

 while IFS= read -r entry; do
    tempArray[${#tempArray[@]}]="$entry";
 done < <(jq -r '.data | .[].name' $2);

 eval $1=\('"${tempArray[@]}"'\);
}