📄 ch08_10.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.10 eval: When You Need Another Chance </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:34:24Z"><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_09.htm"TITLE="8.9 Wildcards Inside of Aliases "><LINKREL="next"HREF="ch08_11.htm"TITLE="8.11 Which One Will bash Use? "></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_09.htm"TITLE="8.9 Wildcards Inside of Aliases "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.9 Wildcards Inside of Aliases "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_11.htm"TITLE="8.11 Which One Will bash Use? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.11 Which One Will bash Use? "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6840">8.10 eval: When You Need Another Chance </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8088"></A><ACLASS="indexterm"NAME="AUTOID-8091"></A>If you read the<SPANCLASS="link">previous article (<ACLASS="linkend"HREF="ch08_09.htm"TITLE="Wildcards Inside of Aliases ">8.9</A>)</SPAN>,you saw that, most of the time, theshell evaluates the command line "in the right order." But what aboutwhen it doesn't? Here's a situation that the shell can't handle.It's admittedly contrived, but not too different from what you mightfind in a<SPANCLASS="link">shell program (<ACLASS="linkend"HREF="ch01_05.htm"TITLE="Anyone Can Program the Shell ">1.5</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set b=\$a</B></CODE>% <CODECLASS="userinput"><B>set a=foo</B></CODE>% <CODECLASS="userinput"><B>echo $b</B></CODE>$a</PRE></BLOCKQUOTE></P><PCLASS="para">When we use the variable <CODECLASS="literal">$b</CODE>, we'd like to get the variable<CODECLASS="literal">$a</CODE>, read it, and use its value. But that doesn't happen.Variable substitution happens once, and it isn't recursive. The valueof <CODECLASS="literal">$b</CODE> is <CODECLASS="literal">$a</CODE>, and that's it. You don't go any further.</P><PCLASS="para">But there's a loophole. The <EMCLASS="emphasis">eval</EM> command says, in essence,"Give me another chance. Re-evaluate this line and execute it."Here's what happens if we stick <EMCLASS="emphasis">eval</EM> before the <EMCLASS="emphasis">echo</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>eval echo $b</B></CODE>foo</PRE></BLOCKQUOTE></P><PCLASS="para">The shell converts <CODECLASS="literal">$b</CODE> into <CODECLASS="literal">$a</CODE>; then <EMCLASS="emphasis">eval</EM> runs through thecommand-line evaluation process again, converting <CODECLASS="literal">echo</CODE> <CODECLASS="literal">$a</CODE> into <CODECLASS="literal">echo</CODE> <CODECLASS="literal">foo</CODE>-which is what we wanted in the first place!</P><PCLASS="para">Here's a more realistic example; you see code like this fairly oftenin Bourne shell scripts:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">...command='grep $grepopts $searchstring $file'for optdo case "$opt" in file) output=' > $ofile' ;; read) output=' | more' ;; sort) postproc=' | sort $sortopts';; esacdone...eval $command $postproc $output</PRE></BLOCKQUOTE></P><PCLASS="para">Do you see what's happening? We're constructing a command that willlook something like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">grep $grepopts $searchstring $file | sort $sortopts > $ofile</PRE></BLOCKQUOTE></P><PCLASS="para">But the entire command is "hidden" in shell variables, including theI/O redirectors and various options. If the <EMCLASS="emphasis">eval</EM> isn't there,this command will blow up in all sorts of bizarre ways. You'll seemessages like <CODECLASS="literal">| not found</CODE>, because variable expansion occurs afteroutput redirection. The "nested" variables (like <CODECLASS="literal">$ofile</CODE>, which isused inside of <CODECLASS="literal">$output</CODE>) won't be expanded either, so you'll also see<CODECLASS="literal">$ofile not found</CODE>.Putting an <EMCLASS="emphasis">eval</EM> in front of the command forces the shell toprocess the line again, guaranteeing that the variables will beexpanded properly and that I/O redirection will take place.</P><PCLASS="para"><EMCLASS="emphasis">eval</EM> is incredibly useful if you have shell variables thatinclude other shell variables, shell variables that include aliases,shell variables that include I/O redirectors, or all sorts of perversities. It's commonly used within shellscripts to "evaluate" commands that are built during execution.There are more examples of <EMCLASS="emphasis">eval</EM> in articles<ACLASS="xref"HREF="ch05_04.htm"TITLE="Setting the TERMCAP Variable with tset ">5.4</A>,<ACLASS="xref"HREF="ch10_07.htm"TITLE="How to Put if-then-else in a C Shell Alias ">10.7</A>,<ACLASS="xref"HREF="ch10_10.htm"TITLE="Simulated Bourne Shell Functions and Aliases ">10.10</A>,<ACLASS="xref"HREF="ch45_17.htm"TITLE="Making a for Loop with Multiple Variables ">45.17</A>,<ACLASS="xref"HREF="ch45_34.htm"TITLE="Arrays in the Bourne Shell ">45.34</A>,<ACLASS="xref"HREF="ch46_03.htm"TITLE="Bourne Shell Debugger Shows a Shell Variable ">46.3</A>,and others.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</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_09.htm"TITLE="8.9 Wildcards Inside of Aliases "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.9 Wildcards Inside of Aliases "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_11.htm"TITLE="8.11 Which One Will bash Use? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.11 Which One Will bash Use? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.9 Wildcards Inside of Aliases </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.11 Which One Will bash Use? </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 & 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 + -