Friday, January 9, 2009

Oracle bash and ksh prompt

Purpose:
Colect different bash promts to adecuate our Oracle terminal session.

Version: Unix bash/ksh

Bash prompt customization
After a user login to the system, user environment variables are initialized from various files like:

* global system files /etc/profile or /etc/bashrc
* user files ~/.bash_profile , ~/.bash_login , ~/.profile , ~/.bashrc or ~/.bash_logout.

It is important to know that all users environment variable have a life time equal to the terminal session. When the terminal session is closed the user's variables including bash shell variables defined during a terminal session are emptied and a again redefined when new terminal session is created either via logon in shell or interactive shell.

Note: Use "source" command to re-read a bash initialization .bashrc file instead of restarting a terminal session.

source .bashrc

or similarly

. .bashrc

source .bash_profile
. .bash_profile

source .profile
. .profile


Bash and ksh prompt Examples

Display current user, host, working dir, hour, Oracle versiĆ³n and SID
Working on multi-instance and different Oracle versions environments we'd need to know the working directory, ORACLE SID and other usefull data and keep the entire command line free, specially with long paths. Furthermore, we can use the promt to separate command outputs. (adapted from http://www.ludovicocaldara.net/dba/tips-bash-prompt-and-oracle)

Over bash
.bash_profile or .profile
---------------------------------------------------------------------
ohvers ()
{
echo -n $ORACLE_HOME |sed -n 's/.*\///p'
}
export PS1=$'\\n# [ \u@\h:\w [\\t] [`ohvers` SID:${ORACLE_SID:-"no sid"}] ]\\n# '

---------------------------------------------------------------------
Note: could be that way if prefered...
export PS1=$'\\n# [ $LOGNAME@\h:$PWD [\\t] [`ohvers` SID:${ORACLE_SID:-"no sid"}] ]\\n# '

output:



Display hostname, working directory and the history number of commands
We'd want to know hostname and history # of executed commands.

Over ksh
.profile
---------------------------------------------------------------------
export PS1="`uname -n`$[!]$ "---------------------------------------------------------------------
output:
tauro$[8]$

W/working directory
---------------------------------------------------------------------
export PS1=`uname -n`@`whoami`':$PWD [!] $'
---------------------------------------------------------------------
output:
tauro@oracle:/oracle [2] $

Over bash
.bash_profile
---------------------------------------------------------------------
export PS1=`uname -n`@`whoami`':$PWD [\!] $'
---------------------------------------------------------------------
output:
tauro@oracle:/oracle/product/10.2.0/bin [824] $

Counting Files in the Current Directory
This bash prompt displays current number of files and directories in the current directory.

---------------------------------------------------------------------
export PS1="\u@\h [\$(ls | wc -l)]:\$ "
---------------------------------------------------------------------
output:
oracle@tauro [334]:$ pwd
/oracle/product/10.2.0/bin
oracle@tauro [334]:$

Links, References:
http://www.linuxconfig.org/Bash_prompt_basics
Unix man pages: bash/ksh
http://www.ludovicocaldara.net/dba/tips-bash-prompt-and-oracle