1
我的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
当我尝试执行上面的代码,会显示以下错误信息:
syntax error near unexpected token `$(sed '/^\#/d' $propertiesFile | grep $lookupProperty | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')'
我相当肯定的问题是我声明的方式“myArray $ i”数组。但是,我尝试的任何不同方法都会产生相同的错误或不完整的结果。
任何意见/建议?
谢谢! '读-a myArray $ i <<<“a b c”'做了诀窍。 – user3078422