问题
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.
回答1:
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)
来源:https://stackoverflow.com/questions/17078631/windows-shell-script-is-was-unexpected-at-this-time-in-command-prompt