Weblog entry #73 for ajt
I often run a command and grep for something in the output list. One of the most common commands I use it history. After a while typing "history pipe grep foo " got a bit of a pain. So in collaboration with my LUG I came up with an alias, so I could do "h foo" and achieve the same. Yesterday I added some sorting to cut down the duplicates.
In case anyone wants it, it's shown below. Should work fine on any Bash like shell, I keep mine in my .bashrc.
function h() {
if [ -z "$1" ]
then
history | grep -v " h" | sed 's/[ \t]*$//' | sort -k 2 -r | uniq -f 1 | sort -n
else
history | grep -v " h" | grep $1 | sed 's/[ \t]*$//' | sort -k 2 -r | uniq -f 1 | sort -n
fi
}
Comments on this Entry
[ Send Message | View dkg's Scratchpad | View Weblogs ]
[ Parent | Reply to this comment ]
[ Send Message | View dkg's Scratchpad | View Weblogs ]
So, for example, if you use socat often to talk to/debug SMTP servers like this:
socat TCP4:servername:25 READLINE,history=/home/username/.smtp_histyou can actually use the same ctrl-r to recall SMTP inputs from previous sessions, which will be stored in /home/username/.smtp_hist.
Any other readline-enabled tool should have a similar capability in it too. Well-implemented libraries are wonderful things.
[ Parent | Reply to this comment ]
[ Send Message | View dkg's Scratchpad | View Weblogs ]
# make a TCP connection to a standard service, using readline to track your history:
# example usage:
# hsrv http www.example.com
# you can specify an alternate port by appending it:
# hsrv smtp mailserver.example.com 2525
# FIXME: which services besides smtp need the crnl option?
function hsrv() {
[ -d "$HOME/.histories" ] || mkdir "$HOME/.histories"
local extraopts=""
[ "smtp" == "$1" ] && extraopts=",crnl"
socat "READLINE,history=$HOME/.histories/$1$extraopts" "TCP4:$2:${3:-$1}"
}
[ Parent | Reply to this comment ]
[ Send Message | View dkg's Scratchpad | View Weblogs ]
# make a TCP connection to a standard service, using readline to track your history:
# example usage:
# hsrv http www.example.com
# you can specify an alternate port by appending it:
# hsrv smtp mailserver.example.com 2525
# FIXME: which services besides smtp and http need the crnl option?
function hsrv() {
[ -d "$HOME/.histories" ] || mkdir "$HOME/.histories"
local extraopts=""
( [ "smtp" == "$1" ] || [ "http" == "$1" ] ) && extraopts=",crnl"
socat "READLINE,history=$HOME/.histories/$1$extraopts" "TCP4:$2:${3:-$1}"
}
[ Parent | Reply to this comment ]
I can see the utility of your suggestion in most cases. I tend to use my h alias to see several commands at the same time to see variations all at once, but this is probably the minority of cases.
Our Red Hat and AIX boxen at work are a bit more antique and probably won't support this.
--
"It's Not Magic, It's Work"
Adam
[ Parent | Reply to this comment ]
[ Send Message | View dkg's Scratchpad | View Weblogs ]
It works in psql, sqlite, and mysql (command-line db clients for postgres, sqlite, and MySQL, respectively), octave, trac-admin, and probably many other places.
[ Parent | Reply to this comment ]
For those apps that _don't_ have a similar feature, but you'd like to have it, there's a tool called "rlwrap" that is supposed to do just that -- I haven't tried it yet as it but it's on my todo list.
Cheers.
[ Parent | Reply to this comment ]
I remember having seen this tip before but I forgot it...
[ Parent | Reply to this comment ]