Problems with folder, inc, and refile

[previous] [next] [table of contents] [index]

If you give a folder name to folder, inc, or refile and that folder doesn't exist, you'll be asked whether to create the folder. That's usually fine. But if you leave the shell script running unattended, your script can hang, waiting for an answer, if you accidentally give a nonexistent folder name:

% somescript +inbos
There can be a worse problem if you put your shell script into the background (if you type an ampersand (&) at the end of the command line) -- and if your shell doesn't have job control. In this case, the folder, inc, and refile commands will still prompt for a y or n answer. But they'll immediately assume you answered y, create the folder you didn't want, and the script will keep running. (This is because, if these three MH commands read anything except the character n, they assume you mean "yes." Shells without job control run background shell scripts with standard input taken from /dev/null, the UNIX "empty file," which is always readable. Even reading a null string from /dev/null is a "yes" to these three commands!)

MH 6.8.3 has new -create and -nocreate options that solve these problems. folder -create will create the named folder if it doesn't exist; it won't prompt for confirmation. The -nocreate option isn't quite the opposite of -create; it has a few twists of its own. If the named folder exists, folder -nocreate works just like plain folder. If the folder doesn't exist, folder -nocreate will exit without changing the current folder and with no error message! The only way to tell that folder -nocreate failed is by checking its exit status. A non-zero status means the current folder wasn't changed.

On MH versions before 6.8.3, the workaround is code like the following in shell scripts before every refile, inc, or folder (or, at least, before the first use of any folder name):

if mhpath new +$folderarg > /dev/null
then
    # do whatever you want to do with +$folderarg
else
    # complain that folder doesn't exist...
fi
The preceding example uses mhpath to get the pathname of a new message in the folder. It throws away the name (if any) from mhpath; to throw away the error message, too, add 2>&1 to the end of the shell command line. If the folder doesn't exist, mhpath returns a non-zero status; the script can print an error, abort, or do whatever you want.

[Table of Contents] [Index] [Previous: Watch Out for the MH Profile] [Next: Chapter Introduction: Tour Through xmh]


Last change $Date: 1996/06/06 15:12:19 $

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 <jerry@ora.com>

HIVE: All information for read only. Please respect copyright!
Hosted by hive КГБ: Киевская городская библиотека