gpt4 book ai didi

windows - 2段相同代码中批处理脚本中延迟变量扩展的不同行为

转载 作者:可可西里 更新时间:2023-11-01 10:26:16 26 4
gpt4 key购买 nike

@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=2if "!VARY!"=="2" echo If you see this, yes echo !VARY!)

关于windows - 2段相同代码中批处理脚本中延迟变量扩展的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com