bash 如何重定向写入 tty 的程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4056075/
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
How to redirect a program that writes to tty?
提问by Davide
This is the un-redirected output (if you don't know what moduleis, it doesn't matter much):
这是未重定向的输出(如果您不知道是什么module,则无关紧要):
$ module help null
----------- Module Specific Help for 'null' -----------------------
This module does absolutely nothing.
It's meant simply as a place holder in your
dot file initialization.
Version 3.2.6
Suppose I'd like to redirect that to a file....
假设我想将它重定向到一个文件......
$ module help null > aaa.txt
----------- Module Specific Help for 'null' -----------------------
This module does absolutely nothing.
It's meant simply as a place holder in your
dot file initialization.
Version 3.2.6
$ cat aaa.txt
$
Well, it must be on the stderr
嗯,它必须在 stderr
$ module help null 2> aaa.txt
This module does absolutely nothing.
It's meant simply as a place holder in your
dot file initialization.
Version 3.2.6
$ cat aaa.txt
----------- Module Specific Help for 'null' -----------------------
$
Hey! It is resetting my redirect. This is really annoying, and I have two questions:
嘿!它正在重置我的重定向。这真的很烦人,我有两个问题:
- How can I achieve what I want, namely redirecting everything into my file
- Why are they doing such a weird thing?
- 我怎样才能实现我想要的,即将所有内容重定向到我的文件中
- 他们为什么要做这么奇怪的事情?
See also thisrelated question.
另请参阅此相关问题。
EDIT: somebody asked in a comment, so some details. This is on AIX 5.3 at 64 bits. I have python 2.6.5 almost fully available. I have both gcc 4.1.1 and gcc 4.5.1 but not many libraries to link them against (the util-linux-ng library, which contains the script version mentioned in an answer fails to compile for the getopt part). I also have several version of IBM XL compiler xlc. The reason why I didn't specify in the first place is that I was hoping in some shell tricks, maybe with exec, not in an external program.
编辑:有人在评论中问,所以一些细节。这是在 64 位的 AIX 5.3 上。我有几乎完全可用的python 2.6.5。我有 gcc 4.1.1 和 gcc 4.5.1 但没有多少库可以链接它们(util-linux-ng 库,其中包含一个答案中提到的脚本版本无法为 getopt 部分编译)。我也有几个版本的 IBM XL 编译器 xlc。我没有首先指定的原因是我希望使用一些 shell 技巧,也许是使用 exec,而不是在外部程序中。
采纳答案by Davide
I'm answering the second question first: as a design choice, module is an eval and they took the (questionable) choice to use stderr/tty instead of stdout/stderr to keep their side of the design easier. See here.
我首先回答第二个问题:作为一个设计选择,模块是一个 eval 并且他们(有问题的)选择使用 stderr/tty 而不是 stdout/stderr 来让他们的设计更容易。见这里。
My solution, since I couldn't use any of the other recommended tools (e.g. script, expect) is the following python mini-wrapper:
我的解决方案,因为我不能使用任何其他推荐的工具(例如脚本,期望)是以下 python 迷你包装器:
import pty, os
pid, fd = pty.fork()
if pid == 0: # In the child process execute another command
os.execv('./my-progr', [''])
print "Execv never returns :-)"
else:
while True:
try:
print os.read(fd,65536),
except OSError:
break
回答by Paused until further notice.
Try this:
尝试这个:
script -q -c 'module help null' /dev/null > aaa.txt
This works in a shell script (non-interactively) using
这在 shell 脚本(非交互式)中使用
$ script --version
script (util-linux-ng 2.16)
You may also be able to use expect.
您也许还可以使用expect.
Also see: Catching a direct redirect to /dev/tty.
另请参阅:捕获到 /dev/tty 的直接重定向。
回答by Steve Emmerson
Looks like moduleis writing to /dev/tty, which is always the console associated with the process. If so, then I don't think you can do anything about it. Typically, this is done to guarantee that the person sees the message (assuming that the program was invoked interactively).
看起来module正在写入/dev/tty,它始终是与进程关联的控制台。如果是这样,那么我认为您对此无能为力。通常,这样做是为了保证此人看到消息(假设程序是交互调用的)。

