LinuxQuestions.org
Help answer threads with 0 replies.
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 04-17-2002, 08:11 AM   #1
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Rep: Reputation: 30
Exclamation Shell Script and Dynamic variable


Hi ,

In my shell script(ksh) i want to create a variable with the name equal to the value of another variable.How can i accomplish this ?

For example suppose my variable "delta" can contain any alphabetic string(ex delta=xan ... this value can vary.. not fixed!),
then my script should create a variable called "xan" and assign it some value ... how do i know what is the name of this variable or how do i use this variable further down the script ?

for example

#!/bin/sh
delta=zine

#Now i need to create a variable called "zine" and later on do
#computation on the variable's(in the example "zine") value.
#remember that the value of "delta" is computed dynamically and #i dont have any way of knowing its value ( in affect the newly #created variables name)before hand

I guess i have been able to explain my needs

Thanks for any help provided.

Regards,
Xanthium

:smash:
 
Old 04-17-2002, 10:08 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
I'm not sure if I get what you mean. But if you want one variable to get assigned the contents of another variable then this should be enough:

var1=$var2
 
Old 04-17-2002, 10:46 AM   #3
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
he he he

Hi ,

Well it was rather funny ( considering that i have written quite a few sys monitoring scripts)... also iam sorry if my post didnt quite make my query understandable ( am not much good at english i guess !) ...

ok so suppose

#Assigning the value to the variable "var-expert"
var-expert=some-value

now immediately i want to assign a value (say 911 ) to a variable called "some-value" ( derived from the value of the above mentioned variable called "var-expert").

some-value=911

Now the trouble for me is that the value of the variable "var-expert" is not known to me ( computed by some alogorithm)

So in short the trouble for me is creating the variable !!
creating a variable with a NAME which is derived from the VALUE of ANOTHER variable


now .... after all this ranting iam not sure if iam able to make my point any more clear to you!

regards,
Xanthium.

 
Old 08-23-2003, 10:11 AM   #4
naga
LQ Newbie
 
Registered: Aug 2003
Posts: 1

Rep: Reputation: 0
Dynamic Variable Creation

hi,

U can have a look at the following code:

To set a value for a dynamic variable inside ur script, u can try the following :
=====================================
testFunc()
{
export $1="$2"
export $2="NewValueAssigned"
}
testFunc first "second"
echo "First value = $first"
echo "Second value = $second"
========================================

here testFunc is a function which accepts two parameters.
First parameter to get a variable name
Second parameter to create a new variable .




cheers
naga
 
Old 08-23-2003, 01:49 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
I think that's also not what xanthium meant.

Code:
#!/bin/sh

myvar="first"

first="one"
second="two"
third="three"

echo ${!myvar}
If you set myvar to "second" instead of "first", it will echo "two", and if you set myvar to "third" it echoes "three".

Xanthium: Is this what you are looking for?
 
Old 08-24-2003, 10:36 PM   #6
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
let me state again

Let me paste again from my first POST.

#!/bin/sh
delta=zine

#Value of the variable Delta is dynamic
#I just want to create a variable with the name equal to value
#of the variable delta
#and assign it some value
#so considering the above code... i shud be able to create a
#variable called "zine" and assign it some value.


also thx for your suggestions .... but i dont think it will work.
 
Old 08-25-2003, 06:27 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
OK. I posted how you can read the dynamic variable myvar. But i didn't mention how to assign a value to it. But naga already posted how to do this more or less.

Here goes:
Code:
#!/bin/sh

myvar="first"

# Create variable "first", by assigning "Aloha" to it,
# through the value of "myvar"
export $myvar=Aloha

echo ${!myvar}
echo $first
If this still is not what you mean, I'll have to give up...

Last edited by Hko; 08-25-2003 at 06:45 AM.
 
Old 08-25-2003, 10:40 PM   #8
xanthium
Member
 
Registered: Apr 2001
Location: Bombay ( INDIA )
Distribution: RedHat 7.x
Posts: 218

Original Poster
Rep: Reputation: 30
Hi Naga/HKO,

Yeh thx both of you .. yeh the code works perfectly.
This is what i wanted.

Thanks for your patience and time.

Regards ,
Xanthium
 
Old 10-29-2008, 12:31 PM   #9
XX_Keo_XX
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
How about this?

Looks like this thread is kind of old, so hopefully someone responds.

I am trying to do something similar to the original thread, except I want to assign a dynamic variable value to a static variable in a script.

Example.

I have a property file that looks like:

TEST_VAR0=blah
TEST_VAR1=boo
TEST_VAR2=bah

QA_VAR0=zoom
QA_VAR1=zoo
QA_VAR2=zip

PROD_VAR0=wha
PROD_VAR1=wee
PROD_VAR2=woh

our environment has a ENV_MODE set depending on the host we run on.
example: ENV_MODE=TEST, ENV_MODE=QA, or ENV_MODE=PROD

Here is what I am trying to do with the script:

#! /usr/bin/sh
#-- source the propertyfile
. PROPERTY_FILE_NAME

VAR0=${ENV_MODE}_VAR0
VAR1=${ENV_MODE}_VAR1
VAR2=${ENV_MODE}_VAR2

echo ${VAR0}
echo ${VAR1}
echo ${VAR2}


If I run the script on a host that has ENV_MODE set to TEST, here is what the output should be:
blah
boo
bah

If I run the script on a host that has ENV_MODE set to QA, here is what the output should be:
zoom
zoo
zip

If I run the script on a host that has ENV_MODE set to PROD, here is what the output should be:
wha
wee
woh

any suggestions?
 
Old 06-16-2011, 01:24 AM   #10
milanchandna
LQ Newbie
 
Registered: Jun 2011
Posts: 1

Rep: Reputation: Disabled
Were you able to do it..

@XX_Keo_XX

Hi,

were you able to do this. I am also looking for the same thing. If you got any luck in this, please guide me too.

Thanks in advance.
 
Old 06-18-2011, 01:23 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
@milanchandna: It's usually considered impolite to re-open old threads, as most people don't pay much attention to the date and my think the topic is current. Also, the original posters may not be around anymore, or may never notice the new posts since it's now buried deep in their posting history.

A better option is to start a new thread for your questions, and reference the old post in your new one.


Now to answer your question, XX_Keo_XX's script can be handled quite easily with the same ${!var} indirect referencing already shown in this thread (at least when using bash).

Slightly modified, with a loop to test all the values at once:
Code:
#!/bin/bash

PROPERTY_FILE_NAME=file.txt
. $PROPERTY_FILE_NAME

for ENV_MODE in TEST QA PROD; do

     VAR0=${ENV_MODE}_VAR0
     VAR1=${ENV_MODE}_VAR1
     VAR2=${ENV_MODE}_VAR2

     echo "${VAR0} = ${!VAR0}"
     echo "${VAR1} = ${!VAR1}"
     echo "${VAR2} = ${!VAR2}"

done
But indirect referencing like this should generally be avoided, especially when other solutions are available. Bash version 4's new associative arrays are especially useful in this regard.

See this page for a detailed run-down on dynamic variable options:
http://mywiki.wooledge.org/BashFAQ/006
 
Old 07-12-2011, 06:05 AM   #12
ZeBadger
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
@milanchandna ... this might be what you are after. I think for googling, responding to old posts is good? Since this was never really answered.

Quote:
DYNAMIC_VARIABLE=ORIGINAL_VARIABLE

# Set the "remote" variable
eval $DYNAMIC_VARIABLE="\"this is some text\""

# Append some text to the "remote" variable
eval $DYNAMIC_VARIABLE="\"\$$DYNAMIC_VARIABLE and now even more text\""


echo $ORIGINAL_VARIABLE
 
  


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
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
Shell Script Random Variable Daniel Programming 14 12-03-2007 05:00 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM
what is the shell command for getting dynamic ip?? yenonn Linux - Networking 12 07-09-2004 11:29 AM


All times are GMT -5. The time now is 09:50 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