当我输入带空格的输入时,批处理 cmd 退出 [英] batch cmd exits as I enter the input with space
问题描述
我正在为一个用户界面编写一个批处理脚本,您可以在其中输入数字 1 - 10.它非常像这个例子:
<前>@回声关闭:问类回声按 1 为 test1回声按 2 为 test2设置/p 输入 =如果 %input% == 1 转到 test1如果 %input% == 2 转到 test2如果 %input% GTR 10 goto ask去问:test1关掉:test2网络视图暂停去问我有 if %input% GTR 10 goto ask,最后 goto ask in global 因为如果有人输入不同的东西,它会回到问题.为什么当我输入不同的东西时它会导致我退出终端?
如果你输入一个带空格的字符串,if
语法会给你一个语法错误.我们来看看:
if hello world == string echo xyz
if
语法为:if
所以 hello
是 value1,world
是比较器 - 等等 - 什么?world
不是比较器 - 语法错误.
将您的价值观括在引号中以确保安全:
if "hello world" == "string" echo xyz
所以 "hello world"
是 value1,==
是比较器,"string"
是 value2 和 echo xyz
是命令.一切顺利.
您可能对 choice 命令感兴趣,它自己进行错误处理并允许只有有效的密钥.
I am writing a batch script for an user interface where you can enter the digits 1 - 10. its pretty much like this example:
@echo OFF :ask cls echo press 1 for test1 echo press 2 for test2 set /p input= if %input% == 1 goto test1 if %input% == 2 goto test2 if %input% GTR 10 goto ask goto ask :test1 shutdown :test2 net view pause goto ask
i have if %input% GTR 10 goto ask, and in the end goto ask in global because if someone types something different it will go back to the question. Why does it crash me out of the terminal when I type something different?
if you enter a string with space(s), if
syntax will give you a syntax error. Let's look at:
if hello world == string echo xyz
if
syntax is: if <value1> <comparator> <value2> command
So hello
is value1, world
is the comparator - wait - what? world
isn't a comparator - Syntax error.
Enclose your values in quoutes to be safe:
if "hello world" == "string" echo xyz
So "hello world"
is value1, ==
is the comparator, "string"
is value2 and echo xyz
is the command. All goes well.
You may be interested in the choice command, which does it's own error handling and allows only valid keys.
这篇关于当我输入带空格的输入时,批处理 cmd 退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!