Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

perl and windows batch

by leonidlm (Pilgrim)
on Jul 31, 2008 at 10:26 UTC ( [id://701385]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all
I am running a batch file from perl script. The problem is that I am interested in the output of the batch and it's return value (returned by "exit /B val" command).
Can someone suggest how I can obtain that ?

Replies are listed 'Best First'.
Re: perl and windows batch
by BrowserUk (Patriarch) on Jul 31, 2008 at 10:48 UTC

    Given error.bat:

    c:\test>type error.bat @echo off exit /b 123

    Use:

    system 'error.bat & exit ERRORLEVEL';; print $? >>8;; 123

    Note: Because Perl attempts to emulate *nix here, exit codes (ERRORLEVELs) are truncated to 8 bits.

    Or, skip using /b:

    c:\test>type error.bat @echo off exit 123 c:\test>p1 [0] Perl> system 'error.bat'; print $? >>8;; 123

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks all
      The BrowserUk solution worked for me! Very good explanation also :)
Re: perl and windows batch
by moritz (Cardinal) on Jul 31, 2008 at 10:32 UTC
Re: perl and windows batch
by MidLifeXis (Monsignor) on Jul 31, 2008 at 10:47 UTC

    If moritz's suggestion does not help, please post your code (how you are calling the batch file, how you are reading the return code, etc).

    --MidLifeXis

Re: perl and windows batch
by Bloodnok (Vicar) on Jul 31, 2008 at 12:37 UTC
    IMHO, the suggestions put forward thus far, whilst returning the status code back to the calling script, don't appear to address leonidlm's interest in the output from the called batch script - so why not just use backticks. That way, you gain access to the return code (via $?) and the output (via the return from the backtick invocation) e.g.

    my $output = `batch_file args`; die "Gone horribly wrong - $?" if $?; # or ... my @output = `batch_file args`; die "Gone horribly wrong - $?" if $?;

    .oO(Unless, of course, Windoze sees fit to not return the status code from the batch script, in which case...)

    my $output = `batch_file args && exit /b ERRORLEVEL`; die "Gone horribly wrong - $?" if $?; # or ... my @output = `batch_file args && exit /b ERRORLEVEL`; die "Gone horribly wrong - $?" if $?;

    ,or something similar, may suffice ... unless I'm missing something obvious?!

    Sits back and awaits flames, errm, constructive observations ;-)

    HTH ,

    At last, a user level that overstates my experience :-))
        Actually, I found that when I used exit /b in the batch file, I was not getting the return code in my perl. Once I stripped the /b I did.
Re: perl and windows batch
by Anonymous Monk on Jan 08, 2011 at 16:56 UTC
    b.bat:
    @echo off exit 3
    bb.bat:
    @echo off exit /b 3
    p.pl:
    sub test; test 'b.bat'; test 'call b.bat'; test 'b.bat & exit errorlevel'; test 'bb.bat'; test 'call bb.bat'; test 'bb.bat & exit errorlevel'; sub test { local $\ = "\n"; print "@_"; system @_; print ' exit code is: ', $? >> 8; }
    output: b.bat exit code is: 3 call b.bat exit code is: 3 b.bat & exit errorlevel exit code is: 3 bb.bat exit code is: 0 call bb.bat exit code is: 3 bb.bat & exit errorlevel exit code is: 3
      output:
      b.bat
        exit code is: 768
      call b.bat
        exit code is: 768
      b.bat & exit errorlevel
        exit code is: 768
      bb.bat
        exit code is: 0
      call bb.bat
        exit code is: 768
      bb.bat & exit errorlevel
        exit code is: 768

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-25 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found