⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trap.html

📁 posix标准英文,html格式
💻 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-2004 IEEE and 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, 2004 Edition<br>Copyright &copy; 2001-2004 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 beread and executed by the shell when one of the corresponding conditions arises. The action of <i>trap</i> shall override a previousaction (either default action or one explicitly set). The value of <tt>"$?"</tt> after the <i>trap</i> action completes shall bethe 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, aslisted in the tables of signal names in the <a href="../basedefs/signal.h.html"><i>&lt;signal.h&gt;</i></a> header defined in theBase Definitions volume of IEEE&nbsp;Std&nbsp;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 anextension. 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 thelast 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 reportedwhen attempting to do so. An interactive shell may reset or catch signals ignored on entry. Traps shall remain in place for a givenshell 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. Theformat shall be:</p><pre><tt>"trap -- %s %s ...\n", &lt;</tt><i>action</i><tt>&gt;, &lt;</tt><i>condition</i><tt>&gt; ...</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 ascommands 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:</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">1</p></td><td align="left"><p class="tent">SIGHUP</p></td></tr><tr valign="top"><td align="left"><p class="tent">2</p></td><td align="left"><p class="tent">SIGINT</p></td></tr><tr valign="top"><td align="left"><p class="tent">3</p></td><td align="left"><p class="tent">SIGQUIT</p></td></tr><tr valign="top"><td align="left"><p class="tent">6</p></td><td align="left"><p class="tent">SIGABRT</p></td></tr><tr valign="top"><td align="left"><p class="tent">9</p></td><td align="left"><p class="tent">SIGKILL</p></td></tr><tr valign="top"><td align="left"><p class="tent">14</p></td><td align="left"><p class="tent">SIGALRM</p></td></tr><tr valign="top"><td align="left"><p class="tent">15</p></td><td align="left"><p class="tent">SIGTERM</p></td></tr></table><p><img src="../images/opt-end.gif" alt="[Option End]" border="0"> </p><p>The <i>trap</i> special built-in shall conform to the Base Definitions volume of IEEE&nbsp;Std&nbsp;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>See the DESCRIPTION.</p></blockquote><h4><a name="tag_04_146_06"></a>STDIN</h4><blockquote><p>Not used.</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>Default.</p></blockquote><h4><a name="tag_04_146_10"></a>STDOUT</h4><blockquote><p>See the DESCRIPTION.</p></blockquote><h4><a name="tag_04_146_11"></a>STDERR</h4><blockquote><p>The standard error shall be used only for diagnostic messages.</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"> &nbsp;or number <img src="../images/opt-end.gif" alt="[Option End]" border="0"> is invalid, a non-zeroexit status shall be returned; otherwise, zero shall be returned. For both interactive and non-interactive shells, invalid signalnames <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;or numbers <img src="../images/opt-end.gif" alt="[Option End]" border="0"> shall not be considered a syntax error and donot cause the shell to abort.</p></blockquote><h4><a name="tag_04_146_15"></a>CONSEQUENCES OF ERRORS</h4><blockquote><p>Default.</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 SIGprefix; no known historical shell does so. The <i>trap</i> and <a href="../utilities/kill.html"><i>kill</i></a> utilities in thisvolume of IEEE&nbsp;Std&nbsp;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 POSIXapplications 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 (becausenumeric signal values are not portable) and had to change to become so, an opportunity was taken to format the output in a way thata 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> wouldcause 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 shellswhich, for example, close the standard input before executing a trap on 0. For example, in some historical interactive shellsessions 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 markedobsolescent). Other implementations need not provide this optional mapping.</p><p>IEEE&nbsp;Std 1003.1-2001/Cor&nbsp;1-2002, item XCU/TC1/D6/5 is applied so that the reference page sections use terms asdescribed in the Utility Description Defaults ( <a href="xcu_chap01.html#tag_01_11"><i>Utility Description Defaults</i></a>). Nochange in behavior is intended.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; 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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -