bash 输出重定向对某个程序不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4459077/
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
Output redirection doesn't work for a certain program
提问by kshahar
On Linux, I'm trying to redirect stdout from a console application to a file instead of console. I do not have the source code. I tried several methods but all resulted in an empty file. Without output redirection everything works fine (I see console messages).
在 Linux 上,我试图将标准输出从控制台应用程序重定向到文件而不是控制台。我没有源代码。我尝试了几种方法,但都导致一个空文件。如果没有输出重定向,一切正常(我看到控制台消息)。
I tried, for example:
我试过,例如:
progname > out.txt
progname > out.txt 2&>1
And nothing appears in out.txt and in the console.
在 out.txt 和控制台中什么也没有出现。
I tried to run the application with strace. When I do not use redirection, I see lines such as -
我尝试使用 strace 运行该应用程序。当我不使用重定向时,我会看到诸如 -
write(1, "bla bla", 8)
When I introduce output redirection, there are no write calls at all, which makes me think the application is testing for something before writing the message. What is the application looking for? How can I bypass it?
当我引入输出重定向时,根本没有写入调用,这让我认为应用程序在写入消息之前正在测试某些内容。应用程序在寻找什么?我怎样才能绕过它?
I'm using CentOS 5.5 and Bash.
我正在使用 CentOS 5.5 和 Bash。
采纳答案by Laurence Gonsalves
Based on your strace it sounds like the program is testing if stdin and/our stdout is a tty. I've never used it but you could try using empty.sourceforge.net. It's suppose to create a pseudo-tty which should fool your program's check.
根据您的 strace,听起来该程序正在测试 stdin 和/我们的 stdout 是否为 tty。我从未使用过它,但您可以尝试使用 empty.sourceforge.net。假设创建一个伪 tty 来欺骗您的程序检查。
回答by plundra
progname > out.txt 2>&1
Edit: If you actually did this and it was a typo in your question. It might be possible that the application checks if the output is a TTY.
编辑:如果您确实这样做了并且您的问题中有错字。应用程序可能会检查输出是否为 TTY。
Edit2:Ok, in that case, try script -qc progname > out.txt > 2>&1
Edit2:好的,在这种情况下,尝试script -qc progname > out.txt > 2>&1
In Debian/Ubuntu, scriptis a part of the bsdutilspackage which is an Essential package and always installed.
在 Debian/Ubuntu 中,script是bsdutils软件包的一部分,它是基本软件包并且始终安装。
回答by l0b0
Try to redirect a single stream at a time to /dev/null, to see which one it prints on:
尝试一次将一个流重定向到 /dev/null,以查看它打印在哪个流上:
progname > /dev/null
progname 2> /dev/null
Then you'll see which stream you have to capture. For technical reasons some programs print even informational messages on stdout.
然后您将看到您必须捕获哪个流。由于技术原因,一些程序甚至在标准输出上打印信息性消息。

