如何将程序的输出同时指向控制台和日志文件?

时间:2022-11-27 13:51:22

How can I print the output to terminal and file at the same time?

如何同时将输出打印到终端和文件?

$ perl foo.pl > foout.txt

does not allow me to see live process.

不允许我看到实时流程。

Is there any way I can see the output process real time and getting at the end the output of the screen on a file?

有没有什么方法可以实时看到输出过程并在文件的屏幕输出结束?

4 个解决方案

#1


perl foo.pl | tee foout.txt

perl foo.pl | tee foout.txt

#2


The utility tee will do that.

实用T恤会做到这一点。

#3


See IO::Tee. This module will allow you to do this selectively with fine grained control within your program (there is also a less mature module called File::Tee which worked for me once but I would not recommend that for any serious project).

见IO :: Tee。这个模块允许你有选择地在你的程序中进行细粒度控制(还有一个不太成熟的模块,名为File :: Tee,它曾经为我工作过一次,但我不建议任何严肃的项目)。

See also Log4perl for really fine grained control over what gets logged where and how.

另请参阅Log4perl,以便对记录在何处和如何记录的内容进行非常精细的控制。

For one off usage from the command line, as others have recommended, you can, of course, utilize the command line utility tee if you have access to it.

对于命令行中的一次性使用,正如其他人所建议的那样,当然,如果您有权访问它,您可以使用命令行实用程序tee。

#4


Or you could pipe it into a perl programs to print to the screen and a log file (that is if you are not on Unix or have a tee program)

或者你可以把它管道到一个perl程序打印到屏幕和一个日志文件(即如果你不在Unix或有一个发球台程序)

perl foo.pl | perl myPipe.pl myFile.txt

perl foo.pl | perl myPipe.pl myFile.txt

where the data is captureed to myFile.txt and

将数据捕获到myFile.txt和

myPipe.pl is

#
open OUTFILE,">$ARGV[0]" or die "Unable to open file: $ARGV[0]\n";

while(<>)
{
    print;
    print OUTFILE;
}
close OUTFILE;

This reads a line of input from STDIN and prints it to the screen and then to the file. When the end is reached, it will close the file

这将从STDIN读取一行输入并将其打印到屏幕然后再打印到文件。到达结尾时,它将关闭文件

#1


perl foo.pl | tee foout.txt

perl foo.pl | tee foout.txt

#2


The utility tee will do that.

实用T恤会做到这一点。

#3


See IO::Tee. This module will allow you to do this selectively with fine grained control within your program (there is also a less mature module called File::Tee which worked for me once but I would not recommend that for any serious project).

见IO :: Tee。这个模块允许你有选择地在你的程序中进行细粒度控制(还有一个不太成熟的模块,名为File :: Tee,它曾经为我工作过一次,但我不建议任何严肃的项目)。

See also Log4perl for really fine grained control over what gets logged where and how.

另请参阅Log4perl,以便对记录在何处和如何记录的内容进行非常精细的控制。

For one off usage from the command line, as others have recommended, you can, of course, utilize the command line utility tee if you have access to it.

对于命令行中的一次性使用,正如其他人所建议的那样,当然,如果您有权访问它,您可以使用命令行实用程序tee。

#4


Or you could pipe it into a perl programs to print to the screen and a log file (that is if you are not on Unix or have a tee program)

或者你可以把它管道到一个perl程序打印到屏幕和一个日志文件(即如果你不在Unix或有一个发球台程序)

perl foo.pl | perl myPipe.pl myFile.txt

perl foo.pl | perl myPipe.pl myFile.txt

where the data is captureed to myFile.txt and

将数据捕获到myFile.txt和

myPipe.pl is

#
open OUTFILE,">$ARGV[0]" or die "Unable to open file: $ARGV[0]\n";

while(<>)
{
    print;
    print OUTFILE;
}
close OUTFILE;

This reads a line of input from STDIN and prints it to the screen and then to the file. When the end is reached, it will close the file

这将从STDIN读取一行输入并将其打印到屏幕然后再打印到文件。到达结尾时,它将关闭文件