【问题标题】:How can I redirect stderr to a file?如何将 stderr 重定向到文件?
【发布时间】:2014-09-30 15:26:07
【问题描述】:

在 unix 中将标准错误描述符重定向到名为 error.txt 的文件所需的命令是什么?

到目前为止我有这个命令:

find / -name "report*" ________ error.txt

【问题讨论】:

    标签: bash unix find


    【解决方案1】:

    您可以像这样使用标准错误处理程序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)

    注意与&amp;&gt;error.txt 的区别,它同时重定向标准输入和标准错误(参见Redirect stderr and stdout in a bash scriptHow 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
    

    【讨论】:

      猜你喜欢
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 2021-06-29
      相关资源
      最近更新 更多