我正在尝试将.sh脚本重写为.bat文件,以便我可以在Windows VM上对其进行测试。
我遇到了FOR / F循环的问题,我无法使用我在循环中设置的变量,我真的很沮丧。我一直在寻找一段时间,我已经启用了延迟扩展,没有运气。下面是一段代码:
setlocal ENABLEDELAYEDEXPANSION
FOR /F "eol=; tokens=1,2,3,4,5,6,7,8,9,10 delims=, " %%i in (%SOutPath%) do (
SET sKic1Length = 20
SET sRAMTar = %%i
SET admCode = %%j
SET pin1Code = %%k
SET pin2Code = %%l
SET sKic1 = %%m
SET sKic1Length = %%n
SET sKid1 = %%o
SET sKid1Length = %%p
SET sRAMMSL=%%r
IF sKic1Length EQU 16 SET sKic1Length=11
IF sKic1Length EQU 32 SET sKic1Length=15
IF sKic1Length EQU 48 SET sKic1Length=19
::This echo outputs 'Echo is off' so variable is empty
@echo %sKic1Length%
)
::Also returns same
ECHO %sKid1Length%
下面是我在循环中阅读的文件:
; ;, RAMTAR, ADM1, PIN1, PIN2, KIC1, KICLENGTH,KID1, KIDLENGTH, RAMMSL
000000, 17182320, 2387, 1116, 607038CA34A91FB1, 16, 607038CA34A91FB, 16, 0200
我很感激任何意见。
谢谢
答案 0 :(得分:1)
你必须使用!而不是循环中的ENABLEDELAYEDEXPANSION上的%
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "eol=; tokens=1,2,3,4,5,6,7,8,9,10 delims=, " %%i in (%SOutPath%) do (
set "sKic1Length=20"
set "sRAMTar=%%i"
set "admCode=%%j"
set "pin1Code=%%k"
set "pin2Code=%%l"
set "sKic1=%%m"
set "sKic1Length=%%n"
set "sKid1=%%o"
set "sKid1Length=%%p"
set "sRAMMSL=%%r"
if "!sKic1Length!" equ "16" set "sKic1Length=11"
if "!sKic1Length!" equ "32" set "sKic1Length=15"
if "!sKic1Length!" equ "48" set "sKic1Length=19"
echo.!sKic1Length!
)
echo.%sKid1Length%
endlocal
如果最后一个回声没有返回,那么我假设,在最后一次迭代中,%% p为空。