LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Return variables in scripts (https://www.linuxquestions.org/questions/programming-9/return-variables-in-scripts-354908/)

Elec490 08-19-2005 10:58 AM

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

PenguinPwrdBox 08-19-2005 11:11 AM

At the bottom of your second script - set the following:

Code:

return $variable_name;
What this will do, is instead of outputting an exit code (such as 0 for success) - it will return your value - num, string, whatever.

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.

Elec490 08-20-2005 01:29 PM

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

carl.waldbieser 08-20-2005 02:04 PM

Quote:

Originally posted by Elec490
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

OK, try doing this:

script1.sh
Code:

#!/bin/sh
 
name=`./script2.sh`
 
echo $name

script2.sh
Code:

#!/bin/sh
 
n="script2"
echo -n $n

As far as I know, you can only return an integer as an exit status. The typical shell-script way I have observed for passing info from a child script to a parent script is to have the child output the info on the standard output stream and have the parent collect this information. For the Bourne Shell (sh), you can use the backticks (`) to do this. This also works in bash, but I prefer the $(command) syntax because it seems to be a bit more clear (people sometimes mistake backticks for single quotes).

PenguinPwrdBox 08-20-2005 02:37 PM

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.

Elec490 08-20-2005 08:01 PM

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?

carl.waldbieser 08-21-2005 01:11 AM

Quote:

Originally posted by Elec490
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?

You just need to encode them in the standard output stream in such a way that you can interpret them. For example, if you want to return values like "foo" and "bar" with no whitespace, you could output "foo bar" in the child script. The parent script could then:
Code:

values=`./script2.sh`
for value in $values
do
  #code to process here
done


eddiebaby1023 08-21-2005 09:13 AM

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
do
    #  Some stuff here
    echo "value1 is \"$value1\", value2 is \"$value2\""
done


Elec490 08-22-2005 09:10 AM

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.