# Getting Message Numbers

Does your program handle command-line arguments like last:10 or 6 25-cur? If your script is running only MH commands, that's no problem because MH commands can understand those message lists. If you're using a standard UNIX command like grep or awk, you'll need the message filenames. Also, if your program uses sequences, it may need to know whether a particular sequence is defined or empty. Here are ways to handle those problems.

The mhpath command will give you full pathnames of individual messages or the messages named in a sequence. That's usually okay for short lists of messages. But pathnames aren't always what you want:

- Pathnames aren't efficient for handling lots of messages. UNIX has to traverse the same list of directories in the path to get each message. That can slow down your program and waste disk access time.
- Some programs don't need the message pathnames; they just need the individual message numbers.
- If you give mhpath an undefined sequence name, it will abort with an error. (Sequences are removed if all the messages in them have been removed.)

Two MH commands, pick and scan, can convert a message range like 25-cur into a list of messages. For example, if the current message is 34, the next command might store the list `6 25 26 29 31 33 34` in the variable msgs:

        args="6 25-cur"
        msgs="`pick -list $args`"

Using pick that way can cause trouble if you have certain pick switches in your MH profile. For example, using -sequence picked in the MH profile means that your script will overwrite a folder's picked sequence every time you run pick to get a message number list. (There's no pick -nosequence switch to solve this problem.) A better (but slightly ugly) answer is to use scan. Give scan the MH format string `%(msg)` that prints just the message numbers. Here's the previous example with scan instead of pick:

        args="6 25-cur"
        msgs="`scan -format '%(msg)' $args`"

Whether you get the list from pick or scan, though, here's how to access the individual messages in a folder. Grab any folder name from the command line (as shown in the Example for loop parsing a command line) and cd to the folder like this:

        cd `mhpath $folder` || exit 1

If mhpath fails or the folder name isn't valid, the `|| exit 1` will abort the script. Otherwise, you can use the message numbers from pick or scan as filenames because the messages will be files in the current directory.

Finding an empty or missing sequence isn't quite as clean a process. The best way I know is to run mark -list -seq sequence-name, where sequence-name is the sequence you want to test. The output for an empty or missing sequence looks like this:

        sequence-name: (null)

In a Bourne shell script, for example, where the sequence name is stored in the seqname variable, you could test for an empty sequence with:

        seq_out=`mark -list -seq $seqname`
        case "$seq_out" in
        ""|"$seqname: (null)")
             # "mark" made no output and/or $seqname is empty
             ...