Finding Program Name; Multiple Program Names

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

A UNIX program should use its name as the first word in error messages it prints. That's important when the program is running in the background or as part of a pipeline -- you need to know which program has the problem:

someprog: quitting: can't read file xxx
It's tempting to use just the program name in the echo commands:
echo "someprog: quitting: can't read file $file" 1>&2
but, if you ever change the program name, it's easy to forget to fix the messages. A better way is to store the program name in a shell variable at the top of the script file, and then use the variable in all messages:
myname=someprog
    ...
echo "$myname: quitting: can't read file $file" 1>&2
Even better, use the $0 parameter. The shell automatically puts the script's name there. But $0 can have the full pathname of the script, such as /xxx/yyy/bin/someprog. The basename program fixes this: basename strips off the head of a pathname -- everything but the filename.

For example, if $0 is /u/ehuser/bin/sendit, then:

myname="`basename $0`"
would put sendit into the myname shell variable.

Just as MH can use links to give messages more than one name (see the Section Using Links), and just as you can make links to give MH programs several names (see the Chapter New Versions of MH Commands), you can use links to give your program several names. For instance, see the script named both rmmer and rmmer_1 in the Section Explanation of rmmer. Use $0 to get the current name of the program.

The sidebar What Good is a File with 1000 Links? shows a handy use of multiple program names.

[Table of Contents] [Index] [Previous: Looping Through a List of Arguments] [Next: A Test Mail Setup]


Last change $Date: 1996/06/06 15:10:14 $

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>

Questions or problems regarding this web site should be directed to Steve Gielda.
Copyright 1999 www.cotse.com.  All rights reserved.
Last modified: Friday April 02, 1999.