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

I have writtten a perl script which at some point executes a batch file via the system() command.This batch file in turn executes another batch file after some processing.This second batch file dumps either success or failure messages on to the command prompt.How do i redirect this output to another file? OR collect this output in another file? Since this STDOUT is not directly via the perl script...
  • Comment on Collecting the output of command prompt

Replies are listed 'Best First'.
Re: Collecting the output of command prompt
by grep (Monsignor) on Jan 09, 2002 at 05:21 UTC
    You should look up `` backticks (it's in perlop under Quotes) or piped opens.

    If your other programs require something more sophisticated look into Expect.pm

    This is also a FAQ 'perldoc -q command output'

    grep
    grep> cd pub grep> more beer
Re: Collecting the output of command prompt
by vladb (Vicar) on Jan 09, 2002 at 07:20 UTC
    Actually, did you try using the Super Search to look for post raising similar questions? It's actually a good idea to do so, since many times you'll discover how much easier it is to read a readily available answer to a question very similar to yours that has long been posted on this site by someone else.

    Actually, I did come across quite a few nodes with similar content. Here's a few posts you might want to look at:

    Hopefully these posts would explain a bit more ;-).

    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
Re: Collecting the output of command prompt
by gt8073a (Hermit) on Jan 09, 2002 at 14:03 UTC

    This batch file in turn executes another batch file after some processing.This second batch file dumps either success or failure messages on to the command prompt

    Can you modify your system calls so they direct output?
    file2 1>out.file 2>err.file

    don't forget that system "bypasses any shell processing of the command" if you use more than 1 argument.

    ## assumes $file2 is executable ## won't work system( $file2, $out, $err ) ## will work system( "$file2 $out $err" )

    You may or may not want to use this method, though, depending on your actual circumstances. Later, you can read $out and $err if need be.

    Will perl for money
    JJ Knitis
    (901) 756-7693
    gt8073a@industrialmusic.com

Re: Collecting the output of command prompt
by JojoLinkyBob (Scribe) on Jan 09, 2002 at 08:27 UTC
    Not sure if makes a difference but are you calling the nested .bat via with the "call" command? I
    =~Desertcoder
      It does make a difference, without call the batch won't return to the caller for further processing.

      --
      perl -pe "s/\b;([st])/'\1/mg"

Re: Collecting the output of command prompt
by dmmiller2k (Chaplain) on Jan 09, 2002 at 21:24 UTC

    In case you are struggling with capturing STDOUT from a MS-DOS batch file, the Win/DOS equivalent of the UNIX command, 'script 2>&1 >file' is 'cmd /c script.bat 2>&1 >file'.

    If you prefix the command line you are using within backticks with 'cmd /c', you should be able to capture the output.

    Caveat: don't use 'call' with redirection! It doesn't work.

    Update: reformatted text, added caveat.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime