Humble Trader

Saturday, December 31, 2005

chora - Switching between Oracle databases

Introduction:

This code goes into a user's .bash_profile file. This file is normally hidden from a directory listing (any filename starting with a '.' is normally hidden), but can be seen by typing the command; 'ls -al', or just 'll'.

This creates the shell function; 'chora'. The advantage of a function over a local script is that it runs in the current shell and, therefore, doesn't have to be called like this; '. ./chora'.

There are 2 types of parameter for this function:

'?' - This reports the current SID and lists all available SIDs.
SID - Calling 'chora SID' swithes the user's environment to the new SID if it exists.

When the user logs in, this reports availability.

Aim:

Give users a quick and simple way of switching between SIDs.

Requirements:

Obviously, this only operates in an Oracle environment.

Procedure:

  • Log in as the user.
  • Open Terminal.
  • # cd
  • # vi .bash_profile
  • Add the following lines. They would normally go somewhere between a line that sets the initial Oracle SID (e.g. 'export ORACLE_SID...'), and the umask line.:
    • Best; Download here.
    • Or; Cut and paste the lines of code between the dotted lines:
...
export ORACLE_SID=`grep Y$ /etc/oratab | cut -d: -f1 | head -1`

# ------------------------------ chora --------------------------------
chora () {
if [ "$1" = "?" -o "$1" = "" ]
then
echo
echo "Oracle SID is currently <$ORACLE_SID>"
echo
echo " Available:"
for sid in `grep Y$ /etc/oratab | cut -d: -f1`
do
echo " <$sid>"
done
else
if [ `grep Y$ /etc/oratab | cut -d: -f1 | grep $1` ]
then
export ORACLE_SID=$1
echo
echo "Oracle SID changed to <$ORACLE_SID>"
else
echo
echo "No such SID."
fi
fi
echo
}

# If not on a terminal then don't output report.
if [ "`ps -ef | grep ^$USER | grep 'grep grep' | grep pts`" != "" ]
then
clear
echo
echo "Oracle SID switcher:"
echo " chora "
echo
echo " Available:"
for sid in `grep Y$ /etc/oratab | cut -d: -f1`
do
echo " <$sid>"
done
echo
echo " Currently:"
echo " <$ORACLE_SID>"
echo
fi
# ------------------------------ chora --------------------------------

umask 022
...

Testing:

This will demonstrate that the function is available but won't switch SIDs until databases have been built:
  • Source .bash_profile:
    • $ . ./.bash_profile
    • This returns with either a list of SIDs or an error message; 'grep: /etc/oratab: No such file or directory' if there are no databases installed.
  • Run chora in query mode:
    • $ chora ?
    • This returns with either a list of SIDs or an error message; 'grep: /etc/oratab: No such file or directory' if there are no databases installed.

0 Comments:

Post a Comment

<< Home