如何将 stderr 重定向到文件? [英] How can I redirect stderr to a file?

查看:29
本文介绍了如何将 stderr 重定向到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆