Two answers. First is what I've done before (except I did it with db2profile):

unless (exists $ENV{ORACLE_HOME}) { exec '/bin/sh', '-c', ". /oracle/oracle-env.sh; exec $^X $0 @ARGV"; }
This reads the shell script, then reruns yourself. Hopefully this time ORACLE_HOME will be set, then you won't loop. (Although if you don't know ORACLE_HOME, how do you know which oracle-env.sh to load?)

The second, and generally more usable, option is to mandate that the user runs the environment before running your code. Which is exactly the same as any shell or binary command - you need to set ORACLE_HOME to find Oracle and load libraries and stuff, and this is usually set, I imagine, via sourcing the environment file.

This is actually usable because you work just like everything else. Users will have to put this in their profile or bashrc or whatever anyway, so just take advantage of it. You won't go sourcing the wrong environment if there is more than one copy installed. And you won't go using some default which may surprise the user expecting something else.

Your shell script wrapper would work because running the shell script command (which hopefully has a different name than the perl script) implies the oracle environment that it is setting up. Although I'd make a small modification:

if [ -z "$ORACLE_HOME" ] then . /oracle/oracle-env.sh fi
That way you don't go reloading a file you don't need to. Although if the user already has a different oracle environment set up, you won't use the default one which may be bad - up to you to decide.

As for reading and parsing the shell scripts - that's not easy to do. For example, the db2profile will go and source another script (sqllib/userprofile), oracle-env.sh could do something similar. Then you have to go and follow other scripts to a very deep level. And nevermind that shell can easily call other code to do things, such as LIBPATH=`oracle_setlibpath $LIBPATH`. That can just get annoying to parse. Best to let the shell do that, and concentrate on the perl. ;-)


In reply to Re: Perl: Source shell script to for environment variables? by Tanktalus
in thread Perl: Source shell script to for environment variables? by Gigglesworth

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.