对带有不可接受符号(例如/或:)的变量使用IF语句 [英] Use IF statement on variables with unacceptable symbols (e.g. / or :)

查看:90
本文介绍了对带有不可接受符号(例如/或:)的变量使用IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自己的命令提示符.我想要一个带有条件(例如/h或类似条件)的命令

I wanted to make my own command prompt. And I wanted a command in it with conditions (e.g. /h or something like that)

这是我的命令:

if %adm%=="admin /y" (goto add)

我在原始命令提示符下对其进行了测试,并显示了以下内容:

I tested it on the original command prompt and it says this:

/y ="admin/y"目前是意外情况.

/y="admin /y" was unexpected at this time.

我尝试使用插入符号(^符号),但无效.

I tried using carets (the ^ symbol) but it didn't work.

推荐答案

命令 IF 在比较值时至少需要三个自变量:

The command IF expects at least three arguments on comparing values:

  1. 第一个参数是第一个值:一个字符串(或整数).
  2. 第二个参数是比较运算符:==EQU等.
  3. 第三个参数是第二个值:字符串(或整数).
  1. The first argument is the first value: a string (or an integer).
  2. The second argument is the comparison operator: ==, EQU, etc.
  3. The third argument is the second value: a string (or an integer).

IF 的执行方式在等价于NEQ,LSS,GTR的符号中作了详细解释,例如Windows批处理文件中的.在命令提示符窗口中也运行if /?,以在多个窗口页面上显示该命令的帮助.

How IF executes the comparison is explained in detail in answer on Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files. Run also if /? in a command prompt window to get displayed the help for this command on several window pages.

命令行上的参数分隔符是空格字符.

The argument separator on command line is the space character.

%adm%被环境变量adm的当前值替换.可以从临时删除批处理文件顶部的@echo off或将其更改为@echo ON或注释此行,然后在命令提示符窗口中运行批处理文件,而不是双击批处理来看到预处理后真正执行的操作.文件.

%adm% is replaced by current value of environment variable adm during preprocessing phase of the command line by Windows command interpreter cmd.exe before the command line is executed at all. What is really executed after preprocessing can be seen on temporarily removing @echo off from top of batch file or change it to @echo ON or comment this line, and run the batch file from within a command prompt window instead of double clicking on the batch file.

if admin /y=="admin /y" (goto add)

命令 IF admin解释为第一个参数.第二个参数是/y=="admin /y",它绝对不是受支持的比较运算符,这是错误消息的原因,因为 IF 确实不希望该字符串.

The command IF interprets admin as first argument. The second argument is /y=="admin /y" which is definitely not a supported comparison operator which is the reason for the error message because that string is really not expected by IF.

包含空格字符或字符之一的参数字符串&()[]{}^=;!'+,`~<|>要求将整个参数字符串用双引号引起来,以获取空格和其他字符,将其解释为参数字符串的文字字符.

An argument string containing a space character or one of the characters &()[]{}^=;!'+,`~<|> requires enclosing the entire argument string in double quotes to get the space and the other characters interpreted as literal characters of an argument string.

所以可能的解决方法是:

So a possible solution is:

if "%adm%"=="admin /y" goto add

批处理文件中的此命令行在预处理期间扩展为:

This command line in the batch file is expanded during preprocessing to:

if "admin /y" == "admin /y" goto add

可以看到Windows命令解释器在==作为第二个参数和有效的比较运算符之前和之后插入一个空格.

It can be seen that Windows command interpreter inserts a space before and after == being the second argument and a valid comparison operator.

圆括号已删除,因为默认情况下,命令 IF 被设计为执行单个命令行.仅在也需要 ELSE 分支的情况下才需要括号,或者应根据条件执行多个命令行.以(开头并以匹配的)结尾的命令块在预处理阶段会导致额外的步骤,因此,如果不需要,则应避免使用它.

The round brackets are removed as command IF is designed by default to execute a single command line. Parentheses are only needed if an ELSE branch is needed too, or multiple command lines should be executed depending on the condition. A command block starting with ( and ending with matching ) cause an extra step during preprocessing phase and should be avoided for that reason if not really needed.

但是请注意,在批处理文件中使用if "%adm%" == "admin /y" goto add时,分配给adm的字符串值中的任何双引号"都会再次破坏 IF 条件.可以在使用示例中看到

But be aware that any double quote " in string value assigned to adm breaks again the IF condition on using if "%adm%" == "admin /y" goto add in the batch file. This can be seen on using for example

set /P "adm="
if "%adm%" == "admin /y" goto add

然后用户在执行此批处理文件时输入:

And the user enters on execution of this batch file:

" == "" echo rd /Q /S "C:\" & rem "

批处理文件中的命令行在执行前先展开为:

The command line in the batch file is expanded before execution to:

if "" == "" echo rd /Q /S "C:\"   & rem "" == "admin /y" goto add

因此输出rd /Q /S "C:\",而没有echo批处理文件将开始删除所有递归目录,当前用户有权在这些目录上删除文件和文件夹.

So rd /Q /S "C:\" is output and without echo the batch file would start deleting all directories recursive on which the current user has the permissions to delete files and folders.

因此,如果批处理文件的用户输入了分配给环境变量adm的字符串,则强烈建议使用延迟的环境变量扩展,以避免在预处理阶段将命令行修改为无效的命令行或执行与设计目的完全不同的命令行.

So if the string assigned to environment variable adm is input by a user of the batch file, it is highly recommended to use delayed environment variable expansion to avoid a modification of the command line during preprocessing phase to an invalid command line or a command line which does something completely different than it is designed for.

setlocal EnableExtensions EnableDelayedExpansion
rem Other command lines in the batch file.
set /P "adm="
if /I "!adm!" == "admin /y" goto add

环境变量adm是使用延迟的环境变量扩展引用的,因此不能通过用户输入来修改此命令行以执行.

The environment variable adm is referenced with usage of delayed environment variable expansion and therefore this command line can't be modified for execution by input of the user.

另外,可选参数/I用于使字符串比较不区分大小写.

Additionally the optional parameter /I is used to make the string comparison case-insensitive.

这篇关于对带有不可接受符号(例如/或:)的变量使用IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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