需要运行和外部命令,将输出写入日志文件并陷阱退出状态
my $cmd = "db2 \"insert into schema.tablea (id, name, city) values (99, 'Micheal', 'London')\" ";
open my $log, ">", "logfile.log";
my $rt = open(OUTPUT,"$cmd 2>&1 | " );
while (<OUTPUT>){
chomp;
print $log $_, "\n";
print $_, "\n";
}
close(OUTPUT);
close($log);
print "Exit status is $rt\n";
任何帮助都是相关的。
答案 0 :(得分:1)
close
会将$?
设置为system
。
die $! if $? == -1;
die "Killed by ".( $? & 0x7F ) if $? & 0x7F;
die "Exited with ".( $? >> 8 ) if $? >> 8;
如果你还要打算出局,你也可以考虑
use String::ShellQuote qw( shell_quote );
system("$cmd 2>&1 | tee ".shell_quote($log));
答案 1 :(得分:0)
您可以使用Capture :: Tiny:
use Capture::Tiny ':all';
use File::Slurp;
use autodie;
($stdout, $stderr, $exit) = tee sub {
system("echo hello; exit 15");
};
write_file('logfile.log', $stdout);
print "exit: ", $exit >> 8, "\n";
有关“$ exit&gt;&gt; 8”的说明,请参阅“perldoc -f system”。