如何重定向写入 tty 的程序?

发布于 2024-09-29 10:12:06 字数 1457 浏览 4 评论 0原文

这是未重定向的输出(如果您不知道 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' -----------------------
$

嘿!它正在重置我的重定向。这真的很烦人,我有两个问题:

  1. 我怎样才能实现我想要的,即将所有内容重定向到我的文件中
  2. 为什么他们要做这么奇怪的事情?

另请参阅相关问题。

编辑:有人在评论中问,所以一些细节。这是在 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:

  1. How can I achieve what I want, namely redirecting everything into my file
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱贱哒 2024-10-06 10:12:06

尝试一下:

script -q -c 'module help null' /dev/null > aaa.txt

这可以在 shell 脚本中(非交互式)使用,

$ script --version
script (util-linux-ng 2.16)

您也可以使用 expect

另请参阅:捕获到 /dev/tty 的直接重定向

Try this:

script -q -c 'module help null' /dev/null > aaa.txt

This works in a shell script (non-interactively) using

$ script --version
script (util-linux-ng 2.16)

You may also be able to use expect.

Also see: Catching a direct redirect to /dev/tty.

强者自强 2024-10-06 10:12:06

我首先回答第二个问题:作为一种设计选择,模块是一个 eval,他们选择使用 stderr/tty 而不是 stdout/stderr 来让他们的设计更容易。请参阅此处

由于我无法使用任何其他推荐的工具(例如脚本、expect),我的解决方案是以下 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

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:

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
青丝拂面 2024-10-06 10:12:06

看起来 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文