Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-10-2009, 06:18 AM
|
#1
|
LQ Newbie
Registered: Nov 2009
Posts: 5
Rep:
|
can we use variable inside another variable
Hi,
I am facing this problem where I have declared the variable ($ProgramID) and again I'm callin this variable in another variable ($SOURCEPATH)
here the extract from my code:
SOURCEPATH=../sqa/$ProgramID/seatapps/app_dir
So, I called this variable SOURCEPATH the variable "ProgramID" does not get updated with the actual value.
thanks
|
|
|
11-10-2009, 07:05 AM
|
#2
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Please make sure to specify what platform you're referring to. I assume we're talking about bash scripting?
Yes, you can do it. Using variables to set other variables is a very common practice.
Your code should be working. I can't see any problems with it. Do make sure you aren't using single-quotes around the string, because that would keep the variable from expanding. I do suggest enclosing it in double-quotes though.
To see what your script is doing, you can use "set -x" and "set +x" to turn verbose output on and off, and will show you what substitutions are being made. A few echos liberally spaced around the code can also help.
Code:
set -x
ProgramID=123
echo "$ProgramID"
SOURCEPATH="../sqa/$ProgramID/seatapps/app_dir"
echo "$SOURCEPATH"
set +x
This works fine for me.
If you can, please post a longer sample of the code so we can see what's happening in context.
Last edited by David the H.; 11-10-2009 at 07:06 AM.
|
|
|
11-10-2009, 07:27 AM
|
#3
|
LQ Newbie
Registered: Nov 2009
Posts: 5
Original Poster
Rep:
|
thanks..
However, my problem is that the variable is set in another file and I'm trying to call the variable in another file and this way its not working
config.ini initializes the variable -> SOURCEPATH=../sqa/$ProgramID/seatapps/app_dir
month_files.sh calls-> cp $SOURCEPATH/*.air disks/seatapp/
thanks
|
|
|
11-10-2009, 07:35 AM
|
#4
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
And how are you calling config.ini from month_files.sh?
Forrest
|
|
|
11-10-2009, 07:41 AM
|
#5
|
LQ Newbie
Registered: Nov 2009
Posts: 5
Original Poster
Rep:
|
The file config.ini is read by file named main.sh and through a function call the other file month_files.sh is used.
|
|
|
11-10-2009, 07:45 AM
|
#6
|
LQ Newbie
Registered: Nov 2009
Location: Chicago, IL
Distribution: Ubuntu
Posts: 2
Rep:
|
Variable scope
The scope of a variable is limited to the file in which it is defined. In order to get that value, you need to do one of the following:
1. incorporate a way of passing the value from the external script
2. set an environment variable with the external script
3. incorporate the code from the other script
One way to pass the value is to have the source script return the value from a call like the example below. I have not tested this, but it should work...in theory. Anyone and everyone who knows better is free to correct me. The problem with this approach is that it may require you to modify the source script in which you are defining the variable.
script1:
Code:
#!/bin/bash
var1=SomeValue
return $var1
script2:
Code:
#!/bin/bash
var2=`/bin/bash script1`
echo $var2
Note: The command to define var2 is using back ticks (`) not single quotes.
Having said all that... unless you are creating a fairly large package, it may be simpler and safer to keep each script self-contained and independent of your other scripts. This way you do not need to worry about dependencies and breaking things if you have to modify one script.
|
|
|
11-10-2009, 07:50 AM
|
#7
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Well, no wonder then. It helps to include little details like that when diagnosing a problem, you know. The more relevant information you provide at the beginning, the more likely you are to get help.
It's impossible to use a script to set a variable in the shell running above it. Each script you call runs in it's own subshell, and the variables set inside them are only available to that process and any further subprocesses it creates.
You'll probably have to either alter your first script so that it exports the value to a temporary file for the second script to read, or run the first script directly inside the second script, so that its output can be used directly.
|
|
|
11-10-2009, 07:55 AM
|
#8
|
LQ Newbie
Registered: Nov 2009
Location: Chicago, IL
Distribution: Ubuntu
Posts: 2
Rep:
|
ah yes... I missed that option...
Quote:
Originally Posted by David the H.
You'll probably have to either alter your first script so that it exports the value to a temporary file for the second script to read, or run the first script directly inside the second script, so that its output can be used directly.
|
I generally prefer to avoid temporary files as they tend to make things messy if they are not deleted properly, overwrite existing temporary files, or collide with other people trying to do the same thing on the box. For complex scripts, they can also be difficult to debug if they are deleted or are using randomly generated names (to avoid overwriting an existing temp file).
Update: Of course, using environment variables has the same problems in addition to using additional memory.
Last edited by dunkar70; 11-10-2009 at 08:04 AM.
Reason: Adding clarity
|
|
|
11-16-2009, 06:19 AM
|
#9
|
LQ Newbie
Registered: Nov 2009
Posts: 5
Original Poster
Rep:
|
thanks david & dunkar...
|
|
|
11-16-2009, 06:45 AM
|
#10
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
script1
Code:
#!/bin/bash
var1=SomeValue
echo $var1
script2
Code:
#!/bin/bash
var2=$(script1)
echo $var2
Or anther way
script1
script2
Code:
#!/bin/bash
. script1
echo $var1
Or a third way.
script1
Code:
#!/bin/bash
export var1=SomeValue
script2
Code:
#!/bin/bash
echo $var1
Call this way
Evo2.
Last edited by evo2; 11-16-2009 at 06:51 AM.
Reason: 3rd way
|
|
|
11-16-2009, 11:28 AM
|
#11
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
No, that third way will not work, as has been explained already in this thread. Exporting the variable makes no difference. There's simply no way a script can set a variable outside its own subshell environment. Once the first script terminates, any variables set within it are lost, and are inaccessible from outside in any case (except through stdout or a similar redirection). Try it yourself and see.
|
|
|
11-16-2009, 11:58 AM
|
#12
|
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187
|
The dot, however, does run the script in the current context, and is used extensively by the system scripts to get parameter values into a running script. So evo2's second way is, I believe, the way to go.
You might also want to look at the exec function, and the differences between $(...) and ${...}, although the latter - in the ${!..} form - is used for referencing the value of a variable defined by the value of another variable. (I.e., "indirection.") It's "useful" for writing self-modifying bash scripts, if you have some need for that functionality.
|
|
|
11-16-2009, 12:35 PM
|
#13
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
A variable can be set in a calling script via the eval command if the called script writes a variable assignment. For example ...
script1.sh: script2.sh:
|
|
|
11-16-2009, 03:44 PM
|
#14
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Quote:
Originally Posted by David the H.
No, that third way will not work, as has been explained already in this thread. Exporting the variable makes no difference. There's simply no way a script can set a variable outside its own subshell environment. Once the first script terminates, any variables set within it are lost, and are inaccessible from outside in any case (except through stdout or a similar redirection). Try it yourself and see.
|
True. No need to try, just need to read what I typed :-)
Should have been:
script1
Code:
export var1=SomeValue
Run like
Evo2.
|
|
|
All times are GMT -5. The time now is 08:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|