我试图捕获使用Perl的system函数执行的输出,并将系统命令的输出重定向到一个文件,但由于某些原因,我没有获得完整的输出。
我使用以下方法:
system("example.exe >output.txt");这段代码出了什么问题,或者有没有其他方法来做同样的事情?
发布于 2011-10-18 08:48:25
和MVS's answer一样,但是很现代,很安全。
use strict;
use warnings;
open (my $file, '>', 'output.txt') or die "Could not open file: $!";
my $output = `example.exe`;
die "$!" if $?;
print $file $output;更简单
use strict;
use warnings;
use autodie;
open (my $file, '>', 'output.txt');
print $file `example.exe`;如果同时需要STDOUT和STDERR
use strict;
use warnings;
use autodie;
use Capture::Tiny 'capture_merged';
open (my $file, '>', 'output.txt');
print $file capture_merged { system('example.exe') };发布于 2011-10-18 04:24:44
使用plain >重定向输出只会捕获STDOUT。如果您还想捕获标准错误,请使用2>&1:
perl -e 'system("dir blablubblelel.txt >out.txt 2>&1");' 有关更多详细信息,请参阅Perlmonks
发布于 2014-02-26 21:11:01
当您想要recirect永久输出时,您可以这样做:
#redirect STDOUT before calling other functions
open STDOUT,'>','outputfile.txt' or die "can't open output";
system('ls;df -h;echo something'); #all will be redirected.https://stackoverflow.com/questions/7799045
复制相似问题