如何将 stderr 重定向到文件? [英] How can I redirect stderr to a file?
问题描述
在 unix 中将标准错误描述符重定向到名为 error.txt 的文件所需的命令是什么?
What is the command required to redirect the standard error descriptor to a file called error.txt in unix?
到目前为止我有这个命令:
I have this command so far:
find / -name "report*" ________ error.txt
推荐答案
你可以像这样使用stderr handler 2
:
You can use the stderr handler 2
like this:
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 (redirectstderr 到 File ),这些是处理程序:
As read in BASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File ), these are the handlers:
把手 | 姓名 | 说明 |
---|---|---|
0 | 标准输入 | 标准输入(stdin) |
1 | 标准输出 | 标准输出(stdout) |
2 | 标准错误 | 标准错误(stderr) |
注意与 &>error.txt
的区别,它同时重定向标准输入和标准错误(参见 在 bash 脚本中重定向标准错误和标准输出 或如何将标准输出和标准错误都重定向到文件):
Note the difference with &>error.txt
, that redirects both stdin and stderr (see Redirect stderr and stdout in a bash script or 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
这篇关于如何将 stderr 重定向到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!