0
我所谓的SQL文件通过我的Perl脚本,该脚本输出写入日志文件,如:Perl的执行sql,打印输出到一个文件,并写入屏幕
system("sqlplus -s schema/pwd\@dbname \@$sql_file > $log_file");
不过,我想将输出写入屏幕。有没有办法做到这一点(除了重新执行命令sans写入日志文件)?
我所谓的SQL文件通过我的Perl脚本,该脚本输出写入日志文件,如:Perl的执行sql,打印输出到一个文件,并写入屏幕
system("sqlplus -s schema/pwd\@dbname \@$sql_file > $log_file");
不过,我想将输出写入屏幕。有没有办法做到这一点(除了重新执行命令sans写入日志文件)?
您可以自己捕获结果并将它们发送到两个目标。
my $output = `sqlplus -s schema/pwd\@dbname \@$sql_file`;
print $output;
open(my $file, '>', $log_file) or die $!;
print {$file} $output;
close $file;
可以有效tee
命令的输出,并节省一些内存,使用管道读取其STDOUT
:
open(my $cmdfh, "sqlplus -s schema/pwd\@dbname \@$sql_file |") or die $!;
open(my $logfh, '>', $log_file) or die $!;
while (<$cmdfh>) {
print;
print {$logfh} $_;
}
close $logfh;
close $cmdfh;
请问'在这种情况下tee'工作? http://en.wikipedia.org/wiki/Tee_%28command%29 – chilemagic 2015-02-09 20:30:28
我会建议,而不是使用'system',你可能要考虑使用'DBI'模块。 – Sobrique 2015-02-09 21:55:15