Batch script error level is not getting reset [duplicate]

做~自己de王妃 提交于 2019-12-11 17:36:28

问题


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

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


回答1:


 if %errorlevel% NEQ 0 (

should be

 if errorlevel 1 (

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").

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).

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

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 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.



来源:https://stackoverflow.com/questions/56866189/batch-script-error-level-is-not-getting-reset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!