How to create history file for different user?

If you use SSH to login using your own user account and su to oracle or su directly to oracle then different users would share the same history file. If you want each user to have it’s own history file using the snippet of script given below one can create different shell history file for each user.

The output may vary for tty and who from different flavors of Unix/Linux but can easily to customized to the environment.

Note: For KSH (Korn shell) the env HISTFILE needs to set in the beginning of the script else it doesn’t take into effect and default .sh_history is used even though value set for HISTFILE.

For KSH(Korn):

# find TTY connected to and remove /dev/
mytty=`tty | sed ‘s/\/dev\///’`

# this will work if you login with your own user and su to oracle
# find the username based on the tty connected
shuser=`who | grep “$mytty” | awk ‘{print $1}’`

# if you directly su to oracle then and if you always connect from the same host you can use the hostname to postfix the history file name instead
# example: calora6db01q:/home/oracle $ who
# ajaffer pts/1 Jan 08 12:36 myhostname
# shuser=`who | grep “$mytty” | awk ‘{print $6}’`

# if tty found then set HISTFILE
if [[ “$shuser” != “” ]]; then
export HISTFILE=$HOME/.sh_history.$shuser

HISTSIZE=100 # sets to number of commands to remember in the command history
HISTFILESIZE=1000 # sets maximum number of lines to store in the history file

History commands
$ fc -l 10 20 # display commands 10 through 20
$ fc -l -5 # display last 5 commands
$ fc -l ls # shows the last command beginning with ls
$ fc -e vi 5 10 # starts vi with commands from 5 – 10

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.