我有一个如下所示的Windows批处理文件:
choice /C:123 /n /m "Generate output? (1. Debug only 2. Production only 3. Both debug and production)"
if errorlevel==1 (
echo 1
)
if errorlevel==2 (
echo 2
)
if errorlevel==3 (
echo 3
)
问题是当用户选择3
时,它会回显出1,2和3.它应该只回显3
。我该如何解决?
答案 0 :(得分:3)
解决方案在SomethingDark的评论中。
您的脚本失败的原因是因为=
是批处理文件中的标准分隔符(例如<space><tab>;,
),并且您的条件采用其他形式的IF
命令与比较不同:
IF ERRORLEVEL N execute command
这意味着 - if errorlevel等于或大于N
执行命令。So that's why with 3 all conditions are performed。
要获得真正的平等检查,您需要:
choice /C:123 /n /m "Generate output? (1. Debug only 2. Production only 3. Both debug and production)"
if %errorlevel% EQU 1 (
echo 1
)
if %errorlevel% EQU 2 (
echo 2
)
if %errorlevel% EQU 3 (
echo 3
)
请注意==
总是强制进行字符串比较,EQU, NEQ, LSS, LEQ, GTR, GEQ
是数字比较的正确开关(执行相等检查时不是那么重要)