batch “set /p” crashes when no input

后端 未结 2 870
礼貌的吻别
礼貌的吻别 2021-01-28 19:45

(I\'ll use this script as an example)

@echo off
:start
cls
set /p x=enter the password:
if %x%==1 goto correct
echo incorrect
timeout 1 >nul /nobreak
goto sta         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-28 20:19

    Change this:

    if %x%==1
    

    To this:

    if "%x%" EQU "1"
    

    The quotes encase the variable so you always have a valid string to compare, then you have to do the same to what you're comparing it to for consistency. EQU is another way to compare strings (check out if /? for other operators).

提交回复
热议问题