我使用与Windows Shell Script “is was unexpected at this time.” in Command Prompt相同的Shell脚本。这里的解决方案适用于Jerry(请求者)并帮助了我,直到我测试了IF NOT
条件。以下是有问题的代码:
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)
在Jerry的例子中,Blorgbeard提供了%reply%
应该被引号包围的解决方案,以便它可以测试:
IF NOT "Who is there?" == "Who is there?"
而不是:
IF NOT Who is there? == "Who is there?"
这导致杰瑞和我都得到错误:is was unexpected at this time
。
使用Blorgbeard的解决方案后,错误消失了,但当我尝试键入Who is there?
而不是"Who is there?"
来检查验证时,代码会跳过验证ECHO "Sorry, but you are not playing the game right!"
我错过了什么?有什么建议吗?
答案 0 :(得分:0)
根据Compo的提示,只检查回复是否包含who
和there
个案独立的字词:
SET /p reply="Knock knock! C:>"
CLS
If not defined reply goto :wrong
echo %reply%|find /i "who" 2>&1>Nul && echo %reply%|find /i "there" 2>&1>Nul && Echo goto :right
:wrong
ECHO "Sorry, but you are not playing the game right!"
GOTO :EOF
:right
答案 1 :(得分:0)
IF NOT "%reply%" == "Who is there?" (
使用原始代码为我工作正常。
合并其他有效回复的另一种方法可能是:
for %%a in ("Who is there"
"Who is it"
"Whos there"
"Who's there") do (
if /i "%%~a"=="%reply%" goto valid
if /i "%%~a"=="%reply%?" goto valid
)
ECHO "Sorry, but you are not playing the game right!"
GOTO :EOF
:valid
请注意,有效的项目列表可以跨行间隔,如图所示,但每个项目必须引用,因为它包含空格。
括号的放置至关重要。
"%%~a"
项表示取%%a
的值,删除任何封闭的引号,然后重新应用引号。通过这种方式,也可以在列表中接受和指定Qui
之类的响应,但由于它不包含空格,因此项Qui
可以可选地引用。< / p>