05 November 2006

More useful shell aliases

This article will expand on my previous post, Useful shell aliases. All of the below was done with the bash shell on a FreeBSD system, but ought to be largely adaptable to other shells and systems.

One of the aliases I defined in the previous article was this one:

alias psa='ps auwx'

which shows the entire process table with my preferred options. But, often you're just looking for one or a few specific processes and it's a bother to pick through the full process listing. Therefore, I developed this alternative alias:

alias psg='ps auxw | grep '

Now, say if I want to just check if apache is running or how many apache processes are up, I just invoke this alias along with the regular expression that I wish to match, in this case "httpd", like this (with some shrunken, example output):

$ psg httpd
root 3870 0.0 0.3 6740 5564 ?? Ss 22Aug06 3:13.62 /usr/local/sbin/httpd
nobody 3871 0.0 0.3 6808 5640 ?? I 22Aug06 0:18.72 /usr/local/sbin/httpd
nobody 3872 0.0 0.3 6844 5652 ?? I 22Aug06 0:11.94 /usr/local/sbin/httpd
nobody 3873 0.0 0.3 6844 5652 ?? I 22Aug06 0:03.08 /usr/local/sbin/httpd
kace 6523 0.0 0.0 1448 856 p0 S+ 10:59PM 0:00.00 grep httpd

That alias is so simple, yet it is a great time saver that I use almost daily.

Many times, I will write and run long "for" loops at the command line or some other command that is long or complicated. At the time, maybe I didn't expect to be using it again. Or, maybe I've forgotten the IP address of a machine I logged into recently. This next alias can help with all of that. I call it "bash history grep".

alias bhg='cat $HISTFILE | grep '

You just have to think of a good, specific match pattern to find the old command that you are thinking of. Like a go0gle search, you may have to refine your search and try again. :) And, if you've forgotten an IP address you might try "bhg ssh". If that turns out to give too much output, then try "bhg ssh | sort | uniq " to eliminate duplicates or maybe "bhg 'ssh 1' " if you remember the first digit of the address is '1'. I could hardly make it through the day without this alias. If you're like me and you work from the command line alot and find "bhg" to be useful, then you'll almost certainly want to change the HISTFILESIZE environment variable in your .bash_profile and make it much larger.

This next one I call "sc" for sans comments. (I orginally called it "nc" but that conflicted with the wonderful netcat utility, which now appears in the base system, BTW.) This is another one of my favorites. Please note that between the square brackets below is exactly one space and one tab! (... You can't cut and paste this one, I'm afraid.)

alias sc="grep -v '^[ ]*#\|^[ ]*$' "

What this one does is to use grep to ignore (-v) any lines that are empty, only whitespace, only comments, or some combination of those things. It is fantastic for reading various configuration files and various scripts as well. Many of these files have a lot of comments, which is fine, but sometimes you know what you're looking for and you don't have time to sift through pages of comments (cough! httpd.conf). Yet, you want to leave those comments where they are for other times when you need them.

Here is just one more, a kind of fun one. I don't get far in the morning without some hot tea. But, what happens is I bring the tea back to my desk and start on something and forget to watch the clock and remove the teabag from the hot water. This risks over-steeping and bitterness. Hence, this little alarm alias:

alias beep="echo -ne '\a' ; sleep 0.1 ; echo -ne '\a' ; sleep 0.1 ;echo -ne '\a' "

When I pour my tea I check my watch and then I check my watch again as soon as I get back to my desk. Then I run a sleep with the remaining steeping time followed by the beep (say "sleep 180 ; beep "). The beep is, as you can see, really three quick beeps, which helps distinguish it from the various actual system beeps that may be going on at any time in your busy day. Enjoy your tea.

[ tags: , , , , , , , , ]



Post a Comment

<< Home