Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass var name to sub-script and evaluate

Status
Not open for further replies.

mjpearson

Technical User
Dec 13, 2002
196
US
I'm taking my first Linux class so, I'm still learning.

I want to build a user-variable with value. I want to pass the variable-name to sub-script and have it print the value stored in the passed-variable name. I can't figure out the syntax. I've tried lots of different syntax and can't get it to work. Example below. Anyone got some advice?

script1
*******
var_name=%1
var_value=%2

$var_name=$var_value

./script2 $var_name

script2
*******
echo ${$%1}




mike
 
I forgot to add the EXPORT command but that doesn't solve the problem in the second script.

script1
*******
var_name=%1
var_value=%2

export $var_name=$var_value

./script2 $var_name

script2
*******
echo ${$%1}



mike
 
What does the % character mean in an assignment? I've never seen that before.
 
Sorry, you're right. I'm taking a Linux class and I keep getting the syntax confused.

The problem remains. How do I expand the variable in the subprogram so it can then expand to provide the final value of the variable?


mike
 
First of all is $var_name being passed to script2? If it is you should be able to call it using $1 inside script2 since it's the first positional parameter.

So in this case:
Script2
========
echo $1

Mind you all this is assuming you're using bash (don't know why you would use anything else).
 
Yes, Bash.

By expanding $1, I get the variable name only....not the value stored in this variable.

e.g.

"./script1 mike 123"
script2 echos "mike" not "123"




mike
 
I assume you made a typo in your script calling in the example.

By calling ./script mike 123 you've passed 2 positional parameters to the script. $1 is "mike" and $2 is "123" so you should be calling $2. Hope you see where the problem lies now.
 
That's intentional. Yes, I want to pass the variable name not the value. The idea is to make it look like a function wherein the user defines a variable in the first script, assigns a value to the variable. The variable name is passed to the second script. The second script then evaluates the variable name to retrieve the value.


mike
 
Personally, I don't see why don't just retrieve var_value. In either case the script is:

Script1
*********
var_name=$1
var_value=$2

export var_name=$var_value

./script2 $var_name


Script2
*********
echo $1
 
Are you looking for indirect expansion?

Code:
direct=foo
indirect=direct
echo ${!indirect}  # produces 'foo'
 
I understood he want's to set a variable-name to a value, both dynamically in script 1, like
Code:
script1 COLUMNS 80
script1 DB_TYP ORACLE

and assign the variable-name in script 1 to the variable-value, and call a second script, telling it to show the value of the variable, by calling it with the name

Code:
script2 COLUMNS
shall return
80
and
Code:
script2 DB_TYP
shall return
ORACLE

Why does he not just call script2 directly with the parameter:
Code:
script2 80
or
Code:
script2 ORACLE
?
From the example we don't see.

seeking a job as java-programmer in Berlin:
 
THAT'S IT. Thanks.

Now for some background. I'm taking a Unix(Linux) class and one of the lab assignments was this:

"Script5: Export a varible entered at the prompt line, echo the variable and call Script5b which echoes the variable again."

I think the object was to demonstrate how the EXPORT command works.

The instructor says I read way too much into the instructions but I've invested too much time and effort to back away now. I wanted to add some pizzaz to the script so, I:

* prompt the user for the VARIABLE_NAME;
* prompt the user for the VALUE;
* assign the VALUE to the new VARIABLE_NAME;
* echo the VARIABLE_NAME and VALUE;
* call the subscript and pass the VARIABLE_NAME via the command line;
* the last step was trying to figure out how to display the VARIABLE_NAME and VALUE in the subscript but I didn't understand the indirect method;

I think I tried every possible combination except for the one provided earlier in the thread.

Thanks again for the learning.


mike
 
Here are a few things to keep in mind:
1. Posting for help on assignments is against board rules.

2.) export creates an envroment variable, which as of yet the script posted above doesn't do.

What he wants to get across is that I can run his solution to "script1" once, then "script2" will always return the right value ever after in that shell (even years later if that shell is still open and the machine working). Sorta like your PATH variable is set in .bash_profile, and after that all the shell has to do is search it to find programs so that you don't have to type:
/bin/ls
OR
/usr/bin/ls
every time you want to print the contents of a directory, because .bash_profile is run every time a new bash shell is opened.

[plug=shameless]
[/plug]
 
1) I apologize. I thought I was going above and beyond the scope of the assignment. My instructor couldn't help me. I could not find any references to the subject matter in my text. I couldn't find any references to the subject on the Net. Until this posting, I didn't realize I was looking for the term "indirect".

2) Thanks for the refresher. I understand that the variables are contained within the scope of the "calling" script ONLY. If I use the EXPORT command they become resident in the environment and are visible to all subsequent scripts. This is interesting to me as it "almost" seems contradictory. In most computer languages, when you call a subroutine, the variable is typically visible to the subroutine. But I have to keep reminding myself that Linux runs each script within their own process, the variables aren't visible between the processes whereas in programming languages, both the "calling" function and "called" are contained within the same process. The only way to make the variables visible between the two processes is to put them in a common area that is accessible to both routines....i.e. the environment.

Yes, my text would suggest that the transient exported variables are destroyed when the session is shutdown. If I want to retain the values and keep them resident, I have to add them to the startup scripts. I've seen this done in various other OS systems: VMS; DOS; so it seems to be a common theme however, I'm not so sure about the indirect method. That seems to somewhat unique?


Thanks again,


mike
 
The only way to make the variables visible between the two processes is to put them in a common area that is accessible to both routines....i.e. the environment.
Be aware that each process has its own environment. Among other things, this means that a child cannot change variables in its parent's environment. So it's not exactly a common area.

I'm not so sure about the indirect method. That seems to somewhat unique?
I don't think indirect expansion is very commonly used at all in shell scripting. You might think of an "indirect" variable as a variable whose content is another variable. Indirect expansion might get used wherever that idea makes sense.

Until this posting, I didn't realize I was looking for the term "indirect".
It's a pretty important term in computer science.

This is probably misquoted, but...

Any problem in computer science can be solved by adding a level of indirection.
-- Butler Lampson
 
Interesting. I was under the impression that there was one common environment. I think you're implying that one master enviroment that is used as a "template" and each process gets a copy of the template to use ? This helps to explain how Linux can have multiple users sharing the same processor. Whereas in the DOS world, it only has one shared environment?

Mr.Butler's observation is interesting and makes sense. This sort-of ties in the idea of "abrstraction" commonly used in OOP. It seems like abrstraction is just another means of indirection?
 
I think you're implying that one master enviroment that is used as a "template" and each process gets a copy of the template to use ?
Sort of. Normally, each time a process is created, it inherits its parent's environment. A parent can also choose to start its child with a modified environment. So the "template" is really just the parent's environment.


Whereas in the DOS world, it only has one shared environment?
I'm not sure on that one.


It seems like abrstraction is just another means of indirection?
I'd agree with that statement. I consider abstraction to be a form of "logical" or "representational" indirection.
 
In DOS there is only one enviroment, because there is only one process. In Windows, there is the registry that hangs in the backgound like an enviroment, and is used much the same way (or so I've been told).

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top