2015-11-03 75 views
0

我想运行一个批处理文件时,在命令行获取用户输入的,但是当用户进入输入,按下回车键时崩溃:用户输入崩溃

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

赞赏的任何输入。

回答

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允许单个键按压从键盘(没有额外输入)被捕获:
  • 或分配默认值,以防止在壳体空(未定义)变量值,如果用户按压仅输入

(并注意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%)?"