我正在尝试在运行bat文件时从命令行获取用户输入,但是当用户输入输入并按下return时它会崩溃:
echo Welcome to Xyplex Log On
pause
set /p whichXplex = Why Xyplex server - 1 or 2 ?
if %whichXyplex% == 1(
REM COnnecting to xyplex one IP:Socket
echo Connecting to Xpyplex %whichXyplex%...
start telnet.exe 192.120.187.35 2000
)ELSE(
echo Conencting to xyplex %whichXyplex%....
start telnet 193.120.187.245 2000
)
REM Run the script
cssript logInXyplex.vbs
我使用的是Windows 7。
任何意见都表示赞赏。
答案 0 :(得分:2)
乍一看:
rem y letter missing: variable name %whichXplex% versus %whichXyplex%
set /p whichXyplex= Why Xyplex server - 1 or 2 ?
rem harmful extra ^ space in front of =
rem would create %whichXplex % variable instead of %whichXyplex%
rem missing space v here
if "%whichXyplex%" == "1" (
rem ^ ^ ^ ^ missing double quotes
REM COnnecting to xyplex one IP:Socket
echo Connecting to Xpyplex %whichXyplex%...
start telnet.exe 192.120.187.35 2000
) ELSE (
rem ^ missing spaces in )ELSE(
echo Conencting to xyplex %whichXyplex%....
start telnet 193.120.187.245 2000
)
此外,我更喜欢
choice
command。 choice.exe
允许从键盘捕获单个按键(没有额外的 Enter ):(并注意在set "varname=varvalue"
语法模式中始终使用双引号)
choice
代码段:
choice /C:12 /M:"Which Xyplex server "
set "whichXyplex=%errorlevel%"
set /P
代码段:
set "whichXyplex=1"
set /p "whichXyplex= Which Xyplex server - 1 or 2 (default=%whichXyplex%)?"