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

📄 ch46_09.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 46] 46.9 If Command Doesn't Return a Status, Test the Error Messages</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:43Z"><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_08.htm"TITLE="46.8 Test Built-In Commands for Failure "><LINKREL="next"HREF="ch46_10.htm"TITLE="46.10 A Portable echo Command "></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_08.htm"TITLE="46.8 Test Built-In Commands for Failure "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 46.8 Test Built-In Commands for Failure "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_10.htm"TITLE="46.10 A Portable echo Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 46.10 A Portable echo Command "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0353">46.9 If Command Doesn't Return a Status, Test the Error Messages</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55870"></A><ACLASS="indexterm"NAME="AUTOID-55873"></A><ACLASS="indexterm"NAME="AUTOID-55876"></A>UNIX commands should return a zero<SPANCLASS="link">exit status (<ACLASS="linkend"HREF="ch44_07.htm"TITLE="Exit Status of UNIX Processes ">44.7</A>)</SPAN>if they succeedor a non-zero status if they fail.Not all commands do.For example, here's a log I found of a test I did back in 1985 to the4.3BSD version of<SPANCLASS="link"><EMCLASS="emphasis">touch</EM> (<ACLASS="linkend"HREF="ch21_07.htm"TITLE="Setting File Modification Time with touch ">21.7</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>touch /tmp</B></CODE>touch: /tmp: can only touch regular files$ <CODECLASS="userinput"><B>echo $?</B></CODE>$ <CODECLASS="userinput"><B>touch -f /usr/src/usr.bin/touch.c</B></CODE>touch: /usr/src/usr.bin/touch.c: couldn't chmod: Not owner$ <CODECLASS="userinput"><B>echo $?</B></CODE>$ <CODECLASS="userinput"><B>touch -z</B></CODE>touch: bad option -z$ <CODECLASS="userinput"><B>echo $?</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55891"></A><ACLASS="indexterm"NAME="AUTOID-55894"></A>Because the status was always zero, as if the command had succeeded,my shell scripts couldn't test the exit status for failure.A workaround is to make your own shell script version of <EMCLASS="emphasis">touch</EM>(maybe call it <EMCLASS="emphasis">mytouch</EM>)-or to put code like this into a shellscript where you need to run <EMCLASS="emphasis">touch</EM>:[1]</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] Thanks to Richard Doty for this idea and the section of the shell scriptI've shown here.</P></BLOCKQUOTE><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch45_21.htm"TITLE="45.21 n&gt;&amp;m: Swap Standard Output and Standard Error ">2&gt;&amp;1</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"># RUN touch ON THE COMMAND LINE ARGUMENTS (INCLUDING ANY OPTIONS).# MERGE stderr ONTO stdout AND TEST FOR ERROR MESSAGES:<ACLASS="indexterm"NAME="AUTOID-55904"></A>out=`/bin/touch &quot;$@&quot; 2&gt;&amp;1`case &quot;$out&quot; in&quot;&quot;)                                   exitstat=0;;*bad\ option)                         exitstat=l ; echo &quot;$out&quot; 1&gt;&amp;2;;*does\ not\ exist)                    exitstat=3 ; echo &quot;$out&quot; 1&gt;&amp;2;;*can\ only\ touch\ regular\ files*)   exitstat=4 ; echo &quot;$out&quot; 1&gt;&amp;2;;*couldn\'t\ chmod)                    exitstat=5 ; echo &quot;$out&quot; 1&gt;&amp;2;;*couldn\'t\ chmod\ back)              exitstat=6 ; echo &quot;$out&quot; 1&gt;&amp;2;;*cannot\ touch)                       exitstat=7 ; echo &quot;$out&quot; 1&gt;&amp;2;;*)                                    exitstat=10; echo &quot;$out&quot; 1&gt;&amp;2;;esacexit $exitstat</PRE></TD></TR></TABLE></P><PCLASS="para">That code handles many of the error exits; other errors get an exit statusof 10.You could simplify the code to return a status of 1 on <EMCLASS="emphasis">any</EM> error.On the other hand, it would be easy to expand that code to cover anypossible (or interesting) error output.Article<ACLASS="xref"HREF="ch27_19.htm"TITLE="Finding Words Inside Binary Files ">27.19</A>shows how to get a list of many of the possible error messages;you can also see the errors by making some typical mistakes likeunreadable or unwritable filenames.</P><PCLASS="para">That code won't work everywhere.The command you're running might write text to standard output that youdon't want mixed together with error messages (on the standard error).Or, worse, some vendors' versions of some commands write errors to thestandard output!Still, this technique should give you a good start on working aroundbadly behaved UNIX commands.</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_08.htm"TITLE="46.8 Test Built-In Commands for Failure "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 46.8 Test Built-In Commands for Failure "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_10.htm"TITLE="46.10 A Portable echo Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 46.10 A Portable echo Command "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">46.8 Test Built-In Commands for Failure </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.10 A Portable echo Command </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 + -