Best way to capture output from system command to a text file?
Best way to capture output from system command to a text file?
I?m trying to capture output from using Perl?s system
function to execute and redirect a system command?s ouptut to a file, but for some reason I?m not getting the whole output.
I?m using the following method:
system("example.exe >output.txt");
What?s wrong with this code, or is there an alternative way of doing the same thing?
Answer by Frank Schmitt for Best way to capture output from system command to a text file?
Redirecting the output with plain > will only catch STDOUT. If you also want to catch STDERR, use 2>&1:
perl -e 'system("dir blablubblelel.txt >out.txt 2>&1");'
For more details, see Perlmonks
Answer by MVS for Best way to capture output from system command to a text file?
You could also try having Perl capture the output instead:
open(FILE, ">output.txt") or die "Could not open file: $!"; print FILE `example.exe`; close(FILE);
Answer by Joel Berger for Best way to capture output from system command to a text file?
Same as MVS's answer, but modern and safe.
use strict; use warnings; open (my $file, '>', 'output.txt') or die "Could not open file: $!"; my $output = `example.exe`; die "$!" if $?; print $file $output;
easier
use strict; use warnings; use autodie; open (my $file, '>', 'output.txt'); print $file `example.exe`;
if you need both STDOUT and STDERR
use strict; use warnings; use autodie; use Capture::Tiny 'capture_merged'; open (my $file, '>', 'output.txt'); print $file capture_merged { system('example.exe') };
Answer by Znik for Best way to capture output from system command to a text file?
When you want recirect output permanently, you can do:
#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.
Answer by Francisco Zarabozo for Best way to capture output from system command to a text file?
I find this way a very nice way to do it:
use warnings; use strict; use Capture::Tiny::Extended 'capture'; my ($out, $err, $ret) = capture { system 'example.exe'; }; $ret = $ret >> 8; print "OUT: $out\n"; print "ERR: $err\n"; print "RET: $ret\n";
Thanks DWGuru for commenting on Capture::Tiny::Extended. :-)
Answer by Anil Kongovi for Best way to capture output from system command to a text file?
This works:
In C code, you could have the below line to capture the required output:
system("example.exe > \"output.txt\"");
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71
0 comments:
Post a Comment