Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

asked on

Best way to retrieve ORACLE_HOME with scripting

What is the best way to retrieve ORACLE_HOME directory with ksh scripting, given ORACLE_SID value?
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sean Stuber
Sean Stuber

grep $ORACLE_SID /etc/oratab
After the grep you would still have to parse the line.

Why not just let oraenv take care of it for you?
grep $ORACLE_SID /etc/oratab | awk -F: '{print $2}'


that's essentially all oraenv is going to do.  This way you can don't have to actually change the sid if you don't want to, like iterating through a list of sids
That only works for systems where oratab is in the default location and the default location is in /etc.  That doesn't hold for all *NIX systems.  Solaris is the first one that comes to mind.  The default location is /var/opt/oracle.  oraenv is a much safer way to go.
note, if your $ORACLE_SID is set because you're already using oraenv

you might not need to either of these steps  you can simply echo $ORACLE_HOME
my assumption was the asker knew where the file was.

also, none of these are completely reliable because oratab isn't required, it's simply common.
if it's not populated then oraenv will fail too, regardless of where any one specific platform puts it
>>completely reliable because oratab isn't required

It's been many years since I've been on a *nix platform but:  If you don't have an oratab wouldn't you then need to explicitly hard code ORACLE_HOME in the login scripts?
if your system supports ps eww you could look for one of the pmon processes and then search through the environment variables

for example...

$ ps -ef | grep pmon
oracle   11049     1  0 Jul18 ?        00:01:15 ora_pmon_MYDB

$ ps eww 11049 | awk -F"ORACLE_HOME=" '{print $2}' | awk '{print $1}'
/oracle/product/11.2.0

Open in new window

I guess my point is that if you are setting up oratab, then using oraenv is a much more platform independent way of getting the value.

Most places I have been have had a wide range of platforms and we tried to have a standard set of scripts that didn't need to be modified for each different platform.

We normally use an environment script that gets called when a user logs in.  It sets up the Oracle environment and set up a bunch of useful aliases, plus some useful variables.  As a general rule, we don't use oratab but we do typically put the entries in there anyway.  Scripts are free to call the environment setup scripts and get all the variables set for them.  The scripts were also written so that they would change the environment from one database to another, which makes them quite interesting when you have multiple versions on the same machine.
If you don't have an oratab wouldn't you then need to explicitly hard code ORACLE_HOME in the login scripts?

yes or in some kind of setup script.

I used to work with a system where we had a set "go" scripts, one for each client.

"go-client"  would set oracle home and a bunch of other variables.
we never used oraenv there.
I guess my point is that if you are setting up oratab, then using oraenv is a much more platform independent way of getting the value.

It is more portable; but sort of trivially so since oraenv itself requires oratab.
And my point was to give the ability to do the lookup without also making a new assignment.
Which, if you use oraenv, you'll change your current sid/home.


Given the ksh requirement we can probably assume it is some flavor of unix which is good since other platforms (windows and vms) don't use oratab or oraenv

but then the ps suggestion would fail too.

also, off hand I'm not positive that would work going forward in 12c with pluggable databases.  There should be (I think, I'll have to verify) only one pmon for the cdb and none for the pdbs.
Avatar of YZlat

ASKER

can I use:

#!/bin/ksh
ORAENV_ASK=NO
export ORACLE_SID=ORCL
. oraenv
export $ORACLE_HOME

?

I tried and gotten an error "/u01/app/oracle/product/11.2.0 is not an identifier"

how can I get around that? Should I assign ORACLE_HOME to another variable and then export ORACLE_HOME? Is there a better way?
export $ORACLE_HOME


that should have been "echo $ORACLE_HOME"

As noted above, you won't simply be looking at the value, you'll be changing the value in this process.  That may or may not be acceptable for your usage.
>>that should have been "echo $ORACLE_HOME"

Thanks.  Yes.  It was a typo.
Avatar of YZlat

ASKER

actually I ound the issue - it should have been:

export ORACLE_HOME

without the $. Now it works

thanks all!
Avatar of YZlat

ASKER

Thanks!
>>the thing is if I run my script via crontab, ORACLE_HOME should be exlicitly exported

This is why Oracle provides the oratab file and oraenv script.

It is designed to set up all necessary environment variables based off of just an ORACLE_SID.