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

📄 ch45_20.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 45] 45.20 Overview: Open Files and File Descriptors </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:54:45Z"><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="ch45_01.htm"TITLE="45. Shell Programming for the Initiated"><LINKREL="prev"HREF="ch45_19.htm"TITLE="45.19 A while Loop with Several Loop Control Commands "><LINKREL="next"HREF="ch45_21.htm"TITLE="45.21 n&gt;&amp;m: Swap Standard Output and Standard Error "></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_19.htm"TITLE="45.19 A while Loop with Several Loop Control Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.19 A while Loop with Several Loop Control Commands "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 45<BR>Shell Programming for the Initiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch45_21.htm"TITLE="45.21 n&gt;&amp;m: Swap Standard Output and Standard Error "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.21 n&gt;&amp;m: Swap Standard Output and Standard Error "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2050">45.20 Overview: Open Files and File Descriptors </A></H2><PCLASS="para">[This introduction is general and simplified.If you're a technical person who needs a complete and exact description,read a book on UNIX programming. -JP]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53556"></A>UNIX shells let you redirect the input and output of programswith operators like <CODECLASS="literal">&gt;</CODE> and <CODECLASS="literal">|</CODE>.How does that work?How can you use it better?Here's an overview.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53561"></A>When the UNIX kernel starts any<SPANCLASS="link">process (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>)</SPAN>-for example, <EMCLASS="emphasis">grep</EM>, <EMCLASS="emphasis">ls</EM>, or a shell&nbsp;- it sets up several places for thatprocess to read from and write to.<ACLASS="xref"HREF="ch45_20.htm#UPT-ART-2050-FIG-0"TITLE="Open Standard I/O Files with No Command-Line Redirection">Figure 45.1</A>shows that.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-2050-FIG-0">Figure 45.1: Open Standard I/O Files with No Command-Line Redirection</A></H4><IMGCLASS="graphic"SRC="figs/2050a.gif"ALT="Figure 45.1"><PCLASS="para">These places are called <EMCLASS="emphasis">open files</EM>.The kernel gives each file a number called a <EMCLASS="emphasis">file descriptor</EM>.But people usually use names for these places instead of the numbers:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53577"></A>The <EMCLASS="emphasis">standard input</EM> or <EMCLASS="emphasis">stdin</EM> (File Descriptor (F.D.) number 0) is the place wherethe process can read text.This might be text from other programs or from your keyboard.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53583"></A>The <EMCLASS="emphasis">standard output</EM> or <EMCLASS="emphasis">stdout</EM> (F.D. 1) is a place for the processto write its &quot;answers.&quot;</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53589"></A>The <EMCLASS="emphasis">standard error</EM> or <EMCLASS="emphasis">stderr</EM> (F.D. 2) is where the process can senderror messages.</P></LI></UL><PCLASS="para">By default, as<ACLASS="xref"HREF="ch45_20.htm#UPT-ART-2050-FIG-0"TITLE="Open Standard I/O Files with No Command-Line Redirection">Figure 45.1</A>shows, the file that's opened for <EMCLASS="emphasis">stdin</EM>, <EMCLASS="emphasis">stdout</EM>, and<EMCLASS="emphasis">stderr</EM> is <EMCLASS="emphasis">/dev/tty</EM>-a name for your terminal.This makes life easier for users&nbsp;- and programmers, too.The user doesn't have to tell a program where to read or writebecause the default is your terminal.A programmer doesn't have to open files to read or write from (in manycases); the programs can just read from <EMCLASS="emphasis">stdin</EM>, write to<EMCLASS="emphasis">stdout</EM>, and send errors to <EMCLASS="emphasis">stderr</EM>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53603"></A>This gets better.When the shell starts a process (when you type a command at a prompt),you can tell the shell what file to &quot;connect to&quot; any of those file descriptors.For example, <ACLASS="xref"HREF="ch45_20.htm#UPT-ART-2050-FIG-1"TITLE="Standard Output Redirected to a File">Figure 45.2</A>shows what happens when yourun <EMCLASS="emphasis">grep</EM> and make the shell redirect <EMCLASS="emphasis">grep</EM>'sstandard output away from the terminal to a file named <EMCLASS="emphasis">grepout</EM>.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-2050-FIG-1">Figure 45.2: Standard Output Redirected to a File</A></H4><IMGCLASS="graphic"SRC="figs/2050b.gif"ALT="Figure 45.2"><ACLASS="indexterm"NAME="AUTOID-53613"></A><ACLASS="indexterm"NAME="AUTOID-53615"></A><PCLASS="para">Programs can read and write files besides the ones on <EMCLASS="emphasis">stdin</EM>,<EMCLASS="emphasis">stdout</EM>, and <EMCLASS="emphasis">stderr</EM>.For instance, in <ACLASS="xref"HREF="ch45_20.htm#UPT-ART-2050-FIG-1"TITLE="Standard Output Redirected to a File">Figure 45.2</A>,<EMCLASS="emphasis">grep</EM> opened the file<EMCLASS="emphasis">somefile</EM> itself&nbsp;- it didn't use any of the standard file descriptorsfor <EMCLASS="emphasis">somefile</EM>.A UNIX convention is that if you don't name any files on the commandline, a program will read from its standard input.Programs that work that way are called<SPANCLASS="link"><EMCLASS="emphasis">filters</EM> (<ACLASS="linkend"HREF="ch01_30.htm"TITLE="Redirecting Input and Output ">1.30</A>)</SPAN>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53629"></A><ACLASS="indexterm"NAME="AUTOID-53632"></A><ACLASS="indexterm"NAME="AUTOID-53635"></A><ACLASS="indexterm"NAME="AUTOID-53638"></A><ACLASS="indexterm"NAME="AUTOID-53641"></A><ACLASS="indexterm"NAME="AUTOID-53644"></A>All shells can do basic redirection with <EMCLASS="emphasis">stdin</EM>, <EMCLASS="emphasis">stdout</EM>, and<EMCLASS="emphasis">stderr</EM>.But, as you'll see in article<ACLASS="xref"HREF="ch45_21.htm"TITLE="n&gt;&amp;m: Swap Standard Output and Standard Error ">45.21</A>,the Bourne shell also handles file descriptors 3 through 9.That's useful sometimes:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Maybe you have a few data files that you want to keep reading from orwriting to.Instead of giving their names, you can use the file descriptor numbers.</P></LI><LICLASS="listitem"><PCLASS="para">Once you open a file, the kernel remembers what place in the file youlast read from or wrote to.Each time you use that file descriptor number while the file is open,you'll be at the sameplace in the file.That's especially nice when you want to read from or write to the same filewith more than one program.For example, the <EMCLASS="emphasis">line</EM> command on some UNIX systems reads one linefrom a file&nbsp;- you can call <EMCLASS="emphasis">line</EM> overand over, whenever you want to read the next line from a file.Once the file has been opened, you can<SPANCLASS="link">remove its link (name) from the directory (<ACLASS="linkend"HREF="ch45_10.htm"TITLE="Removing a File Once It's Opened&nbsp;- for Security and Easy Cleanup">45.10</A>)</SPAN>;the process can access the file through its descriptor without using the name.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53661"></A>When UNIX starts a new<SPANCLASS="link">subprocess (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>)</SPAN>,the open file descriptors are given tothat process.A subprocess can read or write from file descriptors opened by its parentprocess.A redirected-I/O loop, as in articles<ACLASS="xref"HREF="ch45_22.htm"TITLE="Handling Files Line-by-Line ">45.22</A>and<ACLASS="xref"HREF="ch45_23.htm"TITLE="The Ins and Outs of Redirected I/O Loops ">45.23</A>,takes advantage of this.</P></LI></UL><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="ch45_19.htm"TITLE="45.19 A while Loop with Several Loop Control Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.19 A while Loop with Several Loop Control Commands "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="ch45_21.htm"TITLE="45.21 n&gt;&amp;m: Swap Standard Output and Standard Error "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.21 n&gt;&amp;m: Swap Standard Output and Standard Error "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.19 A while Loop with Several Loop Control Commands </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">45.21 n&gt;&amp;m: Swap Standard Output and Standard Error </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 + -