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

📄 ch46_10.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 46] 46.10 A Portable echo Command </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:48Z"><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_09.htm"TITLE="46.9 If Command Doesn't Return a Status, Test the Error Messages"><LINKREL="next"HREF="ch47_01.htm"TITLE="47. C Shell Programming...NOT"></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_09.htm"TITLE="46.9 If Command Doesn't Return a Status, Test the Error Messages"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 46.9 If Command Doesn't Return a Status, Test the Error Messages"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="CHAPTER"HREF="ch47_01.htm"TITLE="47. C Shell Programming...NOT"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 47. C Shell Programming...NOT"BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0016">46.10 A Portable echo Command </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-55917"></A><ACLASS="indexterm"NAME="AUTOID-55920"></A><ACLASS="indexterm"NAME="AUTOID-55923"></A><ACLASS="indexterm"NAME="AUTOID-55926"></A><ACLASS="indexterm"NAME="AUTOID-55929"></A>One of the frustrating changes to UNIX (for me, at least) is thenewer versions of<SPANCLASS="link"><EMCLASS="emphasis">echo</EM> (<ACLASS="linkend"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>)</SPAN>that interpret escape sequences like<CODECLASS="literal">\c</CODE> and <CODECLASS="literal">\007</CODE>.That feature is actually nice to have&nbsp;- usually, at least.But if the shell script has to work on both Berkeley and System V UNIX,it's a headache to write an <EMCLASS="emphasis">echo</EM> command that prompts a user foran answer&nbsp;- with no newline at the end of the line.(<EMCLASS="emphasis">bash</EM> users have a better&nbsp;- though less portable&nbsp;- answer:the <EMCLASS="emphasis">-e</EM> and <EMCLASS="emphasis">-E</EM> options explained at the end of the article.)With the original UNIX <EMCLASS="emphasis">echo</EM> command, you write:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">echo -n &quot;Answer y for yes or n for no: &quot;</PRE></BLOCKQUOTE></P><PCLASS="para">but the newer <EMCLASS="emphasis">echo</EM> needs:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">echo &quot;Answer y for yes or n for no: \c&quot;</PRE></BLOCKQUOTE></P><PCLASS="para">and giving the wrong command to the wrong <EMCLASS="emphasis">echo</EM> makes messy output.</P><PCLASS="para">I've seen workarounds byBruce Barnett and Liam R. E. Quin.I've turned them into this version.It sets shell variables that you use this way:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$echo &quot;Answer y for yes or n for no: ${nnl}&quot;</PRE></BLOCKQUOTE></P><PCLASS="para">Can your shell script be set up for a particular UNIX version ahead oftime?If it can, write your no&nbsp;- newline <EMCLASS="emphasis">echo</EM> commands like the example above&nbsp;- andput the lines below at the top of your script:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># UN-COMMENT THE LINE FOR YOUR SYSTEM:echo=&quot;echo -n&quot;   nnl= ;;                                     # BSD #echo=&quot;echo&quot;      nnl=&quot;\c&quot; ;;                                # Sys V#echo=&quot;echo -n&quot;   nnl=     PATH=/usr/bin:$PATH; export PATH  # SunOS</PRE></BLOCKQUOTE></P><PCLASS="para">Lines similar to those let the person who installs the script set theright <EMCLASS="emphasis">echo</EM> version.</P><PCLASS="para">But if your script is shared between many UNIX systems (across anetworked filesystem) or runs on a system where users can choose BSD orSystem V features (like SunOS), your script will need to configure<EMCLASS="emphasis">echo</EM> each time it runs.To do that, put the following code at the top of your script:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">case &quot;`echo 'x\c'`&quot; in'x\c')  echo=&quot;echo -n&quot;   nnl= ;;       # BSD x)      echo=&quot;echo&quot;      nnl=&quot;\c&quot; ;;   # Sys V*)      echo &quot;$0 quitting: Can't set up echo.&quot; 1&gt;&amp;2; exit 1 ;;esac</PRE></BLOCKQUOTE></P><PCLASS="para">In that code, the shell runs the current <EMCLASS="emphasis">echo</EM> commandand tests its output.Newer <EMCLASS="emphasis">echo</EM> commands will interpret the <CODECLASS="literal">\c</CODE> and print<CODECLASS="literal">x</CODE> (with no newline after it; that doesn't matter here).Berkeley <EMCLASS="emphasis">echo</EM> commands willecho the <CODECLASS="literal">\c</CODE> literally;this is matched by the first pattern instead.</P><PCLASS="para">You can handle other escape sequences and unprintablecharacters in the same way.For example, to make the code set <CODECLASS="literal">$esc</CODE>, a shell variable that makesan ESCape character, you can add lines like one of the two below:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch45_35.htm"TITLE="45.35 Using a Control Character in a Script ">`echo...\033`</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><ACLASS="indexterm"NAME="AUTOID-55973"></A><ACLASS="indexterm"NAME="AUTOID-55976"></A><ACLASS="indexterm"NAME="AUTOID-55979"></A>esc=<CODECLASS="literal">`echo -n d | tr &quot;d&quot; &quot;\033&quot;`     </CODE><EMCLASS="emphasis"># BSD</EM><CODECLASS="literal">esc=&quot;\033&quot;                          </CODE><EMCLASS="emphasis"># Sys V</EM></PRE></TD></TR></TABLE></P><PCLASS="para">If you use <EMCLASS="emphasis">bash</EM>, you can tell its <EMCLASS="emphasis">echo</EM> command which way toact.The <EMCLASS="emphasis">echo&nbsp;-e</EM> command always does backslash-escape interpretation.The <EMCLASS="emphasis">echo&nbsp;-E</EM> command never interprets backslashes.And the option <EMCLASS="emphasis">-n</EM> (no newline) works in both cases.<ACLASS="indexterm"NAME="AUTOID-55993"></A><ACLASS="indexterm"NAME="AUTOID-55994"></A></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_09.htm"TITLE="46.9 If Command Doesn't Return a Status, Test the Error Messages"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 46.9 If Command Doesn't Return a Status, Test the Error Messages"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="CHAPTER"HREF="ch47_01.htm"TITLE="47. C Shell Programming...NOT"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 47. C Shell Programming...NOT"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">46.9 If Command Doesn't Return a Status, Test the Error Messages</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">47. C Shell Programming...NOT</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 + -