@echo off
pushd
setlocal enabledelayedexpansion enableextensions
set VARY=before
if "!VARY!" == "before" (
set VARY=2
if "!VARY!" == "2" @echo If you see this, yes echo !VARY!
)
set VAR=before
if "!VAR!" == "before" (
set VAR=1
if "!VAR!" == "1" @echo If you see this, it worked
)
popd
预期输出:
如果你看到这个,那就是 2
如果你看到这个,那就说明它成功了
实际输出:
如果你看到这个,那就说明它成功了
有人可以解释为什么输出没有显示“如果你看到这个,是2”吗?
最佳答案
2
后面有尾随空格,因此请比较 if "2 "== "2"
(不等于)。
为避免这种情况,请使用以下代码:
set "VARY=before" if "!VARY!" == "before" ( set "VARY=2" if "!VARY!"=="2" echo If you see this, yes echo !VARY! )
..如果你设置数字,你也可以使用“set/a”:
set "VARY=before" if "!VARY!" == "before" ( set /a VARY=2 if "!VARY!"=="2" echo If you see this, yes echo !VARY! )
关于windows - 批处理脚本中两段相同代码中延迟变量扩展的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762772/