【问题标题】:how ignore warnings to go in stderr如何在 stderr 中忽略警告
【发布时间】:2019-07-09 11:24:56
【问题描述】:

我正在运行一个命令,我只想得到 ‍‍‍‍stderr 文件中的错误而不是警告

➜  ~ pip2 install 0wned 2> error.txt 

Collecting 0wned
  Using cached https://files.pythonhosted.org/packages/85/fb/af45132a70fa67b7a40383066e284182ae649ce5c2a7839c06da51641a43/0wned-0.6.0-py2.py3-none-any.whl
Installing collected packages: 0wned

➜  ~ cat error.txt 

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/lib/python2.7/site-packages/0wned-0.6.0.dist-info'
Consider using the `--user` option or check the permissions.

我只想在error.txt 中得到错误,而不是警告。

我想要这个输出:

➜  ~ cat error.txt

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/lib/python2.7/site-packages/0wned-0.6.0.dist-info'
Consider using the `--user` option or check the permissions.

【问题讨论】:

  • 可能是grep '^ERROR:' error.txt?
  • 并非总是这样,错误信息之后可能会出现警告,我认为这是一种肮脏的方式:X 我不明白为什么警告应该进入标准错误!
  • 你对名字的解读太多了。 stderr 的目的是为程序实际结果以外的信息提供替代输出通道,以便 stdout 清除信息和诊断消息。错误消息、警告消息和其他类型的诊断之间的区别非常随意,stderr 适合所有这些。

标签: linux unix terminal


【解决方案1】:

您可以根据需要过滤标准错误:

 pip2 install 0wned 2> >( grep ERROR > only-errors.txt)

解释:

 2> >( command )

stderr 流重定向到command

grepv ERROR > only-errors.txt

将包含ERROR 的行复制到only-errors.txt 文件中


如果您想保留所有错误消息的副本,可以添加tee

 pip2 install 0wned 2> >( tee all.txt |  grep ERROR > only-errors.txt)

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多