[英]Command executed from else block in Windows batch script returning wrong errorlevel
I am new to batch scripting and while fiddling with the simple ones in my Windows 7 pc, I stuck on the following - 我是批处理脚本的新手,虽然在Windows 7 pc中摆弄简单的脚本,但仍坚持以下几点-
command1
echo %errorlevel%
if %errorlevel% neq 0 (
echo -- Error occured during command1 execution ---
goto :eof
) else (
echo -- command1 execution was successful ---
command2
echo %errorlevel%
if %errorlevel% neq 0 (
echo -- Error occured during command2 execution ---
goto :eof
)
)
Here, command1 excution is successful(checked separately) and it is returning errorlevel 0(success) while command2 is failing(checked separately) and instead of no zero (failure) it is returning errorlevel as 0. But when I remove else condition, command2 execution is returning 1 (failure). 在这里,命令1执行成功(单独检查),并且返回错误级别0(成功),而命令2执行失败(单独检查),而不是不为零(失败),返回错误级别为0。但是当我删除else条件时,命令2执行返回1(失败)。 Curious to know the reason.
好奇地知道原因。
As suggested, now I am declaring setlocal enabledelayedexpansion at the begining and instead of %errorlevel%, using !errorlevel! 如建议的那样,现在我开始使用!errorlevel声明setlocal enabledelayedexpansion而不是%errorlevel%! and it is giving desired errorlevel on success/failure.
并给出成功/失败所需的错误级别。
But now I am facing another problem. 但是现在我面临另一个问题。 I am calling another command (suppose command3 in place of command2).
我正在调用另一个命令(假设用command3代替command2)。 It inturn calls one of my java class which is throwing java.lang.StringIndexOutOfBoundsException.
它依次调用我的java类之一,该类抛出java.lang.StringIndexOutOfBoundsException。 But in my .bat file it is returning errorlevel as 0, instead of 1 whereas command3 got failed (checked seperately).
但是在我的.bat文件中,它返回的错误级别为0,而不是1,而command3失败了(单独检查)。 Below is my latest script -
以下是我最新的脚本-
@echo off
setlocal enabledelayedexpansion
command1
echo call !errorlevel!
if !errorlevel! neq 0 (
echo -- Error occured during command1 execution ---
goto :eof
)
echo -- command1 execution was successful ---
REM command3
java MyclassName > logfilename.log 2>&1
echo call !errorlevel!
if !errorlevel! neq 0 (
echo -- Error occured during command3 execution ---
goto :eof
)
echo --- command3 Executoin was successful---
How to force !errorlevel! 如何强制!错误级别! to return correct value when it is failed with some exception.
在出现某些异常失败时返回正确的值。 Please help.
请帮忙。
You need to invoke delayedexpansion [hundreds of SO articles about that - use the search feature] in order to display the run-time value of any variable that's changed within a parenthesised series of instructions (aka "code block"). 您需要调用delayedexpansion [关于它的数百篇SO文章-使用搜索功能],以显示括号内的一系列指令(也称为“代码块”)中已更改的任何变量的运行时值。
With your current code, if you want to display the actual error returned by command2
use 使用当前代码,如果要显示
command2
返回的实际错误,请使用
CALL echo %%errorlevel%%
and if you want to execute an if
on the run-time value of errorlevel
, then use 如果要在错误
errorlevel
的运行时值上执行if
,则使用
if errorlevel 1 (
IF ERRORLEVEL n
is TRUE if errorlevel
is n or greater than n . IF ERRORLEVEL n
如果errorlevel
为n 或大于 IF ERRORLEVEL n
则IF ERRORLEVEL n
为TRUE。 IF ERRORLEVEL 0
is therefore always true. IF ERRORLEVEL 0
因此始终为true。 IF NOT ERRORLEVEL 1
is a test for errorlevel=0. IF NOT ERRORLEVEL 1
是对errorlevel = 0的测试。
Try this amended version of the code, let's actually use the if
and else
试试这个修改后的代码版本,让我们实际使用
if
和else
@echo off
setlocal enabledelayedexpansion
command1 2>&1
echo !errorlevel!
if !errorlevel! neq 0 (
echo -- Error occured during command1 execution ---
) else (
echo -- command1 execution was successful ---
command3 2>&1
echo !errorlevel!
if !errorlevel! neq 0 (
echo -- Error occured during command3 execution ---
) else (
echo --- command3 Execution was successful---
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.