【问题标题】:Why does "ls not_existing_file 2&>1 > log.txt" not write anything to log.txt? [duplicate]为什么“ls not_existing_file 2&>1 > log.txt”不向 log.txt 写入任何内容? [复制]
【发布时间】:2021-09-15 08:13:43
【问题描述】:

我想将ls 的stout 和stderr 重定向到log.txt。为什么这个命令

ls not_existing_file 2>&1 > log.txt

导致一个空的 log.txt 文件?

【问题讨论】:

  • 因为你的重定向顺序不对:ls not_existing_file > log.txt 2>&1
  • 还有2&>1 != 2>&1 ?
  • @Jetchisel 这是问题中的错字,我已修正。
  • @zwithouta,好吧,我认为这是 zsh 中的一件事 :-)
  • @JamesBrown 更新了问题

标签: bash zsh


【解决方案1】:

这是由于您试图将重定向合并运算符 stdout 的输出重定向到日志文件而不是命令本身的输出。

您可以做的是将命令的标准输出重定向到日志文件中,然后附加重定向合并运算符,如下所示: ls not_existing_file > log.txt 2>&1

How to redirect and append both standard output and standard error to a file with Bash也回答了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 2015-10-13
    • 2020-04-28
    相关资源
    最近更新 更多