【发布时间】: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适合所有这些。