3
<META NAME="Author" CONTENT="Jerry Peek">
4
<TITLE>MH & nmh: Using Exit Status</TITLE>
6
<!-- $Id: usista.htm,v 6.0 1999/10/10 05:14:05 jpeek Exp $ -->
8
<BODY BGCOLOR="#FFFFFF">
10
<H1>Using Exit Status</H1>
11
[<A HREF="comsub.htm">previous</A>]
12
[<A HREF="hanarg.htm">next</A>]
13
[<A HREF="tocs/jump.htm">table of contents</A>] [<A HREF="indexes/map.htm">index</A>]
16
When a UNIX command runs, it can return a numeric status value to the
17
program that started it.
18
The status can tell the calling program whether the command succeeded
20
Many (but not all) UNIX commands return a status of zero if everything
21
was okay or nonzero (1, 2, etc.) if something went wrong.
22
Almost all MH programs return a status.
24
The Bourne shell puts the exit status of each process it runs into its
25
question mark (<TT>?</TT>) variable; the C shell uses its <I>status</I> variable.
26
So, to see the exit status of an MH command in a particular situation,
27
run the MH command -- then type <TT>echo $?</TT> to the Bourne shell or
28
<TT>echo $status</TT> to the C shell.
30
Of course, you usually don't have to display the exit status in this
31
way, because the Bourne shell provides a couple of ways to use the exit
32
status of one command as a condition of further execution.
34
runs a UNIX command, it can test the exit status to see if there was a problem.
37
Here's a simple example: a Bourne shell script called <I>newmsgs</I>.
38
It uses the shell's <I>if</I> structure to run <I>inc</I>,
39
then test <I>inc</I>'s exit status and branch.
40
If <I>inc</I> brings in some messages, <I>newmsgs</I> runs the
41
<I>show</I> command to show the current message (the first new message).
42
Otherwise, the program prints a note that there were no messages to show.
43
(You can also get this simple script from this book's online archive.
45
<A HREF="../download/split/mh/bin/newmsgs">
46
<I>download/split/mh/bin/newmsgs</I></A>.)
55
echo "Sorry; no new message to show now."
58
A more UNIX-like shell script wouldn't tell you that there were no
59
messages; the <TT>no mail to incorporate</TT>
60
message from <I>inc</I> would be enough.
61
You could rewrite <I>newmsgs</I> to use the shell's <TT>&&</TT> operator:
66
There's more information about <TT>&&</TT> in the
67
Section <A HREF="mhanthsh.htm#MaAlanFu">Making Aliases and Functions</A>.
68
The shells also have a <TT>||</TT> operator that runs the second command
69
if the first command returns a nonzero status.
73
[<A HREF="tocs/jump.htm">Table of Contents</A>] [<A HREF="indexes/map.htm">Index</A>]
74
[<A HREF="comsub.htm">Previous: Shell Command Substitution</A>]
75
[<A HREF="hanarg.htm">Next: Looping Through a List of Arguments</A>]
78
<STRONG>Revised by Jerry Peek.</STRONG>
79
<EM>Last change $Date: 1999/10/10 05:14:05 $</EM>
81
This file is from the third edition of the book <I>MH & xmh: Email
82
for Users & Programmers</I>, ISBN 1-56592-093-7, by Jerry Peek.
83
Copyright © 1991, 1992, 1995 by O'Reilly & Associates, Inc.
84
This file is freely available; you can redistribute it and/or modify
85
it under the terms of the GNU General Public License as published by
86
the Free Software Foundation. For more information, see
87
<A HREF="../copying.htm">the file <I>copying.htm</I></A>.
90
Suggestions are welcome:
91
<A HREF="http://www.jpeek.com/">Jerry Peek</A>
92
<A HREF="mailto:jpeek@jpeek.com"><jpeek@jpeek.com></A>