当我输入带空格的输入时,批处理cmd退出 [英] batch cmd exits as I enter the input with space

查看:163
本文介绍了当我输入带空格的输入时,批处理cmd退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为用户界面编写一个批处理脚本,您可以在其中输入1到10的数字.它非常类似于此示例:

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

如果%input%GTR 10是goto询问的,最后我会在global中进行goto询问,因为如果有人键入其他内容,它将回到问题所在. 为什么当我键入其他内容时,它会使我崩溃,退出终端机?

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语法将为您提供语法错误.让我们看一下:

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语法为:if <value1> <comparator> <value2> command
所以hello是value1,world是比较器-等待-什么? world不是比较器-语法错误.

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

所以"hello world"是value1,==是比较器,"string"是value2,并且echo xyz是命令.一切顺利.

So "hello world" is value1, == is the comparator, "string" is value2 and echo xyz is the command. All goes well.

您可能对 choice 命令感兴趣,该命令本身是错误处理并允许唯一有效的密钥.

You may be interested in the choice command, which does it's own error handling and allows only valid keys.

这篇关于当我输入带空格的输入时,批处理cmd退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆