Search code examples
perlbatch-filereturnerrorlevel

batch file call errorlevel twice


I am struggling at a batch file errorlevel return issue, and could not find the answer.
Could any one give me any hints?

I have an external binary file, external.exe which read the return code (0-success, other-fail) of a batch file to decide what to do.

Within the batch file, mybatch.cmd, I wanted to run a script (perl) and return the result to the batch file.

For example, mybatch.cmd:

@echo off
myperl.pl
exit %errorlevel%

While myperl.pl is:

#!perl.exe
exit 0;

So I wanted myperl.pl to return value 0 to the batch file, and then the batch file return 0 to the external.exe file.

But external.exe didn't work as expected.

1) If I run mybatch.cmd from CLI, and then "echo %errorlevel%" from CLI, I could see it is 0.

2) If I add a line to display the return code from myperl.pl, I could see the return code from myperl.pl is exactly 0.

For example:

@echo off
myperl.pl
echo %errorlevel%
REM exit %errorlevel%

It will display 0, so the perl script returns the value exactly.

3) If I add a line to manually set the return code to 0, the external.exe works fine.

@echo off
myperl.pl
result=0
exit %result%

That is really weird. Any ideas? I could not control the external.exe, so I could not do debug on it.

In this example, the %errorlevel% is called twice:
1) Return from myperl.pl to batch.
2) Return from mybatch.cmd to external.exe. Will that be an issue?


Solution

  • Found out the root cause finally.

    myperl.pl should be with fullpath, otherwise the external.exe didn't know where the myperl.pl was.