【发布时间】:2017-12-18 18:15:00
【问题描述】:
我正在成功使用
IO::Tee
将消息打印到标准输出和日志文件。我还希望能够从 system() 命令捕获标准输出和标准错误并将其发送到同一个文件。
我尝试了各种重定向和/或管道组合,但没有成功
带有一些注释掉失败尝试的简单脚本...
#!/usr/bin/env perl
use IO:Tee;
my $teelog = "tee.log"
open my $tee, ">", $teelog or die "open tee failed.\n";
my $tee = new IO:Tee(\*STDOUT, $tee);
print $tee "First line in log\n";
# This command should work. I want the date to go to screen and tee.log
#system("date | tee -a ${teelog}"); <- nothing goes to tee.log
#system("date >& ${teelog}") <- clobbers tee.log
#system("date >& $tee") <- generates syntax errors
# This command should fail. I want error msg to go to screen and tee.log
#system("jibberish | tee -a ${teelog}"); <- nothing goes to tee.log
#system("jibberish >& ${teelog}") <- clobbers tee.log
system("kibberish >& $tee") <- generates syntax errors
print $tee "Last line in log\n";
exit;
【问题讨论】: