Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15340185
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类:

2008-12-24 10:22:10

file Descriptors
As discussed on page 260, before a process can read from or write to a file it must open that file. When a process opens a file, Linux associates a number (called a file descriptor) with the file. Each process has its own set of open files and its own file descriptors. After opening a file, a process reads from and writes to that file by referring to its file descriptor. When it no longer needs the file, the process closes the file, freeing the file descriptor.
A typical Linux process starts with three open files: standard input (file descriptor 0), standard output (file descriptor 1), and standard error (file descriptor 2). Often those are the only files the process needs. Recall that you redirect standard output with the symbol > or the symbol 1> and that you redirect standard error with the symbol 2>. Although you can redirect other file descriptors, because file descriptors other than 0, 1, and 2 do not have any special conventional meaning, it is rarely useful to do so.
The exception is in programs that you write yourself, in which case you control the meaning of the file descriptors and can take advantage of redirection.
Opening a file descriptor
The Bourne Again Shell opens files using the exec builtin as follows:

exec n> outfile

exec m< infile
 
The first line opens outfile for output and holds it open, associating it with file descriptor n. The second line opens infile for input and holds it open, associating it with file descriptor m.
Duplicating a file descriptor
The <& token duplicates an input file descriptor; use >& to duplicate an output file descriptor. You can duplicate a file descriptor by making it refer to the same file as another open file descriptor, such as standard input or output. Use the following format to open or redirect file descriptor n as a duplicate of file descriptor m:

exec n<&m
 
Once you have opened a file, you can use it for input and output in two different ways. First, you can use I/O redirection on any command line, redirecting standard output to a file descriptor with >&n or redirecting standard input from a file descriptor with <&n. Second, you can use the read (page 487) and echo builtins. If you invoke other commands, including functions (page 315), they inherit these open files and file descriptors. When you have finished using a file, you can close it with

exec n<&–
 
When you invoke the shell function in the next example, named mycp, with two arguments, it copies the file named by the first argument to the file named by the second argument. If you supply only one argument, the script copies the file named by the argument to standard output. If you invoke mycp with no arguments, it copies standard input to standard output.
tip: A function is not a shell script
The mycp example is a shell function; it will not work as you expect if you execute it as a shell script. (It will work: The function will be created in a very short-lived subshell, which is probably of little use.) You can enter this function from the keyboard. If you put the function in a file, you can run it as an argument to the . (dot) builtin (page 259). You can also put the function in a startup file if you want it to be always available (page 317).


luther@gliethttp:~$ exec 9<&0 做个分析:
exec 9<&0 表示将0文件描述符号duplicate到文件描述符号9,用来保存0描述符号句柄
exec 0 read line将从input据并读取数据,也就是从struct.c文件读取一行数据,
exec 0<&- 表示关闭0描述符号对应的文件,也就是由exec 0 exec 0<&9 表示将9描述符号duplicate到文件描述符号0,用来恢复0描述符号句柄

阅读(1880) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~