批处理脚本错误级别未重置 [英] Batch script error level is not getting reset

查看:75
本文介绍了批处理脚本错误级别未重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个小的BAT文件,它将在其中搜索"FAIL"关键字,然后搜索PASS-如果未找到,则将其视为错误:

I am writing a small BAT file where it will search for "FAIL" Keyword followed by PASS - if none is found then take it as an error:

echo
set "topLevel=%cd%"
If [%1]==[] exit /B 1
If [%2]==[] exit /B 1
If [%3]==[] exit /B 1
If [%4]==[] exit /B 1
findstr /? >NUL 2>&1 || exit /B 1
set "arg1=%1"
set "arg2=%2"
set "arg3=%3"
set "arg4=%4"

set /a errno=0
if not exist %arg3% exit /B 1
if not exist %arg2%\%arg1% exit /B 1
set "logfile=%arg1:.=_%"
copy /y/v %arg2%\%arg1% %arg3%\%arg4%.%logfile%.res || exit /B 1
findstr /I /C:"FAIL" /I /C:"UNKNOWN" %arg3%\%arg4%.%logfile%.res 
if %errorlevel% EQU 0 (
    set /a errno=2
) ELSE (
    REM MAKE SURE THAT THE SCRIPT DID NOT CRASH HENCE NEITHER PASS OR FAIL WILL BE LISTED
    findstr /I /C:"PASS" %arg3%\%arg4%.%logfile%.res  
    if %errorlevel% NEQ 0 (
    set /a errno=2
    )
)  
cd %topLevel%
exit /B %errno%

使用示例数据运行时,我得到以下输出:

When I run with sample data I get below output:

..............................................

    C:\agent\_work\30\s1>copy /y/v C:\output\test.log C:\agent\_work\30\s1\tttt.test_log.res   || exit /B 1
            1 file(s) copied.

    C:\agent\_work\30\s1>findstr /I /C:"FAIL" /I /C:"UNKNOWN" C:\agent\_work\30\s1\tttt.SystemWalk_log.res

    C:\agent\_work\30\s1>if 1 EQU 0 (set /a errno=2 )  ELSE (
    REM MAKE SURE THAT THE SCRIPT DID NOT CRASH HENCE NEITHER PASS OR FAIL WILL BE LISTED
     findstr /I /C:"PASS" C:\agent\_work\30\s1\tttt.test_log.res
     if 1 NEQ 0 (set /a errno=2 )
    )
    PASSED
    PASSED
    PASSED
    PASSED
    PASSED

    C:\agent\_work\30\s1>cd C:\agent\_work\30\s1

    C:\agent\_work\30\s1>exit /B 2

    C:\agent\_work\30\s1>echo %ERRORLEVEL%
    2

实际上是因为它找到了"PASS"字符串,而没有找到"FAIL"字符串-因此错误级别应该为0-我该如何解决此问题?

Actually cause it has found "PASS" string and no "FAIL" ones - so the error level should be 0 - how can I fix the issue?

推荐答案

 if %errorlevel% NEQ 0 (

应该是

 if errorlevel 1 (

标准delayedexpansion问题-您需要调用延迟扩展[有关此问题的数百篇SO文章-使用搜索功能],以便显示或使用括号内的一系列指令中已更改的任何变量的运行时值(亦称代码块").

Standard delayedexpansion issue - you need to invoke delayedexpansion [hundreds of SO articles about that - use the search feature] in order to display or use the run-time value of any variable that's changed within a parenthesised series of instructions (aka "code block").

在块语句(a parenthesised series of statements)中,解析整个块并然后执行.块中的任何%var%都将在解析该块时被该变量的值取代-在执行该块之前-相同的情况适用于FOR ... DO (block).

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

因此,IF (something) else (somethingelse)将在遇到IF时使用%variables%的值执行.

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

克服此问题的两种常见方法是:1)使用setlocal enabledelayedexpansion并使用!var!代替%var%来访问更改的var值,或2)调用子例程以使用更改值.

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.

IF ERRORLEVEL n为TRUE.因此,IF ERRORLEVEL 0始终为true. IF NOT ERRORLEVEL 1是对errorlevel = 0的测试. IF %ERRORLEVEL%==0也是如此,除了前者可以在一个块内使用而后者不能使用.

IF ERRORLEVEL n is TRUE if errorlevel is n or greater than n. IF ERRORLEVEL 0 is therefore always true. IF NOT ERRORLEVEL 1 is a test for errorlevel=0. So is IF %ERRORLEVEL%==0, except that the former can be used within a block but the latter cannot.

这篇关于批处理脚本错误级别未重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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