重定向不起作用 [英] Redirection Doesn't Work

查看:106
本文介绍了重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将程序的输出放入文件中.我输入了以下内容:

I want to put my program's output into a file. I keyed in the following :

./prog > log 2>&1

但是文件日志" 中没有任何内容.我正在使用Ubuntu 11.10,默认外壳为bash.

But there is nothing in the file "log". I am using the Ubuntu 11.10 and the default shell is bash.

任何人都知道造成这种情况的原因以及如何调试它?

Anybody know the cause of this AND how I can debug this?

推荐答案

有很多可能的原因:

  • The program reads the input from log file while you try to redirect into it with truncation (see Why doesn't "sort file1 > file1" work?)
  • The output is buffered so that you don't see data in the file until the output buffer is flushed. You can manually call fflush or output std::flush if using C++ I/O stream etc.
  • The program is smart enough and disables output if the output stream is not a terminal.
  • You look at the wrong file (i.e. in another directory).
  • You try to dump file's contents incorrectly.
  • Your program outputs '\0' as the first character so the output appears to be empty, even though there is some data.
  • Name your own.

您最好的选择是在调试器(如gdb)下运行此应用程序,或使用straceptrace(或两者)查看程序在做什么.我的意思是,实际上,输出重定向在过去的40年中一直有效,因此问题一定存在于其他地方.

Your best bet is to run this application under a debugger (like gdb) or use strace or ptrace (or both) and see what the program is doing. I mean, really, output redirection works for the last like 40 years, so the problem must be somewhere else.

这篇关于重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆