Find the answer to your Linux question:
Results 1 to 9 of 9
I'm completely stumped as to how to do this. I have an Expect script which spawns a Korn Shell script. The ksh script exports all sorts of variables which I ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    5

    Retaining Exported Variables from a Spawned Script Inside of an Expect Script

    I'm completely stumped as to how to do this. I have an Expect script which spawns a Korn Shell script. The ksh script exports all sorts of variables which I would normally have access to following the completion of the script on the command line, however, because it's spawned from the Expect script, it seems everything is lost.

    Example. If I execute this Expect script from the command line:
    Code:
    #!/usr/bin/expect
    
    spawn ksh_script
    Code:
    #!/usr/bin/ksh
    
    TMP_VAR="Today is warm"
    export TMP_VAR
    and then 'echo $TMP_VAR', it's not set.

    Is there a way in in the Expect script to "capture" or somehow tell it to retain any new environment variables from a spawned process? I can't use exec or system which might do what I'm looking for in the Expect script because the ksh script is interactive.

    Any help would be greatly appreciated.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try to export the variable in your expect script.

    Regards

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Posts
    5
    Code:
    #!/usr/bin/expect
    
    spawn ksh_script
    
    parray env
    TMP_VAR isn't in the list and the variable $TMP_VAR doesn't exist. If I could get the Expect script to even see these exported variables in the ksh script I could then export them.

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Sorry I misunderstood your question, you can export a variable for the script you're calling but not visa versa. When you call a script, the script runs in another shell.

    Regards

  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Posts
    5
    Right, that's what it seams. Perhaps there's a different approach I might be able to take? Or a command in Expect that won't launch a new subprocess but instead within the same?

    I've never encountered an issue like this and it's a complete dead end.

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    A temporary file?

    Regards

  7. #7
    Just Joined!
    Join Date
    Jun 2007
    Posts
    5
    What do you mean?

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Write the value in the ksh script in a temporary file and read the value back with your expect script.

    Regards

  9. #9
    Just Joined!
    Join Date
    Jun 2007
    Posts
    5
    In the actual environment, the one Expect script spawns a ksh script which in turn sources and executes about four other ksh scripts, each of which export a ton variables. Changing all of the ksh scripts involved would be a chore. If I had that luxury, I would clean them all up and not have to write this Expect script in the first place

    :drown:

Posting Permissions

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