Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )

Advanced DNS Management
New ZoneEdit. New Managment.


Sign Up Now
> Nested Variable Names in Array Summation
post Feb 24 2016, 05:33 PM
Post #1

Whats this Lie-nix Thing?

Group: Members
Posts: 15
Joined: 22-February 16
Member No.: 19,047

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

while [ $j -lt 8 ]
while [ $i -le $max ]
sum[$j]=$( echo "scale=4; ${temp_"$i"[$j]} + ${sum[$j]}" | bc )

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.

This post has been edited by Valjean: Feb 24 2016, 08:24 PM
Go to the top of the page
+Quote Post
Start new topic
post Feb 25 2016, 08:06 AM
Post #2

Its GNU/

Group: Support Specialist
Posts: 1,796
Joined: 23-January 03
Member No.: 360

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 )
Go to the top of the page
+Quote Post

Posts in this topic

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:


RSS Lo-Fi Version Time is now: 16th October 2017 - 07:28 PM