说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/ioredirect.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
17.12. I/O Redirection
每个进程开始时都有三个打开的文件流:标准输入(stdin)、标准输出(stdout)和标准错误(stderr)。除了提供访问存储文件的文件接口之外,Unix 还为它实现的所有抽象都提供了文件接口,包括终端或键盘这样的设备。stdin 文件流是进程读取输入的地方,stdout 和 stderr 是进程写出输出和错误输出的地方。默认情况下,stdin 从键盘读取,stdout 和 stderr 写到终端。不过,你可以用 I/O 重定向(I/O redirection)改变程序的 stdin、stdout 和 stderr 的来源或去向。
Every process begins with three open file streams: standard in (stdin), standard out (stdout), and standard error (stderr). In addition to providing an file interface to access stored files, Unix provides a file interface to all of the abstractions it implements, including devices like the terminal or the keyboard. The stdin file stream is where the process’s input is read from, and stdout and stderr are where the process’s output and error output are written. By default stdin reads from the keyboard and stdout and stderr write to the terminal. However, you can change where a program’s stdin, stdout, and stderr originate from or redirect to using I/O redirection.
stdin、stdout 和 stderr 各有一个与之关联的文件描述符(file descriptor)。文件描述符是一个指向某个打开文件的正整数值。Unix 为许多不是普通文件和目录的东西实现了文件接口。一个例子是为键盘或终端这样的设备实现的文件接口。新进程启动时,它带着三个分别对应于 stdin、stdout 和 stderr 的已打开「文件」开始运行,它们的文件描述符分别是 0、1 和 2。在命令行上执行时,这些文件描述符的值指定了重定向的是 stdin、stdout 还是 stderr:
Each of stdin, stdout, and stderr has a file descriptor associated with it. A file descriptor is a positive integer value that refers to an open file. Unix implements a file interface to many things that are not regular files and directories. One example is a file interface to devices like a keyboard or a terminal. When a new process starts up, it starts with three open “files” corresponding to stdin, stdout, and stderr with file descriptors 0, 1, and 2, respectively. These file descriptor values specify which of stdin, stdout, or stderr, are being redirected when executed at the command line:
译注
严格说来文件描述符是非负整数,因为 stdin 的描述符就是 0。
<重定向 stdin
<redirect stdin
>或1>重定向 stdout
>or1>redirect stdout
2>重定向 stderr。
2>redirect stderr.
在一条命令中可以组合使用这些运算符,把 stdin、stdout 和 stderr 中的一个或多个重定向到文件,覆盖文件原有的内容。shell 还提供了简写记法,用于把 stdout 和 stderr 合并重定向到同一个文件,以及把输出追加到已有的文件中:
These operators can be combined in a command to redirect one or more of stdin, stdout, and stderr to a file, overwriting its contents. The shell also provides shorthand notation to combine stdout and stderr redirection to a single file, and to append output to an existing file:
&>把 stdin 和 stdout 重定向到同一个文件。注意,&>[outfile]等价于1> [outfile] 2>&1,即把 stdout 重定向到outfile,再把 stderr 重定向到 stdout。
&>redirect stdin and stdout to same file. Note that&>[outfile]is equivalent to1> [outfile] 2>&1, which redirects stdout tooutfileand redirects stderr to stdout.
译注
第一句中的「stdin 和 stdout」应为「stdout 和 stderr」,后一句的等价写法以及下文的例子都表明了这一点。
>>把 stdout 追加到文件中。
>>append stdout to a file.
下面是一些例子:
Here are a few examples:
- 用
>或1>把 stdout 重定向到其他文件(我们用wc检查这些文件大小相同,并在调用cat之后用diff确认它们完全相同):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17$ wc quote
4 26 160 quote
$ cat quote
The function of education is to teach one
to think intensively and to think critically.
Intelligence plus character - that is the
goal of true education.
$ cat quote > another # >: redirect cat's stdout to file "another"
$ wc another
4 26 160 another
$ cat quote 1> yetanother # 1>: redirect cat's stdout to a file (same as >)
$ diff -s another yetanother # check to see that both files are identical
Redirect stdout to other files using
>or1>(we usewcto check that the files are the same sizes, anddiffto see that they are identical after the call tocat):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 $ wc quote
4 26 160 quote
$ cat quote
The function of education is to teach one
to think intensively and to think critically.
Intelligence plus character - that is the
goal of true education.
$ cat quote > another # >: redirect cat's stdout to file "another"
$ wc another
4 26 160 another
$ cat quote 1> yetanother # 1>: redirect cat's stdout to a file (same as >)
$ diff -s another yetanother # check to see that both files are identical
- 用
<从文件重定向 stdin:1
2$ wc < quote # <: redirect wc's stdin from "quote" file
4 26 160 quote
Redirect stdin from a file using
<:
1
2 $ wc < quote # <: redirect wc's stdin from "quote" file
4 26 160 quote
译注
用 < 重定向时,wc 是从标准输入读取的,并不知道文件名,所以实际输出中最后不会有 quote 这个名字。译者在 Mac 上验证:wc quote 的输出末尾有文件名,而 wc < quote 只有三个数字。
- 用
2>把 stderr 重定向到文件:1
2
3
4
5
6$ cat blah
cat: blah: No such file or directory
$ cat blah 2> error_out # 2>: redirect stderr to a file "error_out"
$ wc error_out
1 7 37 error_out
Redirect stderr to a file using
2>:
1
2
3
4
5
6 $ cat blah
cat: blah: No such file or directory
$ cat blah 2> error_out # 2>: redirect stderr to a file "error_out"
$ wc error_out
1 7 37 error_out
- 用
>>把程序的 stdout 追加到文件中:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19$ echo "Again:" >> another # append echo's output to file "another"
$ wc another
5 27 167 another
$ sort quote >> another # append sort's output to file "another"
$ wc another
9 53 327 another
$ cat another
The function of education is to teach one
to think intensively and to think critically.
Intelligence plus character - that is the
goal of true education.
Again:
Intelligence plus character - that is the
The function of education is to teach one
goal of true education.
to think intensively and to think critically.
Append program’s stdout to a file using
>>:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 $ echo "Again:" >> another # append echo's output to file "another"
$ wc another
5 27 167 another
$ sort quote >> another # append sort's output to file "another"
$ wc another
9 53 327 another
$ cat another
The function of education is to teach one
to think intensively and to think critically.
Intelligence plus character - that is the
goal of true education.
Again:
Intelligence plus character - that is the
The function of education is to teach one
goal of true education.
to think intensively and to think critically.
- 用
&>把 stdout 和 stderr 都重定向到同一个文件(在这个例子中,文件blah不存在,所以cat会把一条错误信息写到 stderr;而文件quote存在,所以cat会把它的内容输出到 stdout):1
2
3
4
5
6
7
8$ cat blah quote &> err_out
$ cat err_out
cat: blah: No such file or directory
The function of education is to teach one
to think intensively and to think critically.
Intelligence plus character - that is the
goal of true education.
Redirect both stdout and stderr to the same file using
&>(in this example the fileblahdoesn’t exist, socatwill write an error message to stderr, and the filequotedoes exist, socatwill output its contents to stdout):
1
2
3
4
5
6
7
8 $ cat blah quote &> err_out
$ cat err_out
cat: blah: No such file or directory
The function of education is to teach one
to think intensively and to think critically.
Intelligence plus character - that is the
goal of true education.
- 从不同的文件重定向全部三者(在这个例子中,没有 stderr 输出,所以这次调用之后
grep_error文件应该是空的):1
2
3
4
5
6
7
8grep teach < another 1> grep_out 2> grep_error
$ cat grep_out
The function of education is to teach one
The function of education is to teach one
$ cat grep_err
$
Redirect all three from different files (in this example, there is no stderr output, so the
grep_errorfile should be empty after this call):
1
2
3
4
5
6
7
8 grep teach < another 1> grep_out 2> grep_error
$ cat grep_out
The function of education is to teach one
The function of education is to teach one
$ cat grep_err
$
译注
示例中最后查看的是 grep_err,与重定向时写的文件名 grep_error 不一致,应为 cat grep_error;第一行命令前也少了提示符 $。
17.12.1. 参考资料
17.12.1. References
更多信息:
For more information:
- Redirections from the Bash Reference Manual from gnu.org.
- cheat-sheets.org 上的最常用的 Unix 命令
- most used Unix commands from cheat-sheets.org