Our first example is a simplified version of the shell program called fols. It lists the names of your MH folders in columns. The program has just one line:
folders -fast -recurse $* | pr -l1 -4 -w78 -tHere's a comparison of fols and the standard folders -fast -recurse. Besides being easier to type, fols uses less space on the screen.
% folders -fast -recurse drafts drafts/DELETE inbox inbox/DELETE mh-users_ADD mh-users_ADD/DELETE mh-workers_ADD reference reference/DELETE % fols drafts drafts/DELETE inbox inbox/DELETE mh-users_ADD mh-users_ADD/DELET mh-workers_ADD reference reference/DELETEYou can see that the end of one of the subfolder names is cut off. There are a few ways to fix that -- you'll need to add another line or two to the shell program. But this is an introduction, so let's skip that for now. (See the improved fols in the Section Explanation of fols.)
Our second program, incs, uses inc to incorporate new mail messages, then uses show to display them all. If there were no messages, it doesn't run show:
% incs inc: no mail to incorporate ...Later, after some mail comes in... % incs Incorporating new mail into inbox... 45+ 02/04 boris@kremvax.kgb UNIX question<<Comrade Emma, I ha 46 02/05 Jim Bob Smith Re: Encapsulation destroying good (Message inbox:45) From: boris@kremvax.kgb.su To: ehuser@mysun.xyz.edu ...incs uses some more advanced features of the shell, but it's still pretty simple. It looks like this:
% cat incs #! /bin/sh # $Id: incs,v 2.0 92/08/02 18:45:50 jerry book2 $ ### incs - incorporate messages, then show them ### Usage: incs [+folder] [-inc options] # # incs DOES AN inc, THEN A show OF ALL MESSAGES inc'D. IF YOU # SET THE ENVIRONMENT VARIABLE $INCSHOW TO THE NAME OF A PROGRAM # THEN incs WILL USE THAT PROGRAM INSTEAD OF show. temp=/tmp/INCS$$ stat=1 # DEFAULT EXIT STATUS; RESET TO 0 ON NORMAL EXIT trap 'rm -f $temp; exit $stat' 0 1 2 15 # ONLY SHOW MESSAGE IF inc ACTUALLY INCORPORATED ONE. # BE SURE inc CHANGES CURRENT MESSAGE (OVERRIDE .mh_profile): if inc -changecur $* > $temp then cat $temp ${INCSHOW-show} cur-last stat=0 fiI won't explain all of that here. But you can see that with only nine lines of code (and nine more lines of comments), you can write a very useful program. If you're interested in just writing basic one line programs like fols, the following steps should help you. To write more complex scripts, though, get a good book on shell programming.
Here's how to write the fols shell script. You'll use these steps for longer scripts, too.
% folders -fast -recurse | pr -l1 -4 -w78 -tA list of your folder names should come out in columns as you saw at the start of this chapter. (The -l1 is lowercase letter "l" followed by the digit 1.)
#!/bin/shThat tells your system to use the Bourne shell (/bin/sh) as an interpreter. (If you have problems with this, the Section How Does Your System Execute Files? shows how to be sure that it works on your system. If #! doesn't work, you can probably leave the first line blank.)
folders -fast -recurse $* | pr -l1 -4 -w78 -t(The $* pulls in a copy of the script's command-line arguments.)
% chmod +x fols
% rehash
% folsIf that doesn't run, your current directory may not be in your shell's command search path. In that case, try this:
% ./folsand, if you need to, read the Section Making a bin Directory, Setting Search Path.
[Table of Contents] [Index] [Previous: Chapter Introduction (Introduction to UNIX Programming with MH)] [Next: Using MH from Other Languages]
This file is from the third edition of the book MH & xmh: Email for Users & Programmers, ISBN 1-56592-093-7, by Jerry Peek. Copyright © 1991, 1992, 1995 by O'Reilly & Associates, Inc. This file is freely-available; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more information, see the file copying.htm.
Suggestions are welcome: Jerry Peek <jpeek@jpeek.com>