355 lines
12 KiB
HTML
355 lines
12 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>trap</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="trap"></a> <a name="tag_04_146"></a><!-- trap -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_04_146_01"></a>NAME</h4>
|
|
|
|
<blockquote>trap - trap signals</blockquote>
|
|
|
|
<h4><a name="tag_04_146_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<p><code><tt>trap</tt> <b>[</b><i>action condition</i> <tt>...</tt><b>]</b></code></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>If <i>action</i> is <tt>'-'</tt> , the shell shall reset each <i>condition</i> to the default value. If <i>action</i> is null (
|
|
<tt>""</tt> ), the shell shall ignore each specified <i>condition</i> if it arises. Otherwise, the argument <i>action</i> shall be
|
|
read and executed by the shell when one of the corresponding conditions arises. The action of <i>trap</i> shall override a previous
|
|
action (either default action or one explicitly set). The value of <tt>"$?"</tt> after the <i>trap</i> action completes shall be
|
|
the value it had before <i>trap</i> was invoked.</p>
|
|
|
|
<p>The condition can be EXIT, 0 (equivalent to EXIT), or a signal specified using a symbolic name, without the SIG prefix, as
|
|
listed in the tables of signal names in the <a href="../basedefs/signal.h.html"><i><signal.h></i></a> header defined in the
|
|
Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap13.html#tag_13">Chapter 13, Headers</a>;
|
|
for example, HUP, INT, QUIT, TERM. Implementations may permit names with the SIG prefix or ignore case in signal names as an
|
|
extension. Setting a trap for SIGKILL or SIGSTOP produces undefined results.</p>
|
|
|
|
<p>The environment in which the shell executes a <i>trap</i> on EXIT shall be identical to the environment immediately after the
|
|
last command executed before the <i>trap</i> on EXIT was taken.</p>
|
|
|
|
<p>Each time <i>trap</i> is invoked, the <i>action</i> argument shall be processed in a manner equivalent to:</p>
|
|
|
|
<pre>
|
|
<tt>eval</tt> <i>action</i>
|
|
</pre>
|
|
|
|
<p>Signals that were ignored on entry to a non-interactive shell cannot be trapped or reset, although no error need be reported
|
|
when attempting to do so. An interactive shell may reset or catch signals ignored on entry. Traps shall remain in place for a given
|
|
shell until explicitly changed with another <i>trap</i> command.</p>
|
|
|
|
<p>When a subshell is entered, traps that are not being ignored are set to the default actions. This does not imply that the
|
|
<i>trap</i> command cannot be used within the subshell to set new traps.</p>
|
|
|
|
<p>The <i>trap</i> command with no arguments shall write to standard output a list of commands associated with each condition. The
|
|
format shall be:</p>
|
|
|
|
<pre>
|
|
<tt>"trap -- %s %s ...\n", <</tt><i>action</i><tt>>, <</tt><i>condition</i><tt>> ...
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The shell shall format the output, including the proper use of quoting, so that it is suitable for reinput to the shell as
|
|
commands that achieve the same trapping results. For example:</p>
|
|
|
|
<pre>
|
|
<tt>save_traps=$(trap)
|
|
...
|
|
eval "$save_traps"
|
|
</tt>
|
|
</pre>
|
|
|
|
<p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
XSI-conformant systems also allow numeric signal numbers for the conditions corresponding to the following signal names: <img src=
|
|
"../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
|
|
<table border="1" cellpadding="3">
|
|
<tr valign="top">
|
|
<th align="center">
|
|
<p class="tent"><b>Signal Number</b></p>
|
|
</th>
|
|
<th align="center">
|
|
<p class="tent"><b>Signal Name</b></p>
|
|
</th>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> 1</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">SIGHUP <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> 2</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">SIGINT <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> 3</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">SIGQUIT <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> 6</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">SIGABRT <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> 9</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">SIGKILL <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> 14</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">SIGALRM <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> 15</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">SIGTERM <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>The <i>trap</i> special built-in shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, <a href=
|
|
"../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_04"></a>OPTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_05"></a>OPERANDS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_06"></a>STDIN</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_07"></a>INPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_08"></a>ENVIRONMENT VARIABLES</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_09"></a>ASYNCHRONOUS EVENTS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_10"></a>STDOUT</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_11"></a>STDERR</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_12"></a>OUTPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_13"></a>EXTENDED DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_14"></a>EXIT STATUS</h4>
|
|
|
|
<blockquote>
|
|
<p>If the trap name <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt=
|
|
"[Option Start]" border="0"> or number <img src="../images/opt-end.gif" alt="[Option End]" border="0"> is invalid, a non-zero
|
|
exit status shall be returned; otherwise, zero shall be returned. For both interactive and non-interactive shells, invalid signal
|
|
names <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
|
|
"0"> or numbers <img src="../images/opt-end.gif" alt="[Option End]" border="0"> shall not be considered a syntax error and do
|
|
not cause the shell to abort.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_15"></a>CONSEQUENCES OF ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_04_146_16"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_17"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<p>Write out a list of all traps and actions:</p>
|
|
|
|
<pre>
|
|
<tt>trap
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>Set a trap so the <i>logout</i> utility in the directory referred to by the <i>HOME</i>
|
|
environment variable executes when the shell terminates:</p>
|
|
|
|
<pre>
|
|
<tt>trap '$HOME/logout' EXIT
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>or:</p>
|
|
|
|
<pre>
|
|
<tt>trap '$HOME/logout' 0
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>Unset traps on INT, QUIT, TERM, and EXIT:</p>
|
|
|
|
<pre>
|
|
<tt>trap - INT QUIT TERM EXIT
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_18"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>Implementations may permit lowercase signal names as an extension. Implementations may also accept the names with the SIG
|
|
prefix; no known historical shell does so. The <i>trap</i> and <a href="../utilities/kill.html"><i>kill</i></a> utilities in this
|
|
volume of IEEE Std 1003.1-2001 are now consistent in their omission of the SIG prefix for signal names. Some <a href=
|
|
"../utilities/kill.html"><i>kill</i></a> implementations do not allow the prefix, and <a href=
|
|
"../utilities/kill.html"><i>kill</i></a> <b>-l</b> lists the signals without prefixes.</p>
|
|
|
|
<p>Trapping SIGKILL or SIGSTOP is syntactically accepted by some historical implementations, but it has no effect. Portable POSIX
|
|
applications cannot attempt to trap these signals.</p>
|
|
|
|
<p>The output format is not historical practice. Since the output of historical <i>trap</i> commands is not portable (because
|
|
numeric signal values are not portable) and had to change to become so, an opportunity was taken to format the output in a way that
|
|
a shell script could use to save and then later reuse a trap if it wanted.</p>
|
|
|
|
<p>The KornShell uses an <b>ERR</b> trap that is triggered whenever <a href="../utilities/set.html"><i>set</i></a> <b>-e</b> would
|
|
cause an exit. This is allowable as an extension, but was not mandated, as other shells have not used it.</p>
|
|
|
|
<p>The text about the environment for the EXIT trap invalidates the behavior of some historical versions of interactive shells
|
|
which, for example, close the standard input before executing a trap on 0. For example, in some historical interactive shell
|
|
sessions the following trap on 0 would always print <tt>"--"</tt> :</p>
|
|
|
|
<pre>
|
|
<tt>trap 'read foo; echo "-$foo-"' 0
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_19"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_20"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="xcu_chap02.html#tag_02_14"><i>Special Built-In Utilities</i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_146_21"></a>CHANGE HISTORY</h4>
|
|
|
|
<h4><a name="tag_04_146_22"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>XSI-conforming implementations provide the mapping of signal names to numbers given above (previously this had been marked
|
|
obsolescent). Other implementations need not provide this optional mapping.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|