Windows findstr/find 命令在 运行 批处理文件内部和外部时关于 ERRORLEVEL 的行为不同

Windows findstr/find commands behave different regarding ERRORLEVEL when run inside and outside a batch file

所以我使用批处理文件在 windows 下生成手册。手册的处理工具有一个有缺陷的错误报告。我不能只使用它的错误代码。我还必须检查日志是否有错误。

要执行此错误检查,我想在批处理文件中使用以下代码片段:

findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
if %ERRORLEVEL% NEQ 1 (
    rem do error handling stuff
)

当在 cmd.exe-window 中键入 findstr/find 控制台命令时,它会按预期运行并设置预期的 ERRORLEVEL。如果找到某些内容,它将 ERRORLEVEL 设置为 0。如果未找到任何内容,它将 ERRORLEVEL 设置为 1。

我的问题:findstr/find 在批处理文件中的行为不同。即使未找到任何内容,它也会将 ERRORLEVEL 设置为 0。批处理文件是由 jenkins 启动还是我在 cmd.exe-window.

中启动批处理文件都没有关系

我已经试过了:

这是我使用的完整批处理文件,如果 soemone 在文件的其他地方发现了我的问题的原因。谢谢你的时间。

set BEGIN_TIME=%TIME%

if bServerAdmin NEQ %USERNAME% (
    echo This file is designed to be run on the build server.
    echo Are you sure you want to run it?
    echo Press CTRL+C to abort
    echo Press Return to continue
    pause
)

rem ========================================
rem Set some variable that influence the run
rem ========================================
set HELP_AND_MANUAL="C:\Program Files\HelpAndManual\helpman.exe"
rem 7zip executable to use, http://www.7-zip.org/
set SEVEN_ZIP="C:\Program Files-Zipz.exe"

rem ===================================================
rem Make sure working directory exists and switch to it
rem ===================================================
if not exist output mkdir output
call :pushdWithErrCheck output

rem ===============
rem Clear old stuff
rem ===============
del /Q /S /F *

rem ======
rem German
rem ======
call :helpAndManualWithErrCheck XXXXXXX\XX\XXXXXXX.hmxp XXXXXXX.chm   xxxxx.hmskin
call :helpAndManualWithErrCheck AAAA\AA\AAAA.hmxp       AAAA\AAAA.chm xxxxx.hmskin
call :helpAndManualWithErrCheck BBBB\BB\BBBB.hmxp       BBBB\BBBB.chm xxxxx.hmskin
call :helpAndManualWithErrCheck CCCC\CC\CCCC.hmxp       CCCC\CCCC.chm xxxxx.hmskin

rem ======================
rem Pack all build results
rem ======================
%SEVEN_ZIP% a -bd ..\Manuale.7z
IF %ERRORLEVEL% NEQ 0 exit 1

popd

exit 0

:helpAndManualWithErrCheck 
IF EXIST %WORKSPACE%\%1 (
    echo building %1 now
    if exist %WORKSPACE%\output\%2 (
        echo Error: output file exists before build was started
        Exit 1
    )
    %HELP_AND_MANUAL% %WORKSPACE%\%1 /CHM=%WORKSPACE%\output\%2 /L="%WORKSPACE%\%2.log" /O=%WORKSPACE%\_Design\HTML-Skin\%3
    IF %ERRORLEVEL% NEQ 0 (
        IF %ERRORLEVEL% NEQ 2 (
            rem ERRORLEVEL 2 is not an error either http://www.helpandmanual.com/help/index.html?hm_advanced_commandline_exitcodes.htm
            echo Error: exiting due to bad exit code now
            Exit 1
        )
    )
    if not exist %WORKSPACE%\output\%2 (
        echo Error: exiting due to missing file now
        Exit 1
    )
    rem debugging stuff echo findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
    rem debugging stuff findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
    rem debugging stuff echo %ERRORLEVEL%
    findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
    if %ERRORLEVEL% NEQ 1 (
        echo Error: exiting due to missing file according to HelpAndManual Log
        Exit 1
    )
) ELSE (
    echo Skipping %1 as the source file does not exist
)
goto :EOF

:pushdWithErrCheck 
pushd %1
if %ERRORLEVEL% NEQ 0 Exit 1
goto :EOF

你的主要问题叫做normal vs delayed variable expansion

为了errorlvel测试更好用

findstr /L /I /C:" not found in " "%WORKSPACE%\%2.log"
if errorlevel 1 (
    rem do error handling stuff
) 

请注意,对于任何大于或等于 n

errorlevel 值,表达式 if errorlevel n 的计算结果为真