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?
LVL 35
YZlatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
If everything is configured properly on your system (A correct /etc/oratab file):

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sdstuberCommented:
grep $ORACLE_SID /etc/oratab
0
slightwv (䄆 Netminder) Commented:
After the grep you would still have to parse the line.

Why not just let oraenv take care of it for you?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sdstuberCommented:
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
0
johnsoneSenior Oracle DBACommented:
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.
0
sdstuberCommented:
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
0
sdstuberCommented:
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
0
slightwv (䄆 Netminder) Commented:
>>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?
0
sdstuberCommented:
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

0
johnsoneSenior Oracle DBACommented:
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.
0
sdstuberCommented:
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.
0
sdstuberCommented:
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.
0
YZlatAuthor Commented:
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?
0
sdstuberCommented:
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.
0
slightwv (䄆 Netminder) Commented:
>>that should have been "echo $ORACLE_HOME"

Thanks.  Yes.  It was a typo.
0
YZlatAuthor Commented:
actually I ound the issue - it should have been:

export ORACLE_HOME

without the $. Now it works

thanks all!
0
YZlatAuthor Commented:
Thanks!
0
slightwv (䄆 Netminder) Commented:
>>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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.