~ubuntu-branches/ubuntu/trusty/mh-book/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<HTML>
<HEAD>
<META NAME="Author" CONTENT="Jerry Peek">
<TITLE>MH &amp; nmh: Using Exit Status</TITLE>
</HEAD>
<!-- $Id: usista.htm,v 6.0 1999/10/10 05:14:05 jpeek Exp $ -->

<BODY BGCOLOR="#FFFFFF">
<A NAME="index1"></A>
<H1>Using Exit Status</H1>
[<A HREF="comsub.htm">previous</A>]
[<A HREF="hanarg.htm">next</A>]
[<A HREF="tocs/jump.htm">table of contents</A>] [<A HREF="indexes/map.htm">index</A>]
<P>

When a UNIX command runs, it can return a numeric status value to the
program that started it.
The status can tell the calling program whether the command succeeded
or failed.
Many (but not all) UNIX commands return a status of zero if everything
was okay or nonzero (1, 2, etc.) if something went wrong.
Almost all MH programs return a status.
<A NAME="index2"></A>
The Bourne shell puts the exit status of each process it runs into its
question mark (<TT>?</TT>) variable; the C shell uses its <I>status</I> variable.
So, to see the exit status of an MH command in a particular situation,
run the MH command -- then type <TT>echo&nbsp;$?</TT> to the Bourne shell or
<TT>echo&nbsp;$status</TT> to the C shell.
<P>
Of course, you usually don't have to display the exit status in this
way, because the Bourne shell provides a couple of ways to use the exit
status of one command as a condition of further execution.
After a shell program
runs a UNIX command, it can test the exit status to see if there was a problem.
<P>
<A NAME="index3"></A>
Here's a simple example: a Bourne shell script called <I>newmsgs</I>.
It uses the shell's <I>if</I> structure to run <I>inc</I>,
then test <I>inc</I>'s exit status and branch.
If <I>inc</I> brings in some messages, <I>newmsgs</I> runs the
<I>show</I> command to show the current message (the first new message).
Otherwise, the program prints a note that there were no messages to show.
(You can also get this simple script from this book's online archive.
It's at
<A HREF="../download/split/mh/bin/newmsgs">
<I>download/split/mh/bin/newmsgs</I></A>.)
<A NAME="index4"></A>
<PRE>
% <B>cat newmsgs</B>
#! /bin/sh
if inc
then
    show
else
    echo "Sorry; no new message to show now."
fi
</PRE>
A more UNIX-like shell script wouldn't tell you that there were no
messages; the <TT>no mail to incorporate</TT>
message from <I>inc</I> would be enough.
You could rewrite <I>newmsgs</I> to use the shell's <TT>&amp;&amp;</TT> operator:
<PRE>
#! /bin/sh
inc &amp;&amp; show
</PRE>
There's more information about <TT>&amp;&amp;</TT> in the
Section <A HREF="mhanthsh.htm#MaAlanFu">Making Aliases and Functions</A>.
The shells also have a <TT>||</TT> operator that runs the second command
if the first command returns a nonzero status.
<P>

<P ALIGN=CENTER>
[<A HREF="tocs/jump.htm">Table of Contents</A>] [<A HREF="indexes/map.htm">Index</A>]
[<A HREF="comsub.htm">Previous: Shell Command Substitution</A>]
[<A HREF="hanarg.htm">Next: Looping Through a List of Arguments</A>]
</P>
<HR>
<STRONG>Revised by Jerry Peek.</STRONG>
<EM>Last change $Date: 1999/10/10 05:14:05 $</EM>
<P>
This file is from the third edition of the book <I>MH &amp; xmh: Email
for Users &amp; Programmers</I>, ISBN 1-56592-093-7, by Jerry Peek.
Copyright &copy; 1991, 1992, 1995 by O'Reilly &amp; 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
<A HREF="../copying.htm">the file <I>copying.htm</I></A>.
<P>
<ADDRESS>
Suggestions are welcome:
<A HREF="http://www.jpeek.com/">Jerry Peek</A>
<A HREF="mailto:jpeek@jpeek.com">&lt;jpeek@jpeek.com&gt;</A>
</ADDRESS>
</BODY>
</HTML>