Windows Shell 满足 "IF NOT %reply% ==" 条件时脚本无 ECHO
Windows Shell Script no ECHO when "IF NOT %reply% ==" condition is met
我正在使用与 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?"
这导致 Jerry 和我都收到错误:is was unexpected at this time
。
使用 Blorgbeard 的解决方案后,错误消失了,但是当我尝试输入 Who is there?
而不是 "Who is there?"
来检查验证时,代码跳过了验证 ECHO "Sorry, but you are not playing the game right!"
我错过了什么?有什么建议吗?
根据 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
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
可以 可选 引用。
我正在使用与 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?"
这导致 Jerry 和我都收到错误:is was unexpected at this time
。
使用 Blorgbeard 的解决方案后,错误消失了,但是当我尝试输入 Who is there?
而不是 "Who is there?"
来检查验证时,代码跳过了验证 ECHO "Sorry, but you are not playing the game right!"
我错过了什么?有什么建议吗?
根据 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
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
可以 可选 引用。