UNIX One-Liners: Some Useful Shell Programs
My Favorite One-Liners: Some Useful Shell Programs… But first things first… I would like to note that this aint my list of favorite shell programs.. well, it has become… but what I mean is this aint an original article that I created and posted here.. this is a section from the now defunt, Sysadmin Magazine… and you’ll be reading a lot of those here….
“Keep It Simple, SysAdmin,” or perhaps just “Keep It Shell Script”, that is one of the motto that every sysdamin knows… They automate, they script, they relax.
Too many system administrators, especially former or frustrated programmers, get involved and distracted creating ad hoc programs that grow until they have a life and a following of their own. These programs generally are written in C, awk, or C shell, have no documentation, and are incomprehensible, even to the author, less than two weeks after being finished.
While it’s no cure-all for bad programming practices, I prefer to write simple shell scripts for system administration work. How simple are they? Simple enough that their inner workings should be almost obvious; tricky programs are fine for impressing your friends but hard to understand when the system has to be restored at 3:00 A.M. And of course, the C shell is so much more elegant, but not every system has one, which means that C shell programs aren’t portable. What price elegance?
My own pride is in writing programs that can be easily understood, even if they aren’t perfect. In fact, I’m sure I will get at least five pieces of email for each of these programs, showing how they can be improved. If you like being tricky, find ingenious things to do with your programs after you write them. If these programs are beyond you, don’t despair: but don’t fail to read the manual pages for the shell and every command you don’t understand. Meanwhile, they don’t get much simpler than Tail:
for i in $*
do
echo “=========” $i “==========”
tail $i
done
Tail emulates the behavior of the head command when presented with multiple files, instead of the binary tail’s default behavior of just showing you the tail of one file. The simple for-do-done loop executes the echo and tail commands repeatedly on whatever files you specify on the command line.
Take Four, They’re Small
While we’re on incredibly simple programs, how many times have you had to double-space a file and forgotten how? It happened to me every time, until I wrote double:
pr -dt $*
See? Most of the work is just picking a good name for the program. In the same vein, here’s another self-documenting program called crtolf:
tr “