Capturing error codes returned from a batch file in Perl
I have a perl file that calls a batch file to do an installation of a 3rd party program.
Main.pl
system ("Installer.bat");
print "Error code is $? \n";
Installer.bat
@echo off
installer.exe
echo errorlevel is %errorlevel% > logfile
exit %errorlevel%
The error code 3010 is returned by the batch file and it suggests that a restart is required. However, the perl module prints 49664. I thought it was supposed to print 3010. Could someone please explain how this works? I want to get the error code for a restart required in my perl code and then do some cleanup work and restart the machine from the perl module.
The following related points are also not clear. - Windows batch files only have 255 error codes, so how can 3010 be returned as a error code? - This place suggests that we need to right shift the error code by 8 bits to get the native error code. So if I right shift 4开发者_运维知识库9664 by 8, I get 194 (which is still not the same as 3010). However I also note that 3010 Mod 256 = 194
Per http://search.cpan.org/perldoc?IPC::System::Simple:
As of IPC::System::Simple v0.06, the run subroutine when called with multiple arguments will make available the full 32-bit exit value on Win32 systems. This is different from the previous versions of IPC::System::Simple and from Perl's in-build system() function, which can only handle 8-bit return values.
Exit codes in batch files are broken, exit %errorlevel% will set the exit code for the batch file, but not the process!
@echo off
set err=3010
@%COMSPEC% /C exit %err% >nul
This will set the exit code of the process (cmd.exe probably) to 3010.
精彩评论