Problems with folder, inc, and refile

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.

CAUTION: In nmh-1.0, if a folder isn't created (either with the folder -nocreate switch or by answering n to a "Create folder?" prompt), folder will still return a zero exit status. See the workaround below.

On MH versions before 6.8.3, and in nmh versions with this problem, 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.