LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-08-2012, 07:58 AM   #1
Mixiul
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Rep: Reputation: Disabled
Bash - Creating Arrays using loop counter


I am writing a bash script where I need to create arrays inside a for loop and name each array using the string contained in the counter (an array element) each iteration.

Here is the code:

Code:
myArray=( joe bob dave mark )


for k in "${myArray[@]}"
do
  "$k_array"=( `cat fileUsedToPopulate.txt` )
done

# echo to test if one of the arrays has been created and populated

for j in "${joe_array[@]}"
do
  echo $j
done
The desired result is 4 arrays, joe_array bob_array etc, each populated with the file.

However I haven't found anyway to escape the $k so that it appends to the array name when declaring/populating it.

Here is the error I am getting:
Code:
line 30: syntax error near unexpected token `cat fileUsedToPopulate.txt' 
line 30: "$k_array"=(cat $DIR/$braDir/oem.txt` )'
Thanks for any help provided.

Last edited by Mixiul; 02-08-2012 at 09:04 AM.
 
Old 02-08-2012, 08:32 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 332

Rep: Reputation: 118Reputation: 118
It's most likely because variable $k_array does not exist, so after substitution, the line actually reads

Code:
=( `cat fileUsedToPopulate.txt` )
which is a syntax error.
 
Old 02-08-2012, 08:45 AM   #3
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,334

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The real problem is that this is an example of indirect/variable variable use, and it can't be done directly in the way you want. There are ways to go about it though. See this link for a full discussion.

http://mywiki.wooledge.org/BashFAQ/006

I highly suggest, if possible, looking into redesigning your workflow to use associative arrays instead. But without seeing the larger context of your script it's hard to say if that's the best choice.
 
1 members found this post helpful.
Old 02-08-2012, 08:56 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 332

Rep: Reputation: 118Reputation: 118
The real problem is that the OP is trying to use a non-existing variable $k, when it's clearly supposed to be $i. After fixing that, and possibly also using {}, then it's going to be a problem of indirect variables. :P
 
Old 02-08-2012, 09:05 AM   #5
Mixiul
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
The real problem is that the OP is trying to use a non-existing variable $k, when it's clearly supposed to be $i. After fixing that, and possibly also using {}, then it's going to be a problem of indirect variables. :P
Typo ^^ Thank you for your replies.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 09:18 AM
Bash - Arrays and the for-loop Orangutanklaus Linux - General 2 10-23-2009 01:04 PM
bash script: how do I refer to a counter i in a "for i in" loop frankie_DJ Programming 5 07-24-2007 08:45 AM
BASH assistance - loop/counter rickenbacherus Programming 6 03-12-2007 05:34 PM


All times are GMT -5. The time now is 03:01 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration