我正在使用全局变量来存储批处理“函数”的返回值。它以奇怪的方式变化:
result local = 1
result global = 1
result = 4
因此,函数调用后,结果以某种方式变回 4。这里有什么问题?
set result=4
if %build%==1 (
call :build_1
echo "result=%result%"
if %result%==4 (
exit /b 4
goto error
)
call :build_2
if %result%==4 (
exit /b 4
goto error
)
call :build_3
if %result%==4 (
exit /b 4
goto error
)
call :build_4
if %result%==4 (
exit /b 4
goto error
)
goto success
)
rem return error/success code in result variable
:build_1
setlocal
rem Stage 1
call :build_one_unit
if %errorlevel%==4 (
echo FAILED!
set result=4
exit /b
)
rem Stage 2
call :build_one_unit
if %errorlevel%==4 (
echo FAILED!
set result=4
exit /b
)
set result=1
echo "result local = %result%"
endlocal & set result=%result%
echo "result global = %result%"
goto:eof