分批QUOT;设置/ P"当崩溃没有输入 [英] batch "set /p" crashes when no input

查看:334
本文介绍了分批QUOT;设置/ P"当崩溃没有输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我将使用这个脚本作为一个例子)

(I'll use this script as an example)

@echo off
:start
cls
set /p x=enter the password:
if %x%==1 goto correct
echo incorrect
timeout 1 >nul /nobreak
goto start
:correct
echo correct
timeout 2 >nul /nobreak
goto start

如果我只是preSS进入,没有任何输入,它告诉我正确的。为什么?我怎么能prevent呢?是我的语法不正确的?

If I just press enter, without any input, it tells me correct. Why? how can I prevent this? Is my syntax incorrect?

另外,如果我preSS空间,然后回车,该批处理文件退出。
它告诉我在那一刹那屏幕错误(类似):

Also, If I press space, then enter, the batch file quits. It tells me in that split second screen the error (something like):

Goto was unexpected at this time

这使我相信,我没有给一个实例/定义[空格]键,但我切出的空间中的第一个实例。这是不是意味着一切,不符合特定条件的

Which leads me to believe that I didn't give an instance/define the [space] key, but I cut out the spaces in the first instance. Wouldn't this mean that everything that didn't match the specific criteria

if %x%==1 goto correct

只会继续在脚本中,因为它的价值未达到给定的实例描述的吗?

would just continue on in the script, since it's value didn't meet the one described in the given instance?

推荐答案

共同的问题 - 大多数但不是所有这些由

Common problem - most, but not all of which is solved by

if "%x%" EQU "1"...

如果你正在进入一个设置/ P 字符串,那么就没有说,输入数据不包含<大骨节病>空格。克服的办法就是括起来的比较操作符两边的字符串中引号 - 也就是说,双引号'不单引号'

If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes'

另一个问题是更加微妙。 pressing只是<大骨节病>输入离开​​ X 不变 - 它的不可以设置,它为空字符串。因此,如果你开始你的程序和preSS <大骨节病>输入那么的内容X 将确实是什么 - 和指令,如已经提到的,将被解析为如果== 1转到正确,因此语法错误 - 和将发生的空格条目相同的语法错误。

The other problem is more subtle. Pressing just Enter leave x unchanged - it does not "set" it to an empty string. Hence, if you start your procedure and press Enter then the "contents" of x will be indeed be nothing - and the instruction, as has been mentioned, will be resolved to if ==1 goto correct, hence the syntax error - and that same syntax error would occur for an entry of spaces.

假设你不是输入的东西 - 比如 2 X 现在设置为 2 ,所以如果语句很高兴,并决定不采取分支,你会得到执行下一条指令,并返回另一个输入。

Suppose you instead enter something - say 2. x is now set to 2, so the if statement is happy and decides to not take the branch, you get the next instruction executed, and return for another input.

假设你现在输入 1 如果仍是幸福的,作为分公司,显示正确的,并等待另一个输入。

Suppose you now enter 1. if is still happy, takes the branch, shows "correct" and waits for another input.

如果您然后只需preSS <大骨节病>输入 X 将保持不变,因此正确分公司将采取

If you then simply press Enter x will remain unchanged, so the correct branch will be taken.

那么,什么是真正发生的是, X <大骨节病>输入重复上一次输入后。

So what is actually happening is that x after Enter repeats the last input.

要克服这个问题,你需要执行

To get over this problem, you need to execute

set "x="
set /p x=enter the password:

这将清除 X 第一。在设置VAR =值语法确保了在该批次线上的任何尾随空格不包括在分配给 VAR

which clears x first. The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var.

您可以使用此特性指定一个默认值,

You can use this characteristic to assign a default value,

set "x=hello"
set /p x=enter the password:

将设置 X 你好如果用户只需回复<大骨节病>输入您提示

would set x to hello if the user simply replies Enter to your prompt.

这篇关于分批QUOT;设置/ P&QUOT;当崩溃没有输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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