【发布时间】:2014-09-30 15:26:07
【问题描述】:
在 unix 中将标准错误描述符重定向到名为 error.txt 的文件所需的命令是什么?
到目前为止我有这个命令:
find / -name "report*" ________ error.txt
【问题讨论】:
在 unix 中将标准错误描述符重定向到名为 error.txt 的文件所需的命令是什么?
到目前为止我有这个命令:
find / -name "report*" ________ error.txt
【问题讨论】:
您可以像这样使用标准错误处理程序2:
find / -name "report*" 2>error.txt
看一个例子:
$ ls a1 a2
ls: cannot access a2: No such file or directory <--- this is stderr
a1 <--- this is stdin
$ ls a1 a2 2>error.txt
a1
$ cat error.txt
ls: cannot access a2: No such file or directory <--- just stderr was stored
如BASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File ) 中所述,这些是处理程序:
| Handle | Name | Description |
|---|---|---|
| 0 | stdin | Standard input (stdin) |
| 1 | stdout | Standard output (stdout) |
| 2 | stderr | Standard error (stderr) |
注意与&>error.txt 的区别,它同时重定向标准输入和标准错误(参见Redirect stderr and stdout in a bash script 或How to redirect both stdout and stderr to a file):
$ ls a1 a2 &>error.txt
$ cat error.txt
ls: cannot access a2: No such file or directory <--- stdin and stderr
a1 <--- were stored
【讨论】: