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

📄 ch46_01.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 46] Shell Script Debugging and Gotchas</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:33Z"><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="part08.htm"TITLE="VIII. Shell Programming "><LINKREL="prev"HREF="ch45_36.htm"TITLE="45.36 Shell Lockfile "><LINKREL="next"HREF="ch46_02.htm"TITLE="46.2 Quoting Trouble?  Think, Then Use echo "></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="ch45_36.htm"TITLE="45.36 Shell Lockfile "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.36 Shell Lockfile "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 46</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch46_02.htm"TITLE="46.2 Quoting Trouble?  Think, Then Use echo "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 46.2 Quoting Trouble?  Think, Then Use echo "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-46">46. Shell Script Debugging and Gotchas</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-7160"TITLE="46.1 Tips for Debugging Shell Scripts ">Tips for Debugging Shell Scripts </A><BR><ACLASS="sect1"HREF="ch46_02.htm"TITLE="46.2 Quoting Trouble?  Think, Then Use echo ">Quoting Trouble?  Think, Then Use echo </A><BR><ACLASS="sect1"HREF="ch46_03.htm"TITLE="46.3 Bourne Shell Debugger Shows a Shell Variable ">Bourne Shell Debugger Shows a Shell Variable </A><BR><ACLASS="sect1"HREF="ch46_04.htm"TITLE="46.4 Stop Syntax Errors in Numeric Tests ">Stop Syntax Errors in Numeric Tests </A><BR><ACLASS="sect1"HREF="ch46_05.htm"TITLE="46.5 Stop Syntax Errors in String Tests ">Stop Syntax Errors in String Tests </A><BR><ACLASS="sect1"HREF="ch46_06.htm"TITLE="46.6 Watch Out for Bourne Shell -e Bug ">Watch Out for Bourne Shell -e Bug </A><BR><ACLASS="sect1"HREF="ch46_07.htm"TITLE="46.7 Quoting and Command-Line Parameters ">Quoting and Command-Line Parameters </A><BR><ACLASS="sect1"HREF="ch46_08.htm"TITLE="46.8 Test Built-In Commands for Failure ">Test Built-In Commands for Failure </A><BR><ACLASS="sect1"HREF="ch46_09.htm"TITLE="46.9 If Command Doesn't Return a Status, Test the Error Messages">If Command Doesn't Return a Status, Test the Error Messages</A><BR><ACLASS="sect1"HREF="ch46_10.htm"TITLE="46.10 A Portable echo Command ">A Portable echo Command </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7160">46.1 Tips for Debugging Shell Scripts </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-7160-IX-BOURNE-SHELL-SCRIPTS-DEBUGGING"></A><ACLASS="indexterm"NAME="UPT-ART-7160-IX-DEBUGGING-BOURNE-SHELL-SCRIPTS"></A><ACLASS="indexterm"NAME="AUTOID-55327"></A>Depending on the Bourne shell version you have, the error messagesit gives can be downright useless.For instance, it can just say <CODECLASS="literal">End</CODE> <CODECLASS="literal">of</CODE> <CODECLASS="literal">file</CODE><CODECLASS="literal">unexpected</CODE>.Here are a few tricks to use to get a little more information aboutwhat's going on.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7160-SECT-1.1">46.1.1 Use -xv </A></H3><PCLASS="para">Start your script like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">#!/bin/sh -xv</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55340"></A><ACLASS="indexterm"NAME="AUTOID-55343"></A>(or, if your UNIX can't handle <CODECLASS="literal">#!</CODE>, use the command<SPANCLASS="link"><CODECLASS="literal">set&nbsp;-xv</CODE> (<ACLASS="linkend"HREF="ch44_19.htm"TITLE="The Bourne Shell set Command ">44.19</A>)</SPAN>).The <CODECLASS="literal">-xv</CODE> shows you what's happening as the shell reads your script.The lines of the script will be shown as the shell reads them.The shell shows each command it executes with a plus sign (<CODECLASS="literal">+</CODE>) before the command.</P><PCLASS="para">Note that the shell reads an entire loop (<EMCLASS="emphasis">for</EM>, <EMCLASS="emphasis">while</EM>, etc.)before it executes any commands in the loop.</P><PCLASS="para">If you want to run a script with debugging but you don't want to editthe script file, you can also start the shell explicitly from the commandline and give the options there:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>sh -xv </B></CODE><CODECLASS="replaceable"><I>scrfile</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55360"></A><ACLASS="indexterm"NAME="AUTOID-55363"></A><ACLASS="indexterm"NAME="AUTOID-55366"></A><ACLASS="indexterm"NAME="AUTOID-55369"></A><ACLASS="indexterm"NAME="AUTOID-55372"></A><ACLASS="indexterm"NAME="AUTOID-55375"></A><ACLASS="indexterm"NAME="AUTOID-55378"></A><ACLASS="indexterm"NAME="AUTOID-55381"></A><ACLASS="indexterm"NAME="AUTOID-55384"></A><ACLASS="indexterm"NAME="AUTOID-55388"></A>Debugging output is usually pretty long, more than a screenful.So I pipe it to a pager like <EMCLASS="emphasis">pg</EM>.But the shell sends its debugging output to <EMCLASS="emphasis">stderr</EM>, so I<SPANCLASS="link">pipe both <EMCLASS="emphasis">stdout</EM> and <EMCLASS="emphasis">stderr</EM> (<ACLASS="linkend"HREF="ch13_04.htm"TITLE="Problems Piping to a Pager ">13.4</A>)</SPAN>.Using a pager has another advantage: if you want to kill the scriptbefore it finishes, just use the pager's &quot;quit&quot; command (like <EMCLASS="emphasis">q</EM>).<ACLASS="indexterm"NAME="AUTOID-55397"></A>When the pager quits, UNIX may even killthe shell script (you may seethe message<SPANCLASS="link"><CODECLASS="literal">Broken pipe</CODE> (<ACLASS="linkend"HREF="ch50_12.htm"TITLE="Common UNIX Error Messages ">50.12</A>)</SPAN>).</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55402"></A><ACLASS="indexterm"NAME="AUTOID-55404"></A>Do you want to save the debugging output in a file and see it on yourscreen, too?Use<SPANCLASS="link"><EMCLASS="emphasis">tee</EM> (<ACLASS="linkend"HREF="ch13_09.htm"TITLE="Send Output Two or More Places with tee ">13.9</A>)</SPAN>to snag the <EMCLASS="emphasis">scrfile</EM> stdout and stderr; add <EMCLASS="emphasis">tee</EM> to thepipeline before the pager.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55412"></A>If the script is slow, you can run it in the background.<SPANCLASS="link">Redirect the shell's output and errors (<ACLASS="linkend"HREF="ch13_05.htm"TITLE="Redirection in C Shell: Capture Errors, Too? ">13.5</A>, <ACLASS="linkend"HREF="ch08_13.htm"TITLE='Is It "2&gt;&amp;1 file&quot; or "&gt; file 2&gt;&amp;1&quot;? Why?'>8.13</A>)</SPAN>into a<SPANCLASS="link">temporary file (<ACLASS="linkend"HREF="ch21_03.htm"TITLE="Unique Names for Temporary Files ">21.3</A>)</SPAN>.Use<SPANCLASS="link"><EMCLASS="emphasis">tail -f</EM> (<ACLASS="linkend"HREF="ch25_16.htm"TITLE="How to Look at a File as It Grows ">25.16</A>)</SPAN>to &quot;watch&quot; the log file.If you want to do something else while the script runs, just kill the<EMCLASS="emphasis">tail</EM> command (with CTRL-cor your interrupt key), do something else,then start another <EMCLASS="emphasis">tail -f</EM> when you want to watch again.</P><PCLASS="para">Finally, if the script normally writes something to its standardoutput, you can<SPANCLASS="link">split the normal and debugging outputs into two files (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7160-SECT-1.2">46.1.2 Unmatched Operators </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55426"></A><ACLASS="indexterm"NAME="AUTOID-55429"></A>If the shell says <CODECLASS="literal">End</CODE> <CODECLASS="literal">of</CODE> <CODECLASS="literal">file</CODE> <CODECLASS="literal">unexpected</CODE>,look for a line in your scriptthat has an opening quote but no closing quote.The shell is probably searching for but never finding the matching quote.The same goes for missing parentheses and braces (<CODECLASS="literal">{}</CODE>).</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7160-SECT-1.3">46.1.3 Exit Early </A></H3><PCLASS="para">If you're getting an <CODECLASS="literal">End</CODE> <CODECLASS="literal">of</CODE> <CODECLASS="literal">file</CODE> <CODECLASS="literal">unexpected</CODE>error, put these two lines near the middle of the script:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">echo &quot;DEBUG: quitting early...&quot; 1&gt;&amp;2exit</PRE></BLOCKQUOTE></P><PCLASS="para">Then run your script.Those lines will print a message and stop the shell where you put them.If you don't get the <CODECLASS="literal">End</CODE> <CODECLASS="literal">of</CODE> <CODECLASS="literal">file</CODE> <CODECLASS="literal">unexpected</CODE>error anymore, you know thatthe problem is somewhere after the <CODECLASS="literal">exit</CODE> line.Move those two lines farther down and try again.(Otherwise, move them up...)</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7160-SECT-1.4">46.1.4 Missing or Extra esac, ;;, fi, etc. </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55455"></A><ACLASS="indexterm"NAME="AUTOID-55458"></A><ACLASS="indexterm"NAME="AUTOID-55461"></A><ACLASS="indexterm"NAME="AUTOID-55464"></A>A message like <CODECLASS="literal">line</CODE> <CODECLASS="literal">23:</CODE> <CODECLASS="literal">;;</CODE> <CODECLASS="literal">unexpected</CODE> meansthat you have anunmatched piece of code somewhere before line 23.You'll also see <CODECLASS="literal">fi unexpected</CODE>.Look at all nested <EMCLASS="emphasis">if</EM> and <EMCLASS="emphasis">case</EM> statements, and statements likethem, to be sure that they end in the right places.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7160-SECT-1.5">46.1.5 Line Numbers Reset Inside Redirected Loops </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55477"></A>The shell may give you an error that mentions &quot;line 1&quot; or another line numberthat seems way too small, when there's no error close to the top of yourscript.Look at any loops or other structures with<SPANCLASS="link">redirected inputs or outputs (<ACLASS="linkend"HREF="ch45_22.htm"TITLE="Handling Files Line-by-Line ">45.22</A>)</SPAN>.Some Bourne shells start a separate shell to run these loops and lose trackof the line numbers.&#13;</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></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="ch45_36.htm"TITLE="45.36 Shell Lockfile "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.36 Shell Lockfile "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_02.htm"TITLE="46.2 Quoting Trouble?  Think, Then Use echo "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 46.2 Quoting Trouble?  Think, Then Use echo "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.36 Shell Lockfile </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.2 Quoting Trouble?  Think, Then Use echo </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 + -