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?
Shell ScriptingOracle DatabaseUnix OS

Avatar of undefined
Last Comment
slightwv (䄆 Netminder)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Sean Stuber

grep $ORACLE_SID /etc/oratab
slightwv (䄆 Netminder)

After the grep you would still have to parse the line.

Why not just let oraenv take care of it for you?
Sean Stuber

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
johnsone

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.
Sean Stuber

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
Sean Stuber

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
slightwv (䄆 Netminder)

>>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?
Sean Stuber

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

johnsone

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Sean Stuber

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.
Sean Stuber

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.
ASKER
YZlat

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sean Stuber

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.
slightwv (䄆 Netminder)

>>that should have been "echo $ORACLE_HOME"

Thanks.  Yes.  It was a typo.
ASKER
YZlat

actually I ound the issue - it should have been:

export ORACLE_HOME

without the $. Now it works

thanks all!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
YZlat

Thanks!
slightwv (䄆 Netminder)

>>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.