📄 ch08_04.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.4 Command Evaluation and Accidentally Overwriting Files</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:10Z"><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_03.htm"TITLE="8.3 Introduction to tcsh"><LINKREL="next"HREF="ch08_05.htm"TITLE="8.5 Command-Line Evaluation "></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_03.htm"TITLE="8.3 Introduction to tcsh"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.3 Introduction to tcsh"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_05.htm"TITLE="8.5 Command-Line Evaluation "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.5 Command-Line Evaluation "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1851">8.4 Command Evaluation and Accidentally Overwriting Files</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7534"></A><ACLASS="indexterm"NAME="AUTOID-7536"></A>Before getting into the details of command interpretation, I thought I'd give a very simple example of why it's important. Here's an errorthat occurs all the time. Let's say you have two files, called<EMCLASS="emphasis">file1</EM> and <EMCLASS="emphasis">file2</EM>. You want to create a new version of<EMCLASS="emphasis">file1</EM> that has <EMCLASS="emphasis">file2</EM> added to the end of it. That's what<EMCLASS="emphasis">cat</EM> is all about, so you give the command:<ACLASS="indexterm"NAME="AUTOID-7544"></A></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat file1 file2 > file1</B></CODE> <ICLASS="lineannotation">...wrong</I></PRE></BLOCKQUOTE></P><PCLASS="para">This looks like it should work. If you've ever tried it, you know itdoesn't; it erases <EMCLASS="emphasis">file1</EM>, and then dumps <EMCLASS="emphasis">file2</EM> into it.Why?The shell (not <EMCLASS="emphasis">cat</EM>) handles standard input and output.</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">As the shell is processing the command, it sees that you'reredirecting standard output into <EMCLASS="emphasis">file1</EM>, so it opens the file forwriting, destroying the data that's already in it.</P></LI><LICLASS="listitem"><PCLASS="para">Later, after it'sfinished interpreting the command line, the shell executes <EMCLASS="emphasis">cat</EM>,passing <EMCLASS="emphasis">file1</EM> and <EMCLASS="emphasis">file2</EM> as arguments. But <EMCLASS="emphasis">file1</EM> isalready empty.</P></LI><LICLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">cat</EM> reads <EMCLASS="emphasis">file1</EM> (which is empty) andwrites it on standard output (which goes into <EMCLASS="emphasis">file1</EM>).</P></LI><LICLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">cat</EM> reads <EMCLASS="emphasis">file2</EM> (which also goes into <EMCLASS="emphasis">file1</EM>). Atthis point, <EMCLASS="emphasis">cat</EM> is finished, so it exits.</P></LI></UL><PCLASS="para"><EMCLASS="emphasis">file1</EM> and <EMCLASS="emphasis">file2</EM> are identical, which isn't whatyou wanted. But it's what you got.</P><PCLASS="para">Some versions of <EMCLASS="emphasis">cat</EM> give you a warning message inthis situation(<CODECLASS="literal">cat: input file1 is output</CODE>). This might lead you to believe thatsomehow <EMCLASS="emphasis">cat</EM> was smart and managed to protect you. Sadly, that'snot true. By the time <EMCLASS="emphasis">cat</EM> figures out that an input file and an output file are the same, it's too late: <EMCLASS="emphasis">file1</EM> is already gone.This bit of <EMCLASS="emphasis">cat</EM>ty cleverness does have a function, though: it preventscommands like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat file1 file2 >> file2</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">from creating infinitely long files.</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_03.htm"TITLE="8.3 Introduction to tcsh"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.3 Introduction to tcsh"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_05.htm"TITLE="8.5 Command-Line Evaluation "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.5 Command-Line Evaluation "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.3 Introduction to tcsh</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.5 Command-Line Evaluation </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 + -