Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Capturing output of commands to a file

by parthodas (Acolyte)
on Nov 12, 2015 at 21:18 UTC ( [id://1147606]=perlquestion: print w/replies, xml ) Need Help??

parthodas has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Gurus,
As per my requirement, I need to capture the output of commands to file. These are normal perl commands, vss commands and windows commands. But for the simplicity of code, I just want to capture one windows statement. But the output is not getting captured as expected. My code looks something like this --
open my $FILEOUT, '>>', "$mydir/summary_log.txt" or die "Can't create +'$mycrdir/summary_log.txt'" ; while ( my @row = $sth->fetchrow_array() ) { system("ss GET $row[2]\/$row[1] -V$row[4] -GL$mydir > $FILEOUT 2>& +1"); }
When I'm running the code without the reroute option, I'm able to see on screen message. But when I'm running the code with routing option, the message is getting lost. Neither it is displaying on screen nor it is getting captured in the file.

Also, is it possible to display the message on the screen and route it to the file. Cause, when I'm routing the message the on screen display of message is not showing. On screen message looks something like this --
Version not found
Getting XLNXlogin.css

I tried using Tee and Capture, but they are giving compilation error. It seems these packages are not installed and they are not compiling.

Replies are listed 'Best First'.
Re: Capturing output of commands to a file
by GotToBTru (Prior) on Nov 12, 2015 at 21:29 UTC

    $FILEOUT contains the file handle, not the name, so redirecting to it in your command line will not work. File handles are only usable by perl commands. Perhaps:

    while ( my @row = $sth->fetchrow_array() ) { system("ss GET $row[2]\/$row[1] -V$row[4] -GL$mydir > $mydir/summ +ary_log.txt 2>&1"); }
    Dum Spiro Spero
      The solution worked. Thanks a lot. Looking for an option to display the message on screen too. If there is any option available.
        This is no longer a Perl question, but take a look at the tee Unix/Linux command.

        Update: oops, looking again at your OP, it seems that you are under Windows, not Unix. There are some Windows tee implementations (e.g. wintee or Powershell Tee) but I just don't know if they fit your requirement.

Re: Capturing output of commands to a file
by dasgar (Priest) on Nov 12, 2015 at 22:47 UTC

    You might want to take a look at Capture::Tiny. It's tee function can be used to run a system command where it's "output is captured as well as passed on to the original STDOUT and STDERR".

Re: Capturing output of commands to a file
by choroba (Cardinal) on Nov 12, 2015 at 21:32 UTC
    Your OS doesn't understand Perl's file handles. $FILEOUT in double quotes stringifies to something like GLOB(0x2205cb8). Always check the exit status of system:
    0 == system "ss GET $row[2]\/$row[1] -V$row[4] -GL$mydir > $mydir/sum +mary_log.txt 2>&1" or die "Can't run command: $?";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      The solution worked. Thanks a lot. Is there a way to display the message on screen too.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1147606]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-20 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found