ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
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).
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.
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
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.