Variable doesn't get updated in the function of Batch file

后端 未结 2 883
臣服心动
臣服心动 2020-12-22 04:25

I have a simple batch script that will read the value in file version.property and perform a certain job my code is below

TITLE StartEODMaintenance
echo o         


        
2条回答
  •  别那么骄傲
    2020-12-22 04:32

    The big problem is that your script continues to :FindString as part of the main runtime. On line 6 you call :FindString "MAINALGO", then you've got an IF code block. After that code block, you should exit /b or goto :EOF or something to halt execution of the main runtime, but you don't. Instead, your script continues to :FindString a second unintended time, sets %ERRORLEVEL% by find /I /C "" version.property, and continues to the end.

    Something else you might find interesting is conditional execution. Instead of

    find /I /C "%~1" version.property
    if %errorlevel%==1 (
    echo "errorlevel 1"
    set "Build=0"
    )
    if %errorlevel%==0 (
    echo "errorlevel 0" 
    set "Build=%~1"
    echo build value in function %Build%
    )
    

    ... or ...

    find /I "%~1" version.property
    if errorlevel 1 (
        echo "errorlevel 1"
        set "Build=0"
    ) else (
        echo "errorlevel 0" 
        set "Build=%~1"
        echo build value in function %Build%
    )
    

    ... you could condense your code a bit like this:

    find /I "%~1" version.property && (
        echo "errorlevel 0" 
        set "Build=%~1"
        echo build value in function %Build%
    ) || (
        echo "errorlevel 1"
        set "Build=0"
    )
    

    I also recommend that you avoid being lazy with your indenting and formatting. While your code isn't nearly as bad as it could be, it would still be a little easier to see what's going on in :FindString if you indent your code blocks.

    Anyway, see whether you have better luck with this:

    TITLE StartEODMaintenance
    echo off
    cls
    set "Build=0"
    
    call :FindString "MAINALGO"
    
    IF /I "%Build%"=="MAINALGO" (
        echo "start job on MainAlgo"
    ) else (
        call :FindString "DRSITEALGO"
        echo build value %Build%
        IF /I "%Build%"=="DRSITEALGO" (
            echo "start job on secondAlgo"
        ) else (
            echo "sth wrong"
            rem // exit non-zero
            exit /b 1
        )
    )
    
    exit /b 0
    rem // end main runtime / begin script functions
    
    :FindString
    find /I "%~1" version.property && (
        echo "errorlevel 0" 
        set "Build=%~1"
        echo build value in function %~1
    ) || (
        echo "errorlevel 1"
        set "Build=0"
    )
    

提交回复
热议问题