在Windows批处理脚本中执行Perl脚本并捕获其退出值的语法是什么?

时间:2012-07-03 21:31:14

标签: perl batch-file errorlevel

  

可能重复:
  How do I get the application exit code from a Windows command line?

我在批处理中调用的Perl脚本返回123。使用参数829调用此perl sript并捕获脚本的退出代码的语法是什么?

 Perl.exe listMembership.pl 829 in cmd.exe

 @echo off
 set retVal=Perl.exe listMembership.pl 829
 echo %retVal%

1 个答案:

答案 0 :(得分:2)

在命令行上查看if /?的输出。如果没有cmd扩展名,最小公分母批处理脚本将是:

@echo off
Perl.exe listMembership.pl 829
if errorlevel 4 goto error
if errorlevel 3 goto exit3
if errorlevel 2 goto exit2
if errorlevel 1 goto exit1
if errorlevel 0 goto exit0

:error
echo:Unexpected exit code %ERRORLEVEL%
goto end

:exit3
echo:Forbnicate
goto end

:exit2
echo:Colormaticate
goto end

:exit1
echo:Motorcade
goto end

:exit0
echo:Is this really success?
goto end

:end
echo:Done

请注意,errorlevel检查必须按降序排列,因为:

ERRORLEVEL number Specifies a true condition if the last program run
                returned an exit code equal to or greater than the number
                specified.