문제

Very new to this, so I apologize if this is something simple. I am running the following .bat script in Command Prompt for an assignment.

@ECHO off

TITLE "KnockKnock.bat - The KnockKnock joke game!"

COLOR 0E

CLS

ECHO.
ECHO.   
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.

SET /p reply="Knock Knock!    C:>"

CLS

IF NOT %reply% == "Who is there?" (
  ECHO "Sorry, but you are not playing the game right!"
  GOTO :EOF)

ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.

SET /p reply="Orange!         C:>"

CLS

IF NOT %reply% == "Orange who?" (
  ECHO "Sorry, but you are not playing the game right!"
  GOTO :EOF)

ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.

ECHO "Orange you glad you've written your first Windows shell script?"

ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.

The script will prompt "Knock Knock!" as it should, but upon responding "Who is there?" (w/o quotations), I am presented with the error "is was unexpected at this time". What am I doing wrong?

Again, I realize this is probably very elementary, so I appreciate any help.

Thanks.

도움이 되었습니까?

해결책

The problem is that when the %reply% variable is substituted with its value, cmd tries to interpret this:

IF NOT Who is there? == "Who is there?" (

Rather than this:

IF NOT "Who is there?" == "Who is there?" (

To fix it, add quotes around %reply%, like this:

IF NOT "%reply%" == "Who is there?" (
  ECHO "Sorry, but you are not playing the game right!"
  GOTO :EOF)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top