Posts Tagged ‘bash history’

Forgetfull command line

January 14, 2009

Problem:
I am lucky to work with a few people who’s knowledge of computing is vast. I am always learning new things one can do at the command line. Unfortunately once typed, their details have instantly left my mind. This is a source of great frustration both for me and them. It is not feasible to make notes on what worked and why at the time. When I next want to do it, guaranteed its not in my bash history.

Solution:
I have knocked up this python script that allows me to log the last command with and optional comment in a separate file to my history so that i can review the dust after it has settled:

pyhs:

#!/usr/bin/python
import sys

argc = len(sys.argv)

if not((argc == 2 ) or (argc == 3)):
print “usage is: history 2 | hs logfile “
sys.exit()

if argc == 3:
comment = ” #”+sys.argv[2]+’\n’
else:
comment=’\n’

l = sys.stdin.readline()
f = open(sys.argv[1],’a')
command = l.split(None,1)[1] # remove line number
command = command.rstrip(‘\n’) # remove trailing newline
f.writelines(command+comment)
f.close()

To use it, it is placed in my path ~/bin/pyhs
then in my .bashrc file the entry:

hs () { history 2 | pyhs ~/command_logs/rob “‘$1′” ;}

note the double-single….single-double quotes, these fix the argument passing….sort of . The inner quotes get passed maby someday ill fix this, but I doubt it.

Ps: Useage:

% cd spam
% ls
# ask somone a question
# get answer and try it
% mysql> GRANT ALL PRIVILEGES ON meat_db.* TO ‘lambchop@’localhost’ IDENTIFIED BY ‘ilikemooo’
#instantly forget the command – but its ok you can log it for later
% hs ‘how to changes the privaledges on the database’