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?
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
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.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加