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

📄 ch08_06.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.6 Output Command-Line Arguments </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:34:14Z"><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="ch08_01.htm"TITLE="8. How the Shell Interprets What You Type"><LINKREL="prev"HREF="ch08_05.htm"TITLE="8.5 Command-Line Evaluation "><LINKREL="next"HREF="ch08_07.htm"TITLE="8.7 Setting Your Search Path "></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="ch08_05.htm"TITLE="8.5 Command-Line Evaluation "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.5 Command-Line Evaluation "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 8<BR>How the Shell Interprets What You Type</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_07.htm"TITLE="8.7 Setting Your Search Path "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.7 Setting Your Search Path "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7911">8.6 Output Command-Line Arguments </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7726"></A><ACLASS="indexterm"NAME="AUTOID-7729"></A>The <EMCLASS="emphasis">echo</EM> command writes its command-line arguments and a newlineto the standard output.Shell scripts use <EMCLASS="emphasis">echo</EM> for sending text to the terminal, downa pipe, and into a file.You can use <EMCLASS="emphasis">echo</EM> on the command line to show the value of a<SPANCLASS="link">variable (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>, <ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>,to see how filename wildcards will expand without doing anything else tothose files, or<SPANCLASS="link">to check quoting (<ACLASS="linkend"HREF="ch46_02.htm"TITLE="Quoting Trouble?  Think, Then Use echo ">46.2</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo &quot;USER is $USER.&quot;</B></CODE>USER is jerry.% <CODECLASS="userinput"><B>echo &quot;All 'a' files are:    &quot; a*</B></CODE>All 'a' files are:     abacus apple axes</PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">printf</EM> command gives you more formatting control.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7911-SECT-1.1">8.6.1 Portability </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7746"></A>The C shell and most other newer shells have aversion of <EMCLASS="emphasis">echo</EM> that's<SPANCLASS="link">built in (<ACLASS="linkend"HREF="ch01_10.htm"TITLE="Internal and External Commands ">1.10</A>)</SPAN>so it's faster.</P><PCLASS="para">The original <EMCLASS="emphasis">echo</EM>, and the <EMCLASS="emphasis">csh</EM> echo which acts like it,have just one option.The <EMCLASS="emphasis">-n</EM> option tells<EMCLASS="emphasis">echo</EM> not to print a newline after the message.Shell scripts use <EMCLASS="emphasis">-n</EM> to send a question to a user and leave thecursor at the end of the message:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">echo -n &quot;Enter your name: &quot;</PRE></BLOCKQUOTE></P><PCLASS="para">(The space at the end makes the prompt look better.The quotes make the shell pass that space on to <EMCLASS="emphasis">echo</EM>.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7762"></A><ACLASS="indexterm"NAME="AUTOID-7765"></A><ACLASS="indexterm"NAME="AUTOID-7768"></A>Newer versions of <EMCLASS="emphasis">echo</EM> check their arguments for a backslash (<CODECLASS="literal">\</CODE>).This marks the start of an <EMCLASS="emphasis">escape sequence</EM>, a characterthat the backslash and the next letter stand for.For example, when these newer <EMCLASS="emphasis">echo</EM>s see <CODECLASS="literal">\n</CODE>, they printa newline character:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>echo &quot;1.\n2.\n3.&quot;</B></CODE>1.2.3.$</PRE></BLOCKQUOTE></P><PCLASS="para">In this version of <EMCLASS="emphasis">echo</EM>, a <CODECLASS="literal">\c</CODE> at the end of the last argument suppressesthe newline&nbsp;- like the <EMCLASS="emphasis">-n</EM> option does in the other <EMCLASS="emphasis">echo</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">echo &quot;Enter your name: \c&quot;</PRE></BLOCKQUOTE></P><PCLASS="para">Your online <EMCLASS="emphasis">echo</EM> (or <EMCLASS="emphasis">csh</EM>) manual pageshould tell you which version you haveand list any escape sequences.</P><PCLASS="para">The problem with this newer <EMCLASS="emphasis">echo</EM> is thatit's tough to <EMCLASS="emphasis">echo</EM> an arbitrary string that might have a backslashin it.<ACLASS="indexterm"NAME="AUTOID-7792"></A>Chris Torek has a workaround: use a<SPANCLASS="link">here-document (<ACLASS="linkend"HREF="ch08_18.htm"TITLE="Here Documents ">8.18</A>)</SPAN>and<SPANCLASS="link"><EMCLASS="emphasis">cat</EM> (<ACLASS="linkend"HREF="ch25_02.htm"TITLE="Four Ways to Skin a cat ">25.2</A>)</SPAN>instead of <EMCLASS="emphasis">echo</EM>.  For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">cat &lt;&lt; ENDThe answer is: $<CODECLASS="replaceable"><I>variable-whose-value-might-contain-backslashes</I></CODE>END</PRE></BLOCKQUOTE></P><PCLASS="para">&#13;<EMCLASS="emphasis">bash</EM> users are lucky:That shell's <EMCLASS="emphasis">echo</EM> has a <EMCLASS="emphasis">-e</EM> option that enables backslashinterpretation, and a <EMCLASS="emphasis">-E</EM> option that disables it.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="printf">printf</A><BR></TH><TDVALIGN="TOP">&#13;Another utility called <EMCLASS="emphasis">printf</EM> works like the <EMCLASS="emphasis">printf(3)</EM>routine in the C language; it handles escape sequences, lets you setfield widths, and more.(The GNU version is on the CD-ROM.)Here's an example.<ACLASS="indexterm"NAME="AUTOID-7810"></A><ACLASS="indexterm"NAME="AUTOID-7812"></A>The <EMCLASS="emphasis">wc</EM>command gives the number of lines, words, and characters in a file,followed by the filename.We pass those four fields in the <EMCLASS="emphasis">wc</EM> output to the <EMCLASS="emphasis">printf</EM>command line with<SPANCLASS="link">backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>.(If you need a reminder of <EMCLASS="emphasis">wc</EM>'s output, see article<ACLASS="xref"HREF="ch29_06.htm"TITLE="Counting Lines, Words, and Characters: wc ">29.6</A>.)<EMCLASS="emphasis">printf</EM> takes a formatting command from its first argument.It outputs the fourth argument after that (<CODECLASS="literal">%4$s</CODE>, the filename);the number of words from the second argument, in a field five wide withleading zeroes (<CODECLASS="literal">%2$05s</CODE>); and the number of lines from the firstargument (<CODECLASS="literal">%1$s</CODE>); with a newline (<CODECLASS="literal">\n</CODE>) to finish the line:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>printf 'The %4$s file has %2$05s words on %1$s lines.\n' `wc count`</B></CODE>The count file has 00235 words on 42 lines.</PRE></BLOCKQUOTE></P><PCLASS="para">Because <EMCLASS="emphasis">printf</EM> isn't built into any shells I know of, it's moreportable than the crazy set of <EMCLASS="emphasis">echo</EM>s.If we had <EMCLASS="emphasis">printf</EM> and the old <EMCLASS="emphasis">echo</EM>, life would be easier.Article<ACLASS="xref"HREF="ch46_10.htm"TITLE="A Portable echo Command ">46.10</A>shows a way to make <EMCLASS="emphasis">echo</EM> portable.</P><PCLASS="para">The C shell <EMCLASS="emphasis">echo</EM> works differently from other versions.For example, to make an empty line with the standard <EMCLASS="emphasis">echo</EM>, don't give<ACLASS="indexterm"NAME="AUTOID-7838"></A><ACLASS="indexterm"NAME="AUTOID-7841"></A><ACLASS="indexterm"NAME="AUTOID-7844"></A>any arguments. (This is usually done for readability&nbsp;- to put blank lines between other output.)Standard <EMCLASS="emphasis">echo</EM> will just print a newline:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>echo</B></CODE>$</PRE></BLOCKQUOTE></P><PCLASS="para">Without arguments, the C shell <EMCLASS="emphasis">echo</EM> doesn't print the newline.To get a newline, you have to give an empty argument:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo &quot;&quot;</B></CODE>%</PRE></BLOCKQUOTE></P><PCLASS="para">To use the standard <EMCLASS="emphasis">echo</EM> from the C shell, type <EMCLASS="emphasis">/bin/echo</EM> instead.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7911-SECT-1.2">8.6.2 Making Error Messages </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7862"></A><ACLASS="indexterm"NAME="AUTOID-7865"></A><EMCLASS="emphasis">echo</EM> writes to standard output.Error messages in shell scripts should be written to the standard errorso that<SPANCLASS="link">redirection (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>of standard output doesn't accidentally capture the message.The Bourne shell<SPANCLASS="link"><CODECLASS="literal">1&gt;&amp;2</CODE> operator (<ACLASS="linkend"HREF="ch45_21.htm"TITLE="n&gt;&amp;m: Swap Standard Output and Standard Error ">45.21</A>)</SPAN>will move <EMCLASS="emphasis">echo</EM>'s output to standard error:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">echo &quot;progname: choke wheeze complain&quot; 1&gt;&amp;2</PRE></BLOCKQUOTE></P><PCLASS="para">The C shell can't do that&nbsp;- which is another reason<SPANCLASS="link">not to write shell scripts with <EMCLASS="emphasis">csh</EM> (<ACLASS="linkend"HREF="ch47_02.htm"TITLE="C Shell Programming Considered Harmful ">47.2</A>)</SPAN>.</P></DIV><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="ch08_05.htm"TITLE="8.5 Command-Line Evaluation "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.5 Command-Line Evaluation "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="ch08_07.htm"TITLE="8.7 Setting Your Search Path "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.7 Setting Your Search Path "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.5 Command-Line Evaluation </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">8.7 Setting Your Search Path </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 + -