LinuxQuestions.org
Visit Jeremy's Blog.
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
 
Search this Thread
Old 10-06-2006, 02:50 AM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
Bash scripting: how can I reference a variable?


Hi everyone,

I have some variables whose names I generated when going through the loop.

for i in `seq 1 10`
do
${i}_title=some_string
done

Now I want to make references to the ${i}_title variables in another loop. How do I do that? I guess my question is how to do nested references to a variable. Thanks.

Last edited by frankie_DJ; 10-06-2006 at 02:58 AM.
 
Old 10-06-2006, 03:19 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
The construct
${i}_title
can only be used to create a string value, not a variable name
You need to look at arrays tldp.org/LDP/abs/html/arrays.html
 
Old 10-06-2006, 03:38 AM   #3
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
I thought about that. However, in my second loop I am going through another array. So, if I use array here, then I would have to loop through two arrays simultaneously, using the same index. This would be trivial in any other language but is it possible in bash?

EDIT: Sorry if this is confusing. I think I figured out how to get it working with arrays. Thanks

Last edited by frankie_DJ; 10-06-2006 at 03:41 AM.
 
Old 10-06-2006, 03:55 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,561
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
I think I figured out how to get it working with arrays.
Well, then share it with your fellow LQ members, or do you want to keep everything for yourself?
 
Old 10-06-2006, 06:13 AM   #5
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
OK well in my first loop I am stepping through integers
1 through 11

Code:
for i in `seq 1 11`
do
Then, rather then generating title with

${i}_title=some_string

(which cannot later be referenced as a variable, so it's useless to me) I generate an array "title"
Code:
title[$i]=some_string
In my second loop I am stepping through members of a different array, which I also made to be integers (not necessarily consecutive).
Code:
for i in ${some_other_array[@]}
do
This is convinient b/c now I can reference "title" array inside of this for loop:
Code:
${title[$i]}
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 09:47 PM
A book/Online reference for shell scripting waelaltaqi Linux - General 8 03-08-2006 01:19 PM
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 11:23 PM
shell scripting: variable problem XST1 Programming 7 01-30-2005 02:45 PM
reference Variable in C++ atul_mehrotra Programming 6 10-06-2004 11:49 AM


All times are GMT -5. The time now is 02:00 AM.

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 Google+: linuxquestions
Open Source Consulting | Domain Registration