Another team has a command shell that passes fills the ERRORLEVEL ( which is like a dos variable but it is not set with the SET command, it is a substitute only variable ). I would like to figure out how to change the Perl script to be able to deal with the command script, so I do not want to change the command script ( it is someone ele's script and I have to deal with it as it is). How can I get perl to read the value in ERRORLEVEL & it seems that ENV{ERRORLEVEL} does not work.
Here is the code I am testing ideas out on. You can see that the main callshowerror2.pl gets back the error code from whatever last command executed in the called cmd shell, which is not necessarily the ERRORLEVEL! The only thing I can do is modify the command shell script to do exit %ERRORLEVEL%. But as I stated above, I do not want to modify the command shell.
---------- CallShowError2.pl --------------------------
--
#CallShowError2.pl
use warnings;
use Cwd;
use Env;
my $bdsrez = 0;
print "************ showerror2.bat *************";
$bdsrez = exeCmd("showerror2.bat");
print "************* next showerror2andleave.bat ***********";
$bdsrez = exeCmd("showerror2andleave
.bat");
sub exeCmd {
my ( $cmd) = @_;
my $exeXC = 0;
my $noop = "";
#reset this buffer for this command output only
if ( open( CMD, "$cmd 2>&1 |" ) )
{ #this captures STDOUT *and* STDERR
while (<CMD>) {
print $_;
}
close(CMD); #by explicitly closing we force
#Perl to wait for $? from child proc
$exeXC = $?;
}
else {
$exeXC = $?;
print( "command fork failed: $!");
}
print( " exeCmd exit code=$exeXC\n");
return $exeXC;
}
----------------- showerror2.bat -------------------
cmd /C "exit 2"
::ECHO/Return at top (errorleve): %ERRORLEVEL%
@if ERRORLEVEL 1 goto end
:end
Echo Pass out last errorlevel
:: One way to force the proper errorlevel to be returned
:: is by exiting the cmd file with the exit statement
:: But I would rather figure out how to make the perl
:: script adapt. Inserting this next line makes everything work
:: exit %ERRORLEVEL%
----------------- showerror2andleave.bat --------------------------
cmd /C "exit 2"
Start Free Trial