Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

bash - How to redirect a program that writes to tty?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...