I am struggling to write two nested do loops; there is a problem with repeat dollar sign declaration syntax. This is in bash.

Early in my program, I define

readarray -t temp_"$i" < frequency_values

in a loop over i, thus creating a whole bunch of arrays that are read in. This is fine. Each array is a 1x8 matrix.

Later on, I want to sum all of the matrix values of all the temporary arrays. I wrote

i=1

j=0

while [ $j -lt 8 ]

do

while [ $i -le $max ]

do

sum[$j]=$( echo "scale=4; ${temp_"$i"[$j]} + ${sum[$j]}" | bc )

i=$((i+1))

done

i=1

j=$((j+1))

done

The problem occurs in the sum line; bash objects to the quotes around $i in the name of the temp_$i arrays. I get the error:

line 35: scale=4; ${temp_"$i"[j]} + ${sum[j]}: bad substitution

How can I do a summation of matrix elements with a variable name of the array?

I have not googled this because I am honestly not even sure where to begin; I could not find a search string that was productive.

echo "scale=4; ${temp_"$i"[j]} + ${sum[j]}" | bc

Typically in quoted strings you would need an escape character for quotes.

I would try without i.e.

echo "scale=4; ${temp_$i[j]} + ${sum[j]}" | bc

or if that does not work

echo "scale=4; ${temp_\"$i\"[j]} + ${sum[j]}" | bc

echo "scale=4; ${temp_"$i"[j]} + ${sum[j]}" | bc

Typically in quoted strings you would need an escape character for quotes.

I would try without i.e.

echo "scale=4; ${temp_$i[j]} + ${sum[j]}" | bc

or if that does not work

echo "scale=4; ${temp_\"$i\"[j]} + ${sum[j]}" | bc

Typically in quoted strings you would need an escape character for quotes.

I would try without i.e.

echo "scale=4; ${temp_$i[j]} + ${sum[j]}" | bc

or if that does not work

echo "scale=4; ${temp_\"$i\"[j]} + ${sum[j]}" | bc

No success on either proposal. What do the \ characters do as escape characters? Maybe if I better understand I can find a way to do this. I have been google searching about this, and it seems like the subject of indirect redirection is related to this?

There might be better ways to use a dynamic variable but here is one way.

#!/bin/bash

temp_2[0]=1

i=2

j=0

test=temp_$i[$j]

echo ${!test}

OK! Making progress! Your suggestion worked in getting around the first hurdle, which was the big one! I thank you very much! My, how complicated, though. From what I have been reading, it seems that this problem is a general one in Linux formatting (but, again, I am a neophyte, what do I know). It has been suggested to me that I should learn python for these sorts of manipulations, later.

When I use the following code, and print out the sum array, my values seem good. However, I now have a different problem from the original: there is something wrong with my syntax in summing in old values in the array. The commented line is what I "want" to do, meaning to have a construct like

i=i+j

that is, the new value is equal to the old value plus a variable. However, when I attempt to execute the code as is, I get the error

(standard_in) 2: syntax error

which implies that I do not have the right syntax to read in the previous matrix element.

Code:

j=0

i=0

while [ $j -lt 8 ]

do

while [ $i -le $max ]

do

temp_0[0]=0

test=temp_$i[$j]

#sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )

sum[$j]=$( echo "scale=4; ${!test} + 0" | bc )

i=$((i+1))

done

i=1

j=$((j+1))

done

Is it obvious what I am doing wrong in trying to say array element=array element+variable? I am wondering if this is a case where the programming way of doing math (i.e., i=i+1 is nonsense mathematically) is a problem for an array?

bash syntax can be difficult at times.

I find nothing wrong with your syntax. It works for me on a single case.

sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )

bash syntax can be difficult at times.

I find nothing wrong with your syntax. It works for me on a single case.

sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )

I find nothing wrong with your syntax. It works for me on a single case.

sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )

How curious! I realize we are now in a no man's land of sorts because you cannot reproduce my error. The only thing I can think to ask is if the words (standard_in) 2: has any meaning to you that it can help me identify the mistake? I am unsure what to say, given the same line of code produces a problem for me. My total code is as follows, if it's any more insightful?

while [ $j -lt 8 ]

do

while [ $i -le $max ]

do

temp_0[0]=0

test=temp_$i[$j]

#echo ${!test}

sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )

#sum[$j]=$( echo "scale=4; ${!test} + 0" | bc )

#echo "${sum[@]}"

i=$((i+1))

done

i=1

j=$((j+1))

done

echo "${sum[@]}"

I appreciate your help in this.

My guess is that you do not an initial value assigned to each variable.

#/bin/bash

i=0

j=0

max=8

sum[0]=0

for (( c=0; c<=8; c++ ))

do

sum[$c]=0

temp_0[$c]=$c

temp_1[$c]=$c

temp_2[$c]=$c

temp_3[$c]=$c

temp_4[$c]=$c

temp_5[$c]=$c

temp_6[$c]=$c

temp_7[$c]=$c

temp_8[$c]=$c

temp_9[$c]=$c

done

while [ $j -le 8 ]

do

while [ $i -le $max ]

do

temp_0[0]=0

test=temp_$i[$j]

#echo ${!test}

sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )

#sum[$j]=$( echo "scale=4; ${!test} + 0" | bc )

#echo "${sum[@]}"

i=$((i+1))

done

i=0

j=$((j+1))

done

echo "${sum[@]}"

The output

0 9 18 27 36 45 54 63 72

My guess is that you do not an initial value assigned to each variable.

#/bin/bash

i=0

j=0

max=8

sum[0]=0

for (( c=0; c<=8; c++ ))

do

sum[$c]=0

temp_0[$c]=$c

temp_1[$c]=$c

temp_2[$c]=$c

temp_3[$c]=$c

temp_4[$c]=$c

temp_5[$c]=$c

temp_6[$c]=$c

temp_7[$c]=$c

temp_8[$c]=$c

temp_9[$c]=$c

done

while [ $j -le 8 ]

do

while [ $i -le $max ]

do

temp_0[0]=0

test=temp_$i[$j]

#echo ${!test}

sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )

#sum[$j]=$( echo "scale=4; ${!test} + 0" | bc )

#echo "${sum[@]}"

i=$((i+1))

done

i=0

j=$((j+1))

done

echo "${sum[@]}"

The output

0 9 18 27 36 45 54 63 72

#/bin/bash

i=0

j=0

max=8

sum[0]=0

for (( c=0; c<=8; c++ ))

do

sum[$c]=0

temp_0[$c]=$c

temp_1[$c]=$c

temp_2[$c]=$c

temp_3[$c]=$c

temp_4[$c]=$c

temp_5[$c]=$c

temp_6[$c]=$c

temp_7[$c]=$c

temp_8[$c]=$c

temp_9[$c]=$c

done

while [ $j -le 8 ]

do

while [ $i -le $max ]

do

temp_0[0]=0

test=temp_$i[$j]

#echo ${!test}

sum[$j]=$( echo "scale=4; ${!test} + ${sum[$j]}" | bc )

#sum[$j]=$( echo "scale=4; ${!test} + 0" | bc )

#echo "${sum[@]}"

i=$((i+1))

done

i=0

j=$((j+1))

done

echo "${sum[@]}"

The output

0 9 18 27 36 45 54 63 72

You are correct! Thank you!

Powered by Invision Power Board (http://www.invisionboard.com)

© Invision Power Services (http://www.invisionpower.com)