I am new to bash script and I wonder why I recieve the above message. I try to an arithmetic value which comes from the for loop and then I would like to print the array. Can anyone help me?
我对bash脚本不熟悉,我想知道为什么我收到上面的消息。我尝试一个算术值,它来自于for循环,然后我想打印数组。谁能帮我吗?
Thank you in advance!!
提前谢谢你! !
#!/bin/bash
declare -a SCORES
for j in `seq 0 5`;
do
SCORES$j="$(sh myscript.sh $DSLAM $j | grep "" -c )"
done
for k in "${SCORES[@]}"
do
echo "message $'\t' $SCORES$k"
done
echo ${#SCORES}
=======
= = = = = = =
Output
输出
abcd.sh: line 16: SCORES0=3: command not found
abcd.sh: line 16: SCORES1=135: command not found
abcd.sh: line 16: SCORES2=826: command not found
abcd.sh: line 16: SCORES3=107: command not found
abcd.sh: line 16: SCORES4=3: command not found
abcd.sh: line 16: SCORES5=3: command not found
0
1 个解决方案
#1
4
You cannot assign variable with name, which is generated at run time; at least not the way you are trying.
不能分配带有名称的变量,该变量在运行时生成;至少不是你尝试的方式。
You have below options:
你有以下选择:
declare "SCORES$j=$(sh myscript.sh $DSLAM $j | grep '' -c )" # creates new variables like SCORES1, SCORES2 etc.
eval "SCORES$j=$(sh myscript.sh $DSLAM $j | grep '' -c )" #Definitely not preferred.
SCORES[$j]="$(sh myscript.sh $DSLAM $j | grep '' -c )" #uses array you have created.
Most likely, option 3 is what you want.
最可能的情况是,选项3是您想要的。
#1
4
You cannot assign variable with name, which is generated at run time; at least not the way you are trying.
不能分配带有名称的变量,该变量在运行时生成;至少不是你尝试的方式。
You have below options:
你有以下选择:
declare "SCORES$j=$(sh myscript.sh $DSLAM $j | grep '' -c )" # creates new variables like SCORES1, SCORES2 etc.
eval "SCORES$j=$(sh myscript.sh $DSLAM $j | grep '' -c )" #Definitely not preferred.
SCORES[$j]="$(sh myscript.sh $DSLAM $j | grep '' -c )" #uses array you have created.
Most likely, option 3 is what you want.
最可能的情况是,选项3是您想要的。