Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

  2. #2
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    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>

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    14
    Quote Originally Posted by Thrillhouse View Post
    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>
    Good point! The actual code has the "sha". Edited example.

  4. #4
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    Then what's the problem? That code works fine for me. I added one minor change:
    Code:
    #!/bin/ksh
    . ./child.sh
    c=$a$b
    echo $c
    and got the following output:
    Code:
    [root@venus ~]# ./parent.sh
    foobar

  5. #5
    Just 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.

  6. #6
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    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 ~]#

  7. #7
    Just 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

  8. #8
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...