in reply to Perl: Source shell script to for environment variables?
Two answers. First is what I've done before (except I did it with db2profile):
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?)unless (exists $ENV{ORACLE_HOME}) { exec '/bin/sh', '-c', ". /oracle/oracle-env.sh; exec $^X $0 @ARGV"; }
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:
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.if [ -z "$ORACLE_HOME" ] then . /oracle/oracle-env.sh fi
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. ;-)
|
|---|