Windows Shell Script “is was unexpected at this time.” in Command Prompt

左心房为你撑大大i 提交于 2020-01-03 03:01:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!