in reply to How to change a script's environment after the script is already run, based on shell sourcing ?

Here's a more flexible way, which will work even if the environment file contains shell commands:
#!/usr/bin/perl print "Starting...\n"; use constant ENVSCRIPT => '/etc/profiles/bashrc.ora.v.10'; if (!$ENV{ORACLE_HOME} && !$ENV{SKIP_ENVSCRIPT}) { $ENV{SKIP_ENVSCRIPT}=1; exec("/bin/sh -c '. @{[ENVSCRIPT]}; exec $0'"); } print "ORACLE_HOME=$ENV{ORACLE_HOME}\n";

If $ENV{ORACLE_HOME} is unset, it runs a copy of the shell to first source the environment file, then re-execute your script. $ENV{SKIP_ENVSCRIPT} keeps this from looping if there are problems.

One caveat is that if the script is run like perl your_script.pl the exec may not work. If that's how you normally call your script, you should be able to adjust the shell line to take that into account.

  • Comment on Re: How to change a script's environment after the script is already run, based on shell sourcing ?
  • Select or Download Code