Advertisement

02.14.2008 at 04:04PM PST, ID: 23164803 | Points: 500
[x]
Attachment Details

Passing ERRORLEVEL from command shell to Perl script

Asked by lqwest in Perl Programming Language

Tags:

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
[+][-]02.15.2008 at 05:53AM PST, ID: 20901588

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.15.2008 at 07:49AM PST, ID: 20902783

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.15.2008 at 08:36AM PST, ID: 20903327

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.15.2008 at 09:25AM PST, ID: 20903858

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.15.2008 at 09:39AM PST, ID: 20903997

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.16.2008 at 05:15AM PST, ID: 20909453

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.16.2008 at 09:31AM PST, ID: 20910566

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.19.2008 at 11:32AM PST, ID: 20931686

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.20.2008 at 06:38AM PST, ID: 20938133

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.15.2008 at 09:33AM PDT, ID: 22722997

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628