Results 1 to 8 of 8
So Im trying to assign variables in the script child.sh and return their value to parent.sh but when I enter child.sh I never hit the next line in parent.sh. Is ...
- 08-19-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 14
Calling a ksh script from within a ksh script
So Im trying to assign variables in the script child.sh and return their value to parent.sh but when I enter child.sh I never hit the next line in parent.sh. Is something wrong with my thinking/syntax here?
Code:<child.sh> #!/bin/ksh test1() { a=foo b=bar export a b } case "$1" in test1) test1 ;; esac </child.sh> <parent.sh> #!/bin/ksh . ./child.sh test1 c=$a$b </parent.sh>Last edited by penguino888; 08-19-2008 at 07:00 PM. Reason: Added functions
- 08-19-2008 #2
You forgot the "sha" in the "shabang":
Code:<child.sh> #!/bin/ksh a=foo b=bar export a b </child.sh> <parent.sh> #!/bin/ksh . ./child.sh c=$a$b </parent.sh>
- 08-19-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 14
- 08-19-2008 #4
Then what's the problem? That code works fine for me. I added one minor change:
and got the following output:Code:#!/bin/ksh . ./child.sh c=$a$b echo $c
Code:[root@venus ~]# ./parent.sh foobar
- 08-19-2008 #5Just Joined!
- Join Date
- May 2008
- Posts
- 14
Ok my quick and dirty example was too dirty, this is more representative. The problem is 'a' and 'b' are never making it out of the function test1.
EDITED Original Code.
- 08-19-2008 #6
In the future, don't go back to your original post and edit the code, just put it in your next post so that someone reading the thread can understand your progress.
Can you give us a little more information about the script? What are you trying to accomplish? How are you calling the script?
I still don't see much wrong:
Code:[root@venus ~]# ./parent.sh test1 foobar [root@venus ~]# ./parent.sh blah [root@venus ~]#
- 08-19-2008 #7Just Joined!
- Join Date
- May 2008
- Posts
- 14
Arghhh!!! I found the problem, it was the line in bold below that was causing it, exit 0 returns and wipes out all my variables I suppose.
<child.sh>
#!/bin/ksh
test1()
{
a=foo
b=bar
export a b
}
case "$1" in
test1)
test1
;;
esac
exit 0
</child.sh>
<parent.sh>
#!/bin/ksh
. ./child.sh test1
c=$a$b
</parent.sh>
--------------------------------------------------------------------------------
Last edited by penguino888; 27 Minutes Ago at 07:00 PM
- 08-19-2008 #8
Yup, that would do it. Glad you figured it out.
As good practice you should include a default case in your case statement for erroneous input. Like:
Code:case "$1" in test1) test1 ;; test2) test2 ;; *) exit 0 ;; esac


Reply With Quote
