如何重定向写入 tty 的程序?
这是未重定向的输出(如果您不知道 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
假设我想将其重定向到一个文件......
$ 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
$
好吧,它必须在 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' -----------------------
$
嘿!它正在重置我的重定向。这真的很烦人,我有两个问题:
- 我怎样才能实现我想要的,即将所有内容重定向到我的文件中
- 为什么他们要做这么奇怪的事情?
另请参阅此相关问题。
编辑:有人在评论中问,所以一些细节。这是在 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,而不是在外部程序中。
This is the un-redirected output (if you don't know what module
is, it doesn't matter much):
$ 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
$ 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 this related 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下:
这可以在 shell 脚本中(非交互式)使用,
您也可以使用
expect
。另请参阅:捕获到 /dev/tty 的直接重定向。
Try this:
This works in a shell script (non-interactively) using
You may also be able to use
expect
.Also see: Catching a direct redirect to /dev/tty.
我首先回答第二个问题:作为一种设计选择,模块是一个 eval,他们选择使用 stderr/tty 而不是 stdout/stderr 来让他们的设计更容易。请参阅此处。
由于我无法使用任何其他推荐的工具(例如脚本、expect),我的解决方案是以下 python 迷你包装器:
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.
My solution, since I couldn't use any of the other recommended tools (e.g. script, expect) is the following python mini-wrapper:
看起来
module
正在写入/dev/tty
,它始终是与进程关联的控制台。如果是这样,那么我认为你对此无能为力。通常,这样做是为了保证该人看到该消息(假设该程序是交互式调用的)。Looks like
module
is 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).