Thursday, March 12, 2009

Changing Oracle Environment Variables Using A Bash Script

I have a Linux machine with three different Oracle versions installed. Oracle 9i, Oracle 10g and Oracle 11g. All have been installed under one Linux account. Here are some scripts to set the environment variables for the instance to be accessed.

Oracle 9i Environment: SID ora9
ORACLE_SID=ora9; export ORACLE_SID
ORACLE_BASE=/opt/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/9.2.0/db_1; export ORACLE_HOME
TNS_ADMIN=$ORACLE_HOME/network/admin; export TNS_ADMIN
LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH
ORACLE_OEM_JAVARUNTIME=/usr/local/jre1.3.1_19; export ORACLE_OEM_JAVARUNTIME

path=`echo $PATH | sed "s/:/ /g"`
NEWPATH=$ORACLE_HOME/bin
for dirname in $path
do
if [ `echo $dirname | grep oracle | grep bin | wc -l` -eq 0 ]
then
NEWPATH=`echo ${NEWPATH}:${dirname}`
fi
done
export PATH=$NEWPATH

echo
echo 'Environment variables for' $ORACLE_SID
echo '------------------------------'
echo 'ORACLE_SID='$ORACLE_SID
echo 'ORACLE_BASE='$ORACLE_BASE
echo 'ORACLE_HOME='$ORACLE_HOME
echo 'TNS_ADMIN='$TNS_ADMIN
echo 'LD_LIBRARY_PATH='$LD_LIBRARY_PATH
echo 'PATH='$PATH
echo '------------------------------'
echo


Oracle 10g Environment: SID ora10
ORACLE_SID=ora10; export ORACLE_SID
ORACLE_BASE=/opt/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1; export ORACLE_HOME
TNS_ADMIN=$ORACLE_HOME/network/admin; export TNS_ADMIN
LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH

path=`echo $PATH | sed "s/:/ /g"`
NEWPATH=$ORACLE_HOME/bin
for dirname in $path
do
if [ `echo $dirname | grep oracle | grep bin | wc -l` -eq 0 ]
then
NEWPATH=`echo ${NEWPATH}:${dirname}`
fi
done
export PATH=$NEWPATH

echo
echo 'Environment variables for' $ORACLE_SID
echo '------------------------------'
echo 'ORACLE_SID='$ORACLE_SID
echo 'ORACLE_BASE='$ORACLE_BASE
echo 'ORACLE_HOME='$ORACLE_HOME
echo 'TNS_ADMIN='$TNS_ADMIN
echo 'LD_LIBRARY_PATH='$LD_LIBRARY_PATH
echo 'PATH='$PATH
echo '------------------------------'
echo


Oracle 11g Environment: SID ora11
ORACLE_SID=ora11; export ORACLE_SID
ORACLE_BASE=/opt/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.1.0/db_1; export ORACLE_HOME
TNS_ADMIN=$ORACLE_HOME/network/admin; export TNS_ADMIN
LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH

path=`echo $PATH | sed "s/:/ /g"`
NEWPATH=$ORACLE_HOME/bin:$ORACLE_HOME/bin/OPatch
for dirname in $path
do
if [ `echo $dirname | grep oracle | grep bin | wc -l` -eq 0 ]
then
NEWPATH=`echo ${NEWPATH}:${dirname}`
fi
done
export PATH=$NEWPATH

echo
echo 'Environment variables for' $ORACLE_SID
echo '------------------------------'
echo 'ORACLE_SID='$ORACLE_SID
echo 'ORACLE_BASE='$ORACLE_BASE
echo 'ORACLE_HOME='$ORACLE_HOME
echo 'TNS_ADMIN='$TNS_ADMIN
echo 'LD_LIBRARY_PATH='$LD_LIBRARY_PATH
echo 'PATH='$PATH
echo '------------------------------'
echo


To run the scripts one must use the "source" command in order to set the environment variables in your own environment. Without running the script through source the environment variables will be set in the script's environment and not your own.

[oracle@oratest ~]$ source ora10.sh

Environment variables for ora10
------------------------------
ORACLE_SID=ora10
ORACLE_BASE=/opt/oracle
ORACLE_HOME=/opt/oracle/product/10.2.0/db_1
TNS_ADMIN=/opt/oracle/product/10.2.0/db_1/network/admin
LD_LIBRARY_PATH=/opt/oracle/product/10.2.0/db_1/lib
PATH=/opt/oracle/product/10.2.0/db_1/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin
------------------------------

[oracle@oratest ~]$

Another method is to create aliases to set the enviroment variables. The tricky part is the PATH. Specify a PATH_BASE variable to use when setting the new paths. Here's a sample bash_profile file. The ora10 instance will be the default, with ora9, ora10 and ora11 aliases to switch to different environments.

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs
umask 022

ORACLE_SID=ora10; export ORACLE_SID
ORACLE_BASE=/opt/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1; export ORACLE_HOME
TNS_ADMIN=$ORACLE_HOME/network/admin; export TNS_ADMIN
LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH
PATH_BASE=/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/oracle/bin; export PATH_BASE
PATH=$ORACLE_HOME/bin:$PATH:$HOME/bin; export PATH

alias ora9='ORACLE_SID=ora9; export ORACLE_SID;
ORACLE_HOME=$ORACLE_BASE/product/9.2.0/db_1; export ORACLE_HOME;
TNS_ADMIN=$ORACLE_HOME/network/admin; export TNS_ADMIN;
LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH;
ORACLE_OEM_JAVARUNTIME=/usr/local/jre1.3.1_19; export ORACLE_OEM_JAVARUNTIME;
PATH=$ORACLE_HOME/bin:$PATH_BASE; export PATH;
echo ORACLE_SID=$ORACLE_SID; echo ORACLE_HOME=$ORACLE_HOME'

alias ora10='ORACLE_SID=ora10; export ORACLE_SID;
ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1; export ORACLE_HOME;
TNS_ADMIN=$ORACLE_HOME/network/admin; export TNS_ADMIN;
LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH;
PATH=$ORACLE_HOME/bin:$PATH_BASE; export PATH;
echo ORACLE_SID=$ORACLE_SID; echo ORACLE_HOME=$ORACLE_HOME'

alias ora11='ORACLE_SID=ora11; export ORACLE_SID;
ORACLE_HOME=$ORACLE_BASE/product/11.1.0/db_1; export ORACLE_HOME;
TNS_ADMIN=$ORACLE_HOME/network/admin; export TNS_ADMIN;
LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH;
PATH=$ORACLE_HOME/bin:$PATH_BASE; export PATH;
echo ORACLE_SID=$ORACLE_SID; echo ORACLE_HOME=$ORACLE_HOME'

No comments:

Post a Comment