1

I have some perl scripts which are invoked by a windows batch. The perl scripts provide returncodes which i need to output, but i alwas get 0. Even with a absolute reduced perl script:

use Modern::Perl '2015';

exit 1;

I get 0 als ERRORLEVEL. Here's the part of my windows batch file, where i invoke the perl script:

cmd /c "C:\Strawberry32\portableshell.bat C:\Users\abc\error.pl"
echo ERRORLEVEL Error Script: %ERRORLEVEL%

The output is "ERRORLEVEL Error Script: 0"

I already tried "start /wait ..." insted of cmd, but here i see a second console window, which i have to close manualy. That's not what i want.

If i enter "C:\Strawberry32\portableshell.bat C:\Users\abc\error.pl" manually in a konsole window, i see/get the expected Errorlevel 1, what's wrong here?

roli
  • 89
  • 8

1 Answers1

0

Several points here. First of all, you have not shown us the code where you "invoke the perl script", but the code where you invoke portableshell.bat file, and you did not show us the contents of such Batch file. So the first and simplest way to solve your problem is:

1- Insert in your windows Batch file the same command you use to "invoke the perl script" in the C:\Strawberry32\portableshell.bat file. This will give access to the errorlevel from the perl script for sure.

I assume that the contents of portableshell.bat file are basically two lines: the one that run the perl script followed by this line:

exit /B %errorlevel%

The purpose of previous line is to return the errorlevel from the perl script to the Batch file that called this one Batch file. This mechanism works perfectly as long as this Batch file be called via call command:

2- Invoke the C:\Strawberry32\portableshell.bat file via a call command:

call C:\Strawberry32\portableshell.bat C:\Users\abc\error.pl

If the portableshell.bat file is invoked via cmd /c instead of call, then you must set the returning errorlevel of cmd.exe program via a plain exit command, with NO /B switch:

3- Invoke the C:\Strawberry32\portableshell.bat file via cmd /c line, but terminate it with this command:

exit %errorlevel%

You may read a further explanation of the errorlevel stuff and the differences of exit /B number vs exit number at What are the ERRORLEVEL values set by internal cmd.exe commands?. At that site you may read that a start /B /WAIT portableshell.bat command may also be used to solve this problem, but this method also requires to end the .bat file with exit %errorlevel% in the same way as the cmd /c method.

Community
  • 1
  • 1
Aacini
  • 59,374
  • 12
  • 63
  • 94