Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为动态数组赋值_Arrays_Bash_Shell_Dynamic - Fatal编程技术网

Arrays 为动态数组赋值

Arrays 为动态数组赋值,arrays,bash,shell,dynamic,Arrays,Bash,Shell,Dynamic,我的bash脚本需要从属性文件中读取值,并将它们分配给多个数组。阵列的数量也通过配置进行控制。我目前的代码如下: limit=$(sed '/^\#/d' $propertiesFile | grep 'limit' | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') for (( i = 1 ; i <= $limit ; i++ )) do #properties that def

我的bash脚本需要从属性文件中读取值,并将它们分配给多个数组。阵列的数量也通过配置进行控制。我目前的代码如下:

limit=$(sed '/^\#/d' $propertiesFile | grep 'limit'  | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
for (( i = 1 ; i <= $limit ; i++ ))
do
   #properties that define values to be assigned to the arrays are labeled myprop## (e.g. myprop01, myprop02):
   lookupProperty=myprop$(printf "%.2d" "$i")
   #the following line reads the value of the lookupProperty, which is a set of space-delimited strings, and assigns it to the myArray# (myArray1, myArray2, etc):
   myArray$i=($(sed '/^\#/d' $propertiesFile | grep $lookupProperty  | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'))
done
我很确定问题出在我声明“myArray$I”数组的方式上。然而,我尝试的任何不同方法都会产生相同的错误或不完整的结果


有什么想法/建议吗?

您是对的,bash没有将构造
myArray$i=(一些数组值)
识别为数组变量赋值。解决办法之一是:

read -a myArray$i <<<"a b c"
以上允许分配。下一个问题是如何读取myArray$i之类的变量。一种解决方案是间接命名变量,如下所示:

var="myArray$i[2]" ; echo ${!var}
read -a myArray$i <<<"$(sed '/^\#/d' $propertiesFile | grep$lookupProperty  | tail -n 1 | cut -d "=" -f2- | seds/^[[:space:]]*//;s/[[:space:]]*$//')"  
var="myArray$i[2]" ; echo ${!var}