1826443 Members
3977 Online
109692 Solutions
New Discussion

Re: enable History file

 
SOLVED
Go to solution
HDgio
Regular Advisor

enable History file

Hello to all
Someone knows like qualifying history file on a shell sh?
16 REPLIES 16
Pete Randall
Outstanding Contributor
Solution

Re: enable History file

In your profile, put something similar to this:

HISTFILE=/tmp/.sh_history.$(whoami)
export HISTFILE

Then enable it with "set -o vi"



Pete

Pete
Graham Cameron_1
Honored Contributor

Re: enable History file

This comes up quite often.
See

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=59366

Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Madhu Sudhan_1
Respected Contributor

Re: enable History file

The real Bourne Shell doesn't support history.

-Madhu
Think Positive
Stefan Farrelly
Honored Contributor

Re: enable History file

We use this;

HISTFILE=${HOME}/.history_files/.hist.`who am i|awk '{print $1}'`.$$.`date +%d%m%y.%H%M`
HISTSIZE=1000

in our .profile so we get a unique history file for each user by date for auditing purposes.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Pete Randall
Outstanding Contributor

Re: enable History file

?

Pete
Bill Hassell
Honored Contributor

Re: enable History file

Yes, the Bourne shell does not have a history feature. However, no one uses the Bourne shell because it is located in a very obscure location (/usr/old/bin/sh). Unfortunately, the HP POSIX shell is called sh too so it is easy to be confused by the name. The file /usr/bin/sh (and the archived library file /sbin/sh) are both POSIX shells (like ksh) and have a history file available.

To enable the history file, the env variable HISTFILE (and optionally HISTSIZE) must be set at login. This is best done in /etc/profile for 'normal' logins.


Bill Hassell, sysadmin
Madhu Sudhan_1
Respected Contributor

Re: enable History file

Thanks Bill. I couldn't explain it so clear.

Thanks,
Madhu
Think Positive
Geoff Wild
Honored Contributor

Re: enable History file

Here's a cool thing to do if you have multiple admins who su to root.

In root's .profile, add:

# Set up logging
HISTFILE=${HOME}/.sh_history_`who am i|awk '{ print $1}'`
date >>$HISTFILE
export HISTFILE
HISTSIZE=500
export HISTSIZE

This creates a seperate .sh_history file for each admin - for example, mine is:

.sh_history_gwild

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
HDgio
Regular Advisor

Re: enable History file

After what I've configured the .profile for ebnable a history file, how I can recall a line command ?

es:fc -l
134 pwd
135 ls
136 ll

for recall the ls command?
!ls
john korterman
Honored Contributor

Re: enable History file

Hi,
if it is C-shell, try the exclamation mark followed by the command number, e.g.:
# !135

regards,
John K.
it would be nice if you always got a second chance
Chris Wilshaw
Honored Contributor

Re: enable History file

Other shells need you to have the EDITOR variable assigned

eg: EDITOR=vi

You can then use ESC followed by - to move back through your history, and ESC + to move forwards.

Alternatively, you can run the shell with options

eg:

ksh -o vi
Geoff Wild
Honored Contributor

Re: enable History file

If you are in posix shell:

add:

set -o vi

To your .profile

Then,

esc k

To recall history, keep prssing k to go back...or, better yet, because it is vi, use / to search - ie:

esc k
/ls

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Zigor Buruaga
Esteemed Contributor

Re: enable History file

Hi,

es:fc -l
134 pwd
135 ls
136 ll

Try with:
# r 135

HTH
Kind regards,
Zigor
HDgio
Regular Advisor

Re: enable History file

Thanks to all
Graham Cameron_1
Honored Contributor

Re: enable History file

"r ls" would also have done that, but far more useful is to bring back commands and then edit them.
Instead of "fc -l", press escape, and then "k", or "-" to scroll back through your commands, editing and resubmitting.

Refer to the Command Re-entry section of "man ksh"

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Bill Hassell
Honored Contributor

Re: enable History file

As mentioned, the editor should be defined for a number of reasons:

export EDITOR=/usr/bion/vi

Then ESC k will recall the last command. ESC / will allow a search as in: ESC /abc which recalls the last command with abc in it. The man page for the POSIX shell is man sh-posix but the command recall feature is identical in ksh. A nice alias in the POSIX shells (sh and ksh) is history (an exported alias for fc -l)


Bill Hassell, sysadmin