【问题标题】:bash redirect stdout and stderr with forceful clobberbash 用强力破坏重定向标准输出和标准错误
【发布时间】:2013-11-25 02:50:05
【问题描述】:

我在 .bashrc 中将 bash 设置为 noclobber 模式 - 来自 bash 手册页:

设置 [--abefhkmnptuvxBCHP] [-o 选项] [arg ...] ... -C 如果设置,bash 不会用 >、>& 和重定向运算符。这可能是 使用创建输出文件时被覆盖 重定向运算符 >|而不是>。

有时我想覆盖此行为,因此我使用>| 重定向运算符来执行此操作。但有时我想将 stdout stderr 都重定向到一个文件。该文件可能已经存在,所以我可能想破坏这个文件。鉴于记录在案的运算符>&>|,我希望还会有>|&>&|&>| 运算符,它将stdout 和stderr 重定向到文件,并进行强制覆盖。但是我找不到这样一个运营商:

$ ls file.txt missing
ls: missing: No such file or directory
file.txt
$ ls file.txt missing >& output.txt
bash: output.txt: cannot overwrite existing file
$ ls file.txt missing >&| output.txt
bash: syntax error near unexpected token `|'
$ ls file.txt missing >|& output.txt
bash: syntax error near unexpected token `&'
$ ls file.txt missing &>| output.txt
bash: syntax error near unexpected token `|'
$ 

我知道我可以使用 >|2>&1 的组合来做到这一点,但我宁愿用一个运算符来做到这一点:

$ ls file.txt missing >| output.txt 2>&1
$ cat output.txt 
ls: missing: No such file or directory
file.txt
$ 

这样的运营商是否存在、记录在案或以其他方式存在?


我注意到我所询问的内容似乎有一些优先级 - 特别是在这些运算符的“附加”版本中。在 bash 4 中,引入了 &>> 运算符,它允许将 stdout stderr 附加到输出文件中。但似乎没有“clobber”版本。 How can I redirect and append both stdout and stderr to a file with Bash?

【问题讨论】:

    标签: bash stderr overwrite io-redirection


    【解决方案1】:

    恐怕不会。您必须使用>|...2>&1 解决方案。

    【讨论】:

      猜你喜欢
      • 2010-10-12
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 2014-03-22
      相关资源
      最近更新 更多