bash 将标准输出重定向到文件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13466379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:49:27  来源:igfitidea点击:

Redirection of stdout to a file not working

linuxbashio-redirection

提问by mpenkov

I have a script that uses subprocesses to fetch HTML:

我有一个使用子进程来获取 HTML 的脚本:

misha@misha-K42Jr:~/git/domain_classifier$ python webkit_retrieve.py error-cut.txt html/error -N 5
http://kurabo.co.jp HostNotFoundError
http://monarch.com HostNotFoundError
http://nssmgmt.com HostNotFoundError
http://sbcglobal.net HostNotFoundError
http://dynamixcorp.com SslHandshakeFailedError
http://groupe-synox.com RemoteHostClosedError
QFont::setPixelSize: Pixel size <= 0 (0)
http://www.cnn.com NoError
http://pacbell.net TimeoutError

If I run the same script, but redirect output to a file, I get nothing in the output:

如果我运行相同的脚本,但将输出重定向到文件,则输出中什么也没有:

misha@misha-K42Jr:~/git/domain_classifier$ python webkit_retrieve.py error-cut.txt html/error -N 5 > stdout.txt
QFont::setPixelSize: Pixel size <= 0 (0)
misha@misha-K42Jr:~/git/domain_classifier$ cat stdout.txt
misha@misha-K42Jr:~/git/domain_classifier$

Why is the output empty? Should it not contain the same things that were printed to stdout in the first case?

为什么输出为空?它不应该包含在第一种情况下打印到 stdout 的相同内容吗?

采纳答案by mpenkov

I had a QApplication as part of the script. It seems like that is somehow affecting the output redirection. Other scripts without a QApplication seem to redirect as expected. It could be a bug somewhere, but I can't be bothered tracking it.

我有一个 QApplication 作为脚本的一部分。这似乎以某种方式影响了输出重定向。没有 QApplication 的其他脚本似乎按预期重定向。这可能是某个地方的错误,但我无法费心跟踪它。

回答by Saddam Abu Ghaida

use &>for redirection, this should redirect stdoutand stderrto designated file

使用&>重定向,这应该重定向stdoutstderr到指定的文件

回答by mcalex

You have sent stdout to the file, but your program is reporting errors which go to stderr. To setup redirection of stderr, use 2>syntax.

您已将 stdout 发送到该文件,但您的程序正在报告错误,这些错误将发送到 stderr。要设置 stderr 的重定向,请使用2>语法。

This link might help: http://www.tldp.org/LDP/abs/html/io-redirection.html

此链接可能会有所帮助:http: //www.tldp.org/LDP/abs/html/io-redirection.html

回答by David

Your output is going to stderror. Try redirecting both stderror and stdout using the following:

您的输出将进入 stderror。尝试使用以下命令重定向 stderror 和 stdout:

<command> 2>&1 <file>