Register
It is currently Mon Apr 21, 2014 5:01 am

naming variables with variables


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Fri Jun 27, 2008 7:05 am   

Joined: Fri Jun 27, 2008 7:03 am
Posts: 2
Hello,

FIRST QUESTION:

I am writing a script in which a query is taken at the beginning of the script to be later used at the end. In the query, variables are generated from a loop, and I would like to assign the variable NAME (not value) with an appended 1, 2, 3, 4.....n. The number of loops varies according to the circumstance.

So for example, I get:
$pattern1, $pattern2, $pattern3...etc

The problem I have is that these variables are used later in the script in a loop, in which the first pass of the loop needs to call the first $pattern1, the second pass should call $pattern2, etc.

How do I dynamically call each variable in its sequence?

If I try something like incrementing a variable called "pass" each time, and then appending it to pattern:

$pattern$pass

I merely get the concatenation of the two (which simply is the value of $pass, since $pattern is not set).

I have tried things like:

${pattern$pass}

but only get errors.

Any ideas on how to call variables in this way?


SECOND QUESTION

At the beginning of the script where I am originally assigning values to the $pattern/n variables, I am also naming them dynamically, using

let pattern$pass=value

so when pass=1 I end up with a variable called pattern1 with the value of value.

(simply typing pattern$pass=value does not work, I had to explicitly use the let command.)

This works fine, but I run into trouble when I want to place forward slashes around value in order to substitute it in a sed script, eg,

let pattern$pass=/value/

I get an error from let saying operand expected.

Does anyone know how I might accomplish this?

I can work around this one, but I would rather find out how to do what I want to do.Thank you,

Allasso


Top
 Profile  
 PostPosted: Fri Jun 27, 2008 11:06 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
I think tldp.org's advanced bash scripting guide covers this.


Top
 Profile  
 PostPosted: Sun Jun 29, 2008 12:13 pm   

Joined: Fri Jun 27, 2008 7:03 am
Posts: 2
Someone pointed me to using arrays, and that takes care of both problems.

Thanks for your input.

Allasso


Top
 Profile  
 PostPosted: Wed Jul 23, 2008 2:04 pm   

Joined: Wed Jul 23, 2008 1:52 pm
Posts: 1
Location: Poland
Sorry for late anserw, looks like you've found a solution already. If someone has simillar problem, he can use this:
Code:
eval $<variable>=<value>

Example:

Code:
$ TEMPLATE="NEW_VARIABLE_NAME"
$ eval $TEMPLATE="some_value"
$ echo $NEW_VARIABLE_NAME
some_value


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP