LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-19-2005, 10:58 AM   #1
Elec490
LQ Newbie
 
Registered: Apr 2005
Posts: 13

Rep: Reputation: 0
Unhappy 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
 
Old 08-19-2005, 11:11 AM   #2
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 31
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.
 
Old 08-20-2005, 01:29 PM   #3
Elec490
LQ Newbie
 
Registered: Apr 2005
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 08-20-2005, 02:04 PM   #4
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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).
 
Old 08-20-2005, 02:37 PM   #5
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 31
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.
 
Old 08-20-2005, 08:01 PM   #6
Elec490
LQ Newbie
 
Registered: Apr 2005
Posts: 13

Original Poster
Rep: Reputation: 0
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?
 
Old 08-21-2005, 01:11 AM   #7
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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
 
Old 08-21-2005, 09:13 AM   #8
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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
 
Old 08-22-2005, 09:10 AM   #9
Elec490
LQ Newbie
 
Registered: Apr 2005
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
  


Reply



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
Do Perl functions usually modify variables, or return modified copies? Why? johnMG Programming 3 02-06-2005 10:22 PM
Is it possible to return variables from a script? pedrosan Linux - Newbie 6 04-20-2004 05:51 AM
Exporting variables through scripts rammya Programming 1 04-14-2004 10:24 AM
php scripts return empty pages ararag Linux - Software 1 02-23-2004 10:46 AM
Environment Variables in Scripts louisb Linux - General 3 01-15-2004 04:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:41 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration