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

后端 未结 2 863
臣服心动
臣服心动 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"
    )
    
    0 讨论(0)
  • 2020-12-22 04:50

    The %variables% are expanded when the command is parsed and the command if followed by a block is parsed at once. So when you set a variable in the block, its following usage (in echo here) has been expanded already and it results in showing the value that was valid before entering the block!

    You need delayed expansion to read variable values at the moment when they are used, not parsed. First, issue the command:

    setlocal EnableDelayedExpansion
    

    Then refer to the variables modified earlier within the same block as !variable!.

    if %errorlevel%==0 (
    echo "errorlevel 0" 
    set "Build=%~1"
    echo build value in function !Build!
    )
    
    0 讨论(0)
提交回复
热议问题