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

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

问题描述



到目前为止,我已经有了这个命令:



$ p $ find / -namereport *________ error.txt


解决方案

您可以像这样使用stderr处理程序 2
$ b

  find / -namereport *2> error.txt 

查看示例:

  $ ls a1 a2 
ls:无法访问a2:没有这样的文件或目录< ---这是stderr
a1< ---这是stdin
$ ls a1 a2 2> error.txt
a1
$ cat error.txt
ls:can not access a2:No such file or directory< --- just stderr was stored

阅读 BASH Shell:How To Redirect stderr标准输出(重定向st derr到一个文件),这些是处理程序:

 句柄名称描述
0 stdin标准输入(stdin)
1 stdout标准输出(stdout)
2 stderr标准错误(stderr)






请注意与&> error.txt 的不同之处,它们会重定向stdin和stderr(请参阅如何将stdout和stderr重定向到一个文件):

  $ ls a1 a2&>错误.txt 
$ cat error.txt
ls:无法访问a2:没有这样的文件或目录< --- stdin和stderr
a1< ---被存储了


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

解决方案

You can use the stderr handler 2 like this:

find / -name "report*" 2>error.txt

See an example:

$ 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

As read in BASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File ), these are the handlers:

Handle  Name    Description
0       stdin   Standard input   (stdin)
1       stdout  Standard output  (stdout)
2       stderr  Standard error   (stderr)


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天全站免登陆