在 Windows 批处理文件中检查非零(错误)返回码的万无一失的方法 [英] Foolproof way to check for nonzero (error) return code in windows batch file

查看:23
本文介绍了在 Windows 批处理文件中检查非零(错误)返回码的万无一失的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于处理批处理文件中的返回代码有很多建议(使用 ERROLEVEL 机制),例如

There's a lot of advice out there for dealing with return codes in batch files (using the ERROLEVEL mechanism), e.g.

一些建议是 if errorlevel 1 goto somethingbad,而另一些建议使用%ERRORLEVEL% 变量并使用 ==EQULSS 等.code>IF 语句等,因此鼓励延迟扩展,但它似乎有其自身的怪癖.

Some of the advice is to do if errorlevel 1 goto somethingbad, while others recommend using the %ERRORLEVEL% variable and using ==, EQU, LSS, etc. There seem to be issues within IF statements and such, so then delayedexpansion is encouraged, but it seems to come with quirks of its own.

知道是否返回错误(非零)代码的万无一失(即健壮,因此几乎可以在几乎所有具有任何返回代码的系统上运行)方法是什么?

What is a foolproof (i.e. robust, so it will work on nearly any system with nearly any return code) way to know if a bad (nonzero) code has been returned?

对于基本用法,以下似乎可以捕获任何非零返回码:

For basic usage, the following seems to work ok to catch any nonzero return code:

if not errorlevel 0 (
    echo error level was nonzero
)

推荐答案

抱歉,您的尝试还没有结束.if not errorlevel 0 仅当 errorlevel 为负时才为真.

Sorry, your attempt is not even close. if not errorlevel 0 is only true if errorlevel is negative.

如果你知道 errorlevel 永远不会是负数,那么

If you know that errorlevel will never be negative, then

if errorlevel 1 (echo error level is greater than 0)

如果您必须允许负错误级别,并且不在括号内的代码块内,则

If you must allow for negative errorlevel, and are not within a parenthesized block of code, then

set "errorlevel=1"
set "errorlevel="
if %errorlevel% neq 0 (echo error level is non-zero)

注意 - 在阅读 Joey 对问题中链接答案的评论后,我编辑了我的答案以明确清除任何用户定义的错误级别值.用户定义的错误级别可以屏蔽我们尝试访问的动态值.但这仅适用于您的脚本具有 .bat 扩展名的情况.如果您设置或清除变量,带有 .cmd 扩展名的脚本会将您的 ERRORLEVEL 设置为 0!更糟糕的是,如果您尝试取消定义一个不存在的变量,XP 会将 ERRORLEVEL 设置为 1.这就是为什么我在尝试清除它之前首先明确定义一个 ERRORLEVEL 变量!

Note - I edited my answer to explicitly clear any user defined errorlevel value after reading Joey's comment to the linked answer in the question. A user defined errorlevel can mask the dynamic value that we are trying to access. But this only works if your script has a .bat extension. Scripts with .cmd extension will set your ERRORLEVEL to 0 if you set or clear a variable! To make matters worse, XP will set ERRORLEVEL to 1 if you attempt to undefine a variable that does not exist. That is why I first explicitly define an ERRORLEVEL variable before I attempt to clear it!

如果您在括号内的代码块内,那么您必须使用延迟扩展来获取当前值

If you are within a parenthesized block of code then you must use delayed expansion to get the current value

setlocal enableDelayedExpansion
(
  SomeCommandThatMightGenerateAnError
  set "errorlevel=1"
  set "errorlevel="
  if !errorlevel! neq 0 (echo error level is non-zero)
)

但有时您不希望启用延迟扩展.如果您想在执行命令后立即检查错误级别,一切都不会丢失.

But sometimes you don't want delayed expansion enabled. All is not lost if you want to check the error level immediately after executing a command.

(
  SomeCommandThatMightGenerateAnError && (echo Success, no error) || (echo There was an error)
)

如果您绝对必须检查动态 ERRORLEVEL 值而不使用带括号的块内的延迟扩展,则以下方法有效.但是它有两个地方的错误处理代码.

If you absolutely must check the dynamic ERRORLEVEL value without using delayed expansion within a parenthesized block, then the following works. But it has the error handling code in two places.

(
  SomeCommandThatMightGenerateAnError
  if errorlevel 1 (echo errorlevel is non-zero) else if not errorlevel 0 (echo errorlevel is non-zero)
)


这终于是对非零错误级别的最终"测试,它应该在任何情况下都可以工作:-)

(
  SomeCommandThatMightGenerateAnError
  set foundErr=1
  if errorlevel 0 if not errorlevel 1 set "foundErr="
  if defined foundErr echo errorlevel is non-zero
)

为了方便使用,它甚至可以转换成宏:

It can even be converted into a macro for ease of use:

set "ifErr=set foundErr=1&(if errorlevel 0 if not errorlevel 1 set foundErr=)&if defined foundErr"
(
  SomeCommandThatMightGenerateAnError
  %ifErr% echo errorlevel is non-zero
)

宏支持括号和 ELSE 就好了:

The macro supports parentheses and ELSE just fine:

%ifErr% (
  echo errorlevel is non-zero
) else (
  echo errorlevel is zero
)


最后一期:

输入和/或输出的重定向可能由于多种原因而失败.但是重定向错误不会设置错误级别,除非使用|| 运算符.有关详细信息,请参阅 Windows 中的文件重定向和 %errorlevel%.因此,人们可以争辩说,不存在通过错误级别检查错误的万无一失的方法.最可靠的方法(但仍然不是绝对可靠的)是 || 运算符.

Redirection of input and/or output can fail for any number of reasons. But redirection errors do not set the errorlevel unless the || operator is used. See File redirection in Windows and %errorlevel% for more information. So one can argue that there does not exist a fool-proof way to check for errors via errorlevel. The most reliable method (but still not infallible) is the || operator.

这篇关于在 Windows 批处理文件中检查非零(错误)返回码的万无一失的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆