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

📄 ch46_08.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 46] 46.8 Test Built-In Commands for Failure </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:55:42Z"><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="ch46_01.htm"TITLE="46. Shell Script Debugging and Gotchas"><LINKREL="prev"HREF="ch46_07.htm"TITLE="46.7 Quoting and Command-Line Parameters "><LINKREL="next"HREF="ch46_09.htm"TITLE="46.9 If Command Doesn't Return a Status, Test the Error Messages"></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="ch46_07.htm"TITLE="46.7 Quoting and Command-Line Parameters "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 46.7 Quoting and Command-Line Parameters "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 46<BR>Shell Script Debugging and Gotchas</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch46_09.htm"TITLE="46.9 If Command Doesn't Return a Status, Test the Error Messages"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 46.9 If Command Doesn't Return a Status, Test the Error Messages"BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0462">46.8 Test Built-In Commands for Failure </A></H2><PCLASS="para">Some old-timers in shell programming (myself included) tend to depend onshell &quot;features&quot; that they shouldn't.Here's one bad assumption: the Bourne shell will exit if the <EMCLASS="emphasis">cd</EM>command fails.That wasn't documented (as far as I know), but, even so, people wrotescripts like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">...cd $somedirrm -rf *</PRE></BLOCKQUOTE></P><PCLASS="para">The Korn shell didn't have that undocumented behavior.If a <EMCLASS="emphasis">cd</EM> failed, <EMCLASS="emphasis">ksh</EM> would print an error message andkeep on reading the script.That caused some infamous problems when Bourne shell users gave theirscripts to the Korn shell!</P><PCLASS="para">Unless the behavior of a command is documented, don't count on it tokeep a disaster from happening.In the preceding script, for example, the <EMCLASS="emphasis">rm</EM> command removed everythingfrom a directory different than <CODECLASS="literal">$somedir</CODE>.One thing that's worth doing: test the exit status of a built-inand quit if it returns non-zero status.For instance, the<SPANCLASS="link"><CODECLASS="literal">||</CODE> operator (<ACLASS="linkend"HREF="ch44_09.htm"TITLE="Testing Your Success ">44.9</A>)</SPAN>makes this script abort if the <EMCLASS="emphasis">cd</EM> fails:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">...cd $somedir || exitrm -rf *</PRE></BLOCKQUOTE></P><PCLASS="para">Careful testing of scripts that could do something disastrous&nbsp;- trying tofind places where they'll fail&nbsp;- can be worth the time.That's especially true when yourun the script on a new system or withanother shell: test the built-in commands' exit status after they fail.</P><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="ch46_07.htm"TITLE="46.7 Quoting and Command-Line Parameters "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 46.7 Quoting and Command-Line Parameters "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="ch46_09.htm"TITLE="46.9 If Command Doesn't Return a Status, Test the Error Messages"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 46.9 If Command Doesn't Return a Status, Test the Error Messages"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">46.7 Quoting and Command-Line Parameters </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">46.9 If Command Doesn't Return a Status, Test the Error Messages</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 &amp; 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>

⌨️ 快捷键说明

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