简体   繁体   English

是否有一种从嵌套批处理脚本中捕获错误代码(%errorlevel%)的简单方法?

[英]Is there an easy way to capture the error code (%errorlevel%) from a nested batch script?

I have a nested batch script and I want my error codes from it to percolate up to the main batch script that called it. 我有一个嵌套的批处理脚本,我希望我的错误代码渗透到调用它的主批处理脚本。 I have tried 我努力了

exit /b %errorlevel%

but the variable doesn't make it back. 但是这个变量并没有让它回归。 ECHO'ing %errorlevel% in the called batch script gives me 103, but ECHO'ing %errorlevel% in the main batch script (the very next line in terms of execution) gives me 0. This question has been asked on SO before, but none of the posts have worked for me. 被调用的批处理脚本中的ECHO'ing%errorlevel%给了我103,但主批处理脚本中的ECHO'ing%errorlevel%(执行方面的下一行)给了我0.这个问题之前已经被问到了,但这些帖子都没有对我有用。

EDIT: Due to poor writing, I have revised my question and also will add the code for you to see. 编辑:由于写作不好,我修改了我的问题,并将添加代码供您查看。

Here is the main batch file. 这是主批处理文件。 The if statement here is never hit unless I change the condition to something other than 0: 除非我将条件更改为0以外的值,否则此处的if语句永远不会被命中:

call BuildInstaller.cmd %SourceDir% %TargetDir% %ProductVersion% %%I
if %errorlevel% neq 0 (
    ECHO %date% %time%: Build of %%I failed, halting >> %LOGFILE%
    exit /b %errorlevel%
)

Here is where BuildInstaller.cmd exits. 这是BuildInstaller.cmd退出的地方。 I have cleaned up the prints to avoid confusion: 我已经清理了打印件以避免混淆:

if %errorlevel% neq 0 (
    exit /b %errorlevel%
)

As a side note, I also tried doing 作为旁注,我也尝试过

set returnvalue=12

in the called batch script, but ECHO'ing %returnvalue% in the main batch script was always one execution of the program behind. 在被调用的批处理脚本中,但主批处理脚本中的ECHO'ing%returnvalue%始终是后面程序的一次执行。 If anyone knows the answer to this sub-question, that would be cool to know. 如果有人知道这个子问题的答案,那就知道了。

You have the classical Delayed Expansion error. 您有经典的延迟扩展错误。 See this example: 看这个例子:

call BuildInstaller.cmd %SourceDir% %TargetDir% %ProductVersion% %%I
if %errorlevel% neq 0 (
    ECHO %date% %time%: Build of %%I failed, halting >> %LOGFILE%
    exit /b %errorlevel%
)

This is BuildInstaller.cmd: 这是BuildInstaller.cmd:

@echo off
setlocal EnableDelayedExpansion

if 1 == 1 (
   rem Inside a code block, %errorlevel% have the same value it had before the block
   rem (its %value% is NOT updated inside the block) so you need to use !errorlevel!
   rem in order to get the *updated* value

   verify set errorlevel = 1
   if !errorlevel! neq 0 (
      exit /b !errorlevel!
   )
)

exit /B

For further details, look for "Delayed Expansion". 有关详细信息,请查找“延迟扩展”。

The errorlevel is set after each command has been executed. 执行每个命令后设置errorlevel。 So the error level of 0 is the result of the statement after the one that created error level 103. 因此,错误级别0是创建错误级别103之后的语句的结果。

To illustrate with multiple batch files: 要说明多个批处理文件:

sub.bat: sub.bat:

 rem This is invalid argument for zip which sets errorlevel
 zip -QQ

test.bat: test.bat的:

 @echo off
 call sub.bat
 echo %errorlevel%

Example output which the first line comes from zip: 第一行来自zip的示例输出:

zip error: Invalid command arguments (no such option: Q)
16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM