为什么在使用IF命令进行字符串比较时,由于语法错误而退出我的批处理文件处理? [英] Why is processing of my batch file exited with a syntax error on string comparsion with IF command?

查看:56
本文介绍了为什么在使用IF命令进行字符串比较时,由于语法错误而退出我的批处理文件处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时,批处理将退出,并显示语法错误消息输出,提示它不能识别左括号.

When I run the following code, the batch processing is exited with a syntax error message output that it does not recognize the opening parenthesis.

if %errorlevel2%==s (
    echo.
    echo.
    echo Press any key to continue...
    echo.
    echo.
    pause>nul
    start haxmenu\pin.bat
    goto menu
)

我的括号设置不正确吗?或者是代码块中的代码使脚本执行失败?

Are my parentheses set wrong or is something about code inside the block which lets the script fail on execution?

错误消息是:

(这是意外的.

( was unexpected at this time.

推荐答案

简介

所以您有一个与此代码相似的代码:

Introduction

So you have a code similar to this one:

@echo off
:menu
set /P "errorlevel2=Please enter your choice: "
if %errorlevel2%==s (
    echo/
    echo/
    echo Press any key to continue...
    echo/
    echo/
    pause>nul
    start haxmenu\pin.bat
    goto menu
)

首先,您必须考虑到Windows命令处理器在为命令解析该行之前用此变量的字符串替换%errorlevel2%.

First you have to take into consideration that Windows command processor replaces %errorlevel2% with the string of this variable before the line is parsed for a command.

因此,取决于批处理文件执行时具有 IF 条件的行的外观取决于环境变量errorlevel2的存在和值.

So it depends on existence and value of environment variable errorlevel2 how the line with the IF condition looks like on execution of the batch file.

例如,根本没有定义errorlevel2且用户在提示时只是按下键 RETURN ENTER ,在用户提示后变量errorlevel2仍然不存在因此, IF 条件行如下所示:

For example with errorlevel2 not defined at all and user on prompt just hits the key RETURN or ENTER the variable errorlevel2 is still not existing after user prompt and the IF condition line looks therefore as follows:

if ==s (

IF 条件当然是无效的,并且由于语法错误,命令处理器退出了批处理.

This IF condition is of course invalid and command processor exits batch processing because of the syntax error.

在两个字符串之间使用双引号进行比较是避免语法错误的第一步.

Using double quotes around both strings to compare is a first step to avoid the syntax error.

if "%errorlevel2%"=="s" (

现在执行批处理文件时,用户未输入任何内容的结果行是:

Now the resulting line on execution of the batch file with nothing entered by the user is:

if ""=="s" (

这是有效的语法.

但是由于语法错误,这不能防止您的批处理代码完全退出,因为可以在提示符下输入,例如单引号将其看到.这将导致

But it does not prevent your batch code completely on exiting because of a syntax error as it can be seen by entering on prompt for example a single double quote. This would result in

if """=="s" (

由于等号运算符左侧的双引号奇数引起的语法错误,再次导致批处理退出.

which results again in an exit of batch processing because of a syntax error caused by the odd number of double quotes on left side of the equal operator.

在考虑此附加问题的解决方案之前,我们还应该考虑当用户实际上只是在提示符下按 RETURN ENTER 而不输入任何内容时会发生什么情况.运行以下代码:

Before thinking about a solution for this additional issue, we should also think about what happens when the user really just press RETURN or ENTER on prompt without entering anything. Run the following code:

@echo off
set /P "UserInput=Enter a number: "
echo UserInput=%UserInput%
set /P "UserInput=Hit key RETURN: "
echo UserInput=%UserInput%
pause

在第一个提示符处输入一个类似23的数字并按要求在第二个提示符处击中一个 RETURN 之后,也会再次输出数字23.

After entering on first prompt a number like 23 and hit on second prompt just RETURN as requested, the number 23 is output also a second time.

如果用户只是在用户提示符下单击 RETURN ,则不会修改环境变量的当前值.在菜单中使用set /P时,应始终考虑到这一点,尤其是在此菜单处于循环内的情况下,因为在这种情况下,变量在用户首先选择后已经具有一个值,如果出现以下情况,该值将保持不变.用户什么都不输入.

The current value of the environment variable is not modified if the user just hits RETURN on user prompt. This should be always taken into account when using set /P in a menu, especially if this menu is within a loop because in this case the variable has already a value after first choice made by the user which would be kept unmodified on next prompt if the user does not enter anything.

一种解决方案是使用单引号将变量预定义,并从输入字符串中删除所有双引号.

One solution is predefining the variable with a single double quote and remove all double quotes from input string.

@echo off
:menu
set "errorlevel2=""
set /P "errorlevel2=Please enter your choice: "
set "errorlevel2=%errorlevel2:"=%"
if /I "%errorlevel2%"=="s" (
    echo/
    echo/
    echo Press any key to continue...
    echo/
    echo/
    pause>nul
    start haxmenu\pin.bat
    goto menu
)

重要的是,如果没有将用户应选择的选项之一定义为默认值,则为该解决方案预定义变量errorlevel2的值,该字符串至少包含一个双引号,因为变量errorlevel2必须存在任何值,以避免在线上出现语法错误,从而删除了字符串中的所有双引号.

It is important that the value of variable errorlevel2 is predefined with a string containing at least one double quote for this solution if not one of the options the user should choose from is defined as default value because the variable errorlevel2 must exist with any value to avoid a syntax error on line removing all double quotes from string.

请参见为什么在命令行上使用"set var = text"后,为什么没有带有'echo%var%'的字符串输出?

解释为什么即使在值包含双引号的情况下也使用语法set "variable=value".

See Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation why using syntax set "variable=value" even with value containing a double quote.

通过使用延迟扩展可以避免由于用户输入字符串而导致的字符串比较语法错误,因为在这种情况下, IF 条件行本身不会被输入字符串修改.

A syntax error on string comparison because of string entered by the user could be avoided by using delayed expansion because the IF condition line itself is not modified in this case by the input string.

@echo off
setlocal EnableDelayedExpansion
:menu
set "errorlevel2="
set /P "errorlevel2=Please enter your choice: "
if "!errorlevel2!"=="s" (
    echo/
    echo/
    echo Press any key to continue...
    echo/
    echo/
    pause>nul
    start haxmenu\pin.bat
    goto menu
)
endlocal

现在总是在提示用户之前删除变量errorlevel2.

The variable errorlevel2 is deleted now always before prompting the user.

无论用户输入什么内容,都不会修改批处理代码行本身,因此,即使输入的字符串带有双引号的奇数,字符串比较也始终有效.

Whatever the user enters does not modify the batch code line itself and therefore the string comparison always works even with a string entered with an odd number of double quotes.

Windows为任务提供命令选择(Microsoft文章)用户应通过按键选择提供的选项之一.对于单个字符的选择,使用此命令几乎总是比使用set /P更好.

Windows offers the command choice (Microsoft article) for tasks where a user should select one of offered options by key. Using this command is nearly always better for single character choices than using set /P.

但是选择(SS64文章)的使用方式也分别取决于Windows版本使用的是 choice 的哪个版本,因为有多个版本具有不同的语法和功能.如果不再需要批处理文件与Windows Vista之前的Windows版本的兼容性,则应将选择用于简单菜单,因为该批处理文件绝对不会在Windows XP或更旧的Windows上运行.

But how choice (SS64 article) can be used depends also on version of Windows respectively which version of choice is used as there are multiple versions with different syntax and capabilities. choice should be used for simple menus if compatibility of batch file with Windows versions prior Windows Vista is not needed anymore as the batch file is definitely never run on Windows XP or even older Windows.

这篇关于为什么在使用IF命令进行字符串比较时,由于语法错误而退出我的批处理文件处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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