批处理文件上的用户输入崩溃

时间:2015-11-03 11:53:17

标签: batch-file command-line

我正在尝试在运行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。

任何意见都表示赞赏。

1 个答案:

答案 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 commandchoice.exe允许从键盘捕获单个按键(没有额外的 Enter ):
  • 或指定默认值以防止空(未定义)变量值,以防用户仅按 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%)?"