Search code examples
perlbatch-fileerrorlevel

What is the syntax for executing a Perl script and capturing its exit value in Windows batch script?


Possible Duplicate:
How do I get the application exit code from a Windows command line?

The Perl script that I'm calling inside batch returns a 1, 2, or 3. What's the syntax for calling this perl sript with an argument 829, and capturing the script's exit code?

 Perl.exe listMembership.pl 829 in cmd.exe

 @echo off
 set retVal=Perl.exe listMembership.pl 829
 echo %retVal%

Solution

  • Take a look at the output of if /? on the command line. Without cmd extensions, the lowest common denominator batch script would be something along the lines of:

    @echo off
    Perl.exe listMembership.pl 829
    if errorlevel 4 goto error
    if errorlevel 3 goto exit3
    if errorlevel 2 goto exit2
    if errorlevel 1 goto exit1
    if errorlevel 0 goto exit0
    
    :error
    echo:Unexpected exit code %ERRORLEVEL%
    goto end
    
    :exit3
    echo:Forbnicate
    goto end
    
    :exit2
    echo:Colormaticate
    goto end
    
    :exit1
    echo:Motorcade
    goto end
    
    :exit0
    echo:Is this really success?
    goto end
    
    :end
    echo:Done
    

    Keep in mind, errorlevel checks have to be in descending order because:

    ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.