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.


In reply to Re: How to change a script's environment after the script is already run, based on shell sourcing ? by sgifford
in thread How to change a script's environment after the script is already run, based on shell sourcing ? by ronbarak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.