?? ch44_07.htm
字號:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 44] 44.7 Exit Status of UNIX Processes </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:53:38Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch44_01.htm"TITLE="44. Shell Programming for the Uninitiated"><LINKREL="prev"HREF="ch44_06.htm"TITLE="44.6 Pattern Matching in case Statements "><LINKREL="next"HREF="ch44_08.htm"TITLE="44.8 Test Exit Status with the if Statement "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_06.htm"TITLE="44.6 Pattern Matching in case Statements "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.6 Pattern Matching in case Statements "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 44<BR>Shell Programming for the Uninitiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_08.htm"TITLE="44.8 Test Exit Status with the if Statement "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.8 Test Exit Status with the if Statement "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1930">44.7 Exit Status of UNIX Processes </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50564"></A><ACLASS="indexterm"NAME="AUTOID-50567"></A><ACLASS="indexterm"NAME="AUTOID-50570"></A><ACLASS="indexterm"NAME="AUTOID-50573"></A><ACLASS="indexterm"NAME="AUTOID-50576"></A><ACLASS="indexterm"NAME="AUTOID-50578"></A>When a UNIX process (command) runs, it can return a numericstatus value to the process that called (started) it. The status cantell the calling process whether the command succeeded or failed.Many (but not all) UNIX commands return a status of zero ifeverything was okay or non-zero (1, 2, etc.) if something went wrong.A few commands, like <EMCLASS="emphasis">grep</EM> and <EMCLASS="emphasis">diff</EM>, return a differentnon-zero status for different kinds of problems; see your onlinemanual pages to find out.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50584"></A><ACLASS="indexterm"NAME="AUTOID-50587"></A><ACLASS="indexterm"NAME="AUTOID-50590"></A>The Bourne shell puts the exit status of the previous command in thequestion mark (<CODECLASS="literal">?</CODE>) variable. You can get its value bypreceding it with a dollar sign (<CODECLASS="literal">$</CODE>), just like any other shellvariable. For example, when <EMCLASS="emphasis">cp</EM> copies a file, it sets thestatus to 0. If something goes wrong, <EMCLASS="emphasis">cp</EM> sets the status to 1:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>cp afile /tmp</B></CODE>$ <CODECLASS="userinput"><B>echo $?</B></CODE>$ <CODECLASS="userinput"><B>cp afiel /tmp</B></CODE>cp: afiel: No such file or directory$ <CODECLASS="userinput"><B>echo $?</B></CODE>1</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50604"></A><ACLASS="indexterm"NAME="AUTOID-50607"></A><ACLASS="indexterm"NAME="AUTOID-50610"></A>In the C shell, use the <EMCLASS="emphasis">status</EM> variable instead:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cp afiel /tmp</B></CODE>cp: afiel: No such file or directory% <CODECLASS="userinput"><B>echo $status</B></CODE>1</PRE></BLOCKQUOTE></P><PCLASS="para">Of course, you usually don't have to display the exit status in this way,becausethere are<SPANCLASS="link">several ways (<ACLASS="linkend"HREF="ch44_08.htm"TITLE="Test Exit Status with the if Statement ">44.8</A>, <ACLASS="linkend"HREF="ch44_09.htm"TITLE="Testing Your Success ">44.9</A>, <ACLASS="linkend"HREF="ch44_10.htm"TITLE="Loops That Test Exit Status ">44.10</A>)</SPAN>to use the exit status ofone command as a condition of further execution.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="true">true</A><BR><ACLASS="programreference"HREF="examples/index.htm"TITLE="false">false</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-50621"></A><ACLASS="indexterm"NAME="AUTOID-50623"></A>Two simple UNIX utilities do nothing but return an exit status.<EMCLASS="emphasis">true</EM> returns a status of 0 (zero); <EMCLASS="emphasis">false</EM> returns 1 (one).There are GNU versions on the CD-ROM-and no, theydon't have any amazing extra features. <CODECLASS="literal">;-)</CODE></TD></TR></TABLE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50631"></A><ACLASS="indexterm"NAME="AUTOID-50634"></A><ACLASS="indexterm"NAME="AUTOID-50637"></A>The exit status of<SPANCLASS="link">pipelines (<ACLASS="linkend"HREF="ch01_04.htm"TITLE="Using Pipes to Create a New Tool ">1.4</A>)</SPAN>is the status of the last command in the pipeline.[2]You can't test the exit status of a background job in the Bourne shellunless you use the <EMCLASS="emphasis">wait</EM> command to wait for it (in effect, tobring the job out of the background).</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[2] I've seen a few places where that wasn't true - in an early Korn Shell,I think, and a couple of other places too - but that was a longtime ago.</P></BLOCKQUOTE><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_06.htm"TITLE="44.6 Pattern Matching in case Statements "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.6 Pattern Matching in case Statements "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_08.htm"TITLE="44.8 Test Exit Status with the if Statement "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.8 Test Exit Status with the if Statement "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">44.6 Pattern Matching in case Statements </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">44.8 Test Exit Status with the if Statement </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -