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

📄 exit-status.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Exit and Exit Status</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Basics"HREF="part2.html"><LINKREL="PREVIOUS"TITLE="Escaping"HREF="escapingsection.html"><LINKREL="NEXT"TITLE="Tests"HREF="tests.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="escapingsection.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="tests.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="EXIT-STATUS">Chapter 6. Exit and Exit Status</A></H1><TABLEBORDER="0"WIDTH="100%"CELLSPACING="0"CELLPADDING="0"CLASS="EPIGRAPH"><TR><TDWIDTH="45%">&nbsp;</TD><TDWIDTH="45%"ALIGN="LEFT"VALIGN="TOP"><I><P><I>...there are dark corners in the Bourne shell, and people use all      of them.</I></P></I></TD></TR><TR><TDWIDTH="45%">&nbsp;</TD><TDWIDTH="45%"ALIGN="RIGHT"VALIGN="TOP"><I><SPANCLASS="ATTRIBUTION">Chet Ramey</SPAN></I></TD></TR></TABLE><P><ANAME="EXITCOMMANDREF"></A>The 	<BCLASS="COMMAND">	  	  	  exit	</B> 	command may be used to terminate a script, just as in a	<ICLASS="EMPHASIS">C</I> program. It can also return a value,	which is available to the script's parent process.</P><P><ANAME="EXITSTATUSREF"></A>Every command returns an 	<ICLASS="FIRSTTERM">	  	  exit status	</I> 	(sometimes referred to as a 	<ICLASS="FIRSTTERM">	  	  return status	</I>). A successful command returns a	<SPANCLASS="RETURNVALUE">0</SPAN>, while an unsuccessful one returns	a <SPANCLASS="RETURNVALUE">non-zero</SPAN> value that usually may	be interpreted as an error code. Well-behaved UNIX commands,	programs, and utilities return a <SPANCLASS="RETURNVALUE">0</SPAN>	exit code upon successful completion, though there are some	exceptions.</P><P>Likewise, functions within a script and the script itself	return an exit status. The last command executed in the function	or script determines the exit status. Within a script, an	<TTCLASS="USERINPUT"><B>exit <TTCLASS="REPLACEABLE"><I>nnn</I></TT></B></TT>	command may be used to deliver an	<SPANCLASS="RETURNVALUE"><TTCLASS="REPLACEABLE"><I>nnn</I></TT></SPAN> exit status	to the shell (<SPANCLASS="RETURNVALUE"><TTCLASS="REPLACEABLE"><I>nnn</I></TT></SPAN>	must be a decimal number in the <SPANCLASS="RETURNVALUE">0</SPAN> -	<SPANCLASS="RETURNVALUE">255</SPAN> range).</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>When a script ends with an <BCLASS="COMMAND">exit</B> that has	no parameter, the exit status of the script is the exit status	of the last command executed in the script (previous to the	<BCLASS="COMMAND">exit</B>).</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;COMMAND_1   4&nbsp;   5&nbsp;. . .   6&nbsp;   7&nbsp;# Will exit with status of last command.   8&nbsp;COMMAND_LAST   9&nbsp;  10&nbsp;exit</PRE></TD></TR></TABLE></P><P>The equivalent of a bare <BCLASS="COMMAND">exit</B> is        <BCLASS="COMMAND">exit $?</B> or even just omitting the	<BCLASS="COMMAND">exit</B>.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;COMMAND_1   4&nbsp;   5&nbsp;. . .   6&nbsp;   7&nbsp;# Will exit with status of last command.   8&nbsp;COMMAND_LAST   9&nbsp;  10&nbsp;exit $?</PRE></TD></TR></TABLE></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;COMMAND1   4&nbsp;   5&nbsp;. . .    6&nbsp;   7&nbsp;# Will exit with status of last command.   8&nbsp;COMMAND_LAST</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV><P><ANAME="EXSREF"></A></P><P>	<TTCLASS="VARNAME">	    $?</TT> reads the exit status of the last	    command executed. After a function returns,	    <TTCLASS="VARNAME">$?</TT> gives the exit status of the last	    command executed in the function. This is Bash's way of	    giving functions a <SPANCLASS="QUOTE">"return value."</SPAN> After a	    script terminates, a <TTCLASS="VARNAME">$?</TT> from the command	    line gives the exit status of the script, that is, the last	    command executed in the script, which is, by convention,	    <TTCLASS="USERINPUT"><B>0</B></TT> on success or an integer in the	    range <SPANCLASS="RETURNVALUE">1 - 255</SPAN> on error.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX5"></A><P><B>Example 6-1. exit / exit status</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;echo hello   4&nbsp;echo $?    # Exit status 0 returned because command executed successfully.   5&nbsp;   6&nbsp;lskdf      # Unrecognized command.   7&nbsp;echo $?    # Non-zero exit status returned because command failed to execute.   8&nbsp;   9&nbsp;echo  10&nbsp;  11&nbsp;exit 113   # Will return 113 to shell.  12&nbsp;           # To verify this, type "echo $?" after script terminates.  13&nbsp;  14&nbsp;#  By convention, an 'exit 0' indicates success,  15&nbsp;#+ while a non-zero exit value means an error or anomalous condition.</PRE></TD></TR></TABLE><HR></DIV><P><AHREF="variables2.html#XSTATVARREF">$?</A> is especially useful        for testing the result of a command in a script (see <AHREF="filearchiv.html#FILECOMP">Example 12-32</A> and <AHREF="textproc.html#LOOKUP">Example 12-17</A>).</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <AHREF="special-chars.html#NOTREF">!</A>, the logical	<SPANCLASS="QUOTE">"not"</SPAN> qualifier, reverses the outcome of a test or	command, and this affects its <AHREF="exit-status.html#EXITSTATUSREF">exit	status</A>.	<DIVCLASS="EXAMPLE"><HR><ANAME="NEGCOND"></A><P><B>Example 6-2. Negating a condition using <SPANCLASS="TOKEN">!</SPAN></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;true  # the "true" builtin.   2&nbsp;echo "exit status of \"true\" = $?"     # 0   3&nbsp;   4&nbsp;! true   5&nbsp;echo "exit status of \"! true\" = $?"   # 1   6&nbsp;# Note that the "!" needs a space.   7&nbsp;#    !true   leads to a "command not found" error   8&nbsp;#   9&nbsp;# The '!' operator prefixing a command invokes the Bash history mechanism.  10&nbsp;  11&nbsp;true  12&nbsp;!true  13&nbsp;# No error this time, but no negation either.  14&nbsp;# It just repeats the previous command (true).  15&nbsp;  16&nbsp;# Thanks, St閜hane Chazelas and Kristopher Newsome.</PRE></TD></TR></TABLE><HR></DIV>      </P></TD></TR></TABLE></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Certain exit status codes have <AHREF="exitcodes.html#EXITCODESREF">reserved meanings</A> and should not	be user-specified in a script.	</P></TD></TR></TABLE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="escapingsection.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="tests.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Escaping</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part2.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Tests</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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