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

« back to all changes in this revision

Viewing changes to mh/usista.htm

  • Committer: Bazaar Package Importer
  • Author(s): Peter S Galbraith
  • Date: 2001-03-26 12:42:56 UTC
  • Revision ID: james.westby@ubuntu.com-20010326124256-philz6gf517qz8bo
Tags: upstream-9910
ImportĀ upstreamĀ versionĀ 9910

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<HTML>
 
2
<HEAD>
 
3
<META NAME="Author" CONTENT="Jerry Peek">
 
4
<TITLE>MH &amp; nmh: Using Exit Status</TITLE>
 
5
</HEAD>
 
6
<!-- $Id: usista.htm,v 6.0 1999/10/10 05:14:05 jpeek Exp $ -->
 
7
 
 
8
<BODY BGCOLOR="#FFFFFF">
 
9
<A NAME="index1"></A>
 
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>]
 
14
<P>
 
15
 
 
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
 
19
or failed.
 
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.
 
23
<A NAME="index2"></A>
 
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&nbsp;$?</TT> to the Bourne shell or
 
28
<TT>echo&nbsp;$status</TT> to the C shell.
 
29
<P>
 
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.
 
33
After a shell program
 
34
runs a UNIX command, it can test the exit status to see if there was a problem.
 
35
<P>
 
36
<A NAME="index3"></A>
 
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.
 
44
It's at
 
45
<A HREF="../download/split/mh/bin/newmsgs">
 
46
<I>download/split/mh/bin/newmsgs</I></A>.)
 
47
<A NAME="index4"></A>
 
48
<PRE>
 
49
% <B>cat newmsgs</B>
 
50
#! /bin/sh
 
51
if inc
 
52
then
 
53
    show
 
54
else
 
55
    echo "Sorry; no new message to show now."
 
56
fi
 
57
</PRE>
 
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>&amp;&amp;</TT> operator:
 
62
<PRE>
 
63
#! /bin/sh
 
64
inc &amp;&amp; show
 
65
</PRE>
 
66
There's more information about <TT>&amp;&amp;</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.
 
70
<P>
 
71
 
 
72
<P ALIGN=CENTER>
 
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>]
 
76
</P>
 
77
<HR>
 
78
<STRONG>Revised by Jerry Peek.</STRONG>
 
79
<EM>Last change $Date: 1999/10/10 05:14:05 $</EM>
 
80
<P>
 
81
This file is from the third edition of the book <I>MH &amp; xmh: Email
 
82
for Users &amp; Programmers</I>, ISBN 1-56592-093-7, by Jerry Peek.
 
83
Copyright &copy; 1991, 1992, 1995 by O'Reilly &amp; 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>.
 
88
<P>
 
89
<ADDRESS>
 
90
Suggestions are welcome:
 
91
<A HREF="http://www.jpeek.com/">Jerry Peek</A>
 
92
<A HREF="mailto:jpeek@jpeek.com">&lt;jpeek@jpeek.com&gt;</A>
 
93
</ADDRESS>
 
94
</BODY>
 
95
</HTML>