Return variables in scripts
HI guys,
I am doing the following: - In my script (bourne) I call a second script which does some work and set a variable to a certain value. I want to be able to access this variable in the first script. How do I "export" it? How do I access it? I am sure this is a piece of cake for the experts of scripting? :-) Tx, Bye Elec490 |
At the bottom of your second script - set the following:
Code:
return $variable_name; In the parent script - just define that a $var = a system call to the second script. This will populate $var with whatever the second script returns. |
Hi Penguin,
First of all tnx for your reply.. I am doing this.. Script1 #!/bin/sh name=exec script2.sh echo $name Script2 #!/bin/sh n="script2" return $n Here the output: ./script2.sh: cannot return when not in function Am I obliged to return the value in a fn? Wouldnt this return the value from the fn to the second script instead? Thanks in advance, Elec |
Quote:
script1.sh Code:
#!/bin/sh Code:
#!/bin/sh |
I was under the impression you would be using a function.
For something this simple - and being called in that fashion - simply echo your variable to <STDOUT> when the script completes - and that will populate the variable in script1. |
Hi guys,
tnx I was able to retrieve the variable..BUT doing echo $n without the -n option.and using the ``to call the script... What if I want to retrieve two variables? |
Quote:
Code:
values=`./script2.sh` |
That's okay if you're dealing with the values separately, but if you need them both at once use:
Code:
./script2.sh | while read value1 value2 |
Hi guys,
tnx for your help. Hi used eddie solution and it is fine..even if there is the little annoyance that the variables are only usable locally in the loop.. Tnx a lot! Cheers, Chris |
All times are GMT -5. The time now is 02:59 AM. |