2015-11-03 82 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%)?"