将检查什么语法,如果被定义包含空格的变量名? [英] What syntax will check if a variable name containing spaces is defined?

查看:252
本文介绍了将检查什么语法,如果被定义包含空格的变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows用户定义的环境变量名称可以包含除任何字符 =

Windows user defined environment variable names can contain any character except =.

特殊字符可以转义被包括在内。一个更简单的方法是简单地附上引号内的整套前pression。例如:

Special characters can be included by escaping them. A simpler method is to simply enclose the entire SET expression within quotes. For example:

set "A weird & "complex" variable=My value"
set A weird ^& "complex" variable=My value

以上两个前pressions给出相同的结果。变量名称为 A怪异&安培; 复杂变量 并值为 我的价值

Both expressions above give the same result. The variable name is A weird & "complex" variable and the value is My value

该如果定义构造用于测试如果一个变量定义。行情没有为这个测试,特殊字符的名称(包括引号)工作,必须转义。

The IF DEFINED construct is used to test if a variable is defined. Quotes don't work for this test, special characters in the name (including quotes) must be escaped.

set "A&B=value"
if defined A^&B echo This works
if defined "A&B" echo This does not work

以上逃脱测试工作得很好。带引号的测试无法正常工作

The above escaped test works just fine. The quoted test does not work

但我怎么可以测试是否存在包含空格的变量?

But how can I test if a variable containing spaces exists?

set "A B=value"
if defined A^ B echo this does not work!

好像上面应该工作,但它不会!

It seems like the above should work, but it doesn't!

我在寻找一个答案,不涉及使用%A B%以上!A B扩大变量!

I'm looking for an answer that does NOT involve expanding the variable using %A B% or !A B!

推荐答案

Interessting的问题(我爱这语法基础问题)。

Interessting question (I love this syntax base questions).

显然,你知道如何与延迟扩展检查​​,也为参数的工作原理。

Obviously you know how to check it with delayed expansion and also FOR-parameters works.

@echo off
setlocal
set "AAA BBB=value"
set ""AAA BBB"="
set "AAA="
for %%a in ("AAA BBB") do if defined %%~a echo FOR: This works

setlocal EnableDelayedExpansion
set "varname=AAA BBB"
if defined !varname! echo Delayed: This works

if defined %varname% ( echo percent: Never comes here 
) ELSE ( echo percent: Never comes here ? )

if defined AAA^ BBB ( echo escape1: Never comes here
) ELSE ( echo escape1: fails )

set AAA=Hello
if defined AAA^ BBB ( 
   echo escape2: It only test for AAA the BBB will be "removed"
) ELSE ( echo escape2: fails )

set "space= "
if defined AAA!space!BBB echo inject space: This works

if defined "AAA BBB"  (echo Quote1: Never comes here 
) ELSE ( echo Quote1: Fails )

set ""AAA BBB"=value"
if defined "AAA BBB" echo Quote2: This works, it checks for "AAA BBB" with quotes

在我opionion,在 escape2 的例子解析器第一行拆分成标记是这样的:结果
<如果> <&定义GT; < AAA BBB> <回声....
但在执行时的如果定义的它重新扫描< AAA BBB> 标记,因此只得到了 AAA 。结果
你不能注入第二逃生像 AAA ^^^ BBB 为名为 AAA ^变量这只搜索

In my opionion, in the escape2 example the parser first split the line into tokens this way:
<if> <defined> <AAA BBB> <echo .... But at the execution time of the if defined it rescan the <AAA BBB> token so it only gets the AAA.
You can't inject a second escape like AAA^^^ BBB as this only searches for the variable named AAA^

我看不到解决方案,无需延迟/ FOR,作为空间的转义总是失败。

I can't see a solution without delaying/FOR, as the escaping of the space always fails.

编辑:也可以用解决SET&LT;&VARNAME GT; 结果
IJ preST的解决方案使用SET命令来测试变量,无需逃避VARNAME的。结果
但是,这也显示了内,并在VARNAME月底空间interessting行为。

It can also be solved with SET <varname>
The solution of ijprest uses the SET command to test the variable without the need of escaping the varname.
But it also shows interessting behaviour with spaces inside and at the end of a varname.

这似乎遵循以下规则:结果
SET VARNAME 开头VARNAME所有变量搜索,但首先它消除VARNAME的最后一个空格字符之后的所有字符,并删除所有前导空格。结果
所以你不能搜索与年初的空间变量(但它也是一个有点棘手创建这样一个VARNAME)。

It seems to follow these rules:
SET varname searches for all variables beginning with varname, but first it removes all characters after the last space character of varname, and it removes all leading spaces.
So you can't search for variables with beginning with space (but it is also a bit tricky to create such a varname).

同样的行为也是积极的,如果VARIABLENAME括入引号,但是存在,那么多一个规则。结果
最后一个引号之后,首先删除所有字符,如果至少有两个引号。
使用文字引号内,并使用空间-rule。

The same behaviour is also active if the variablename is enclosed into quotes, but then exists one more rule.
First remove all characters after the last quote, if there are at least two quotes. Use the text inside of the quotes, and use the "space"-rule.

样。

set    "   abc def ghi"  junk junk
*** 1. removes the junk 
set    "   abc def ghi"
*** 2. removes the quotes
set       abc def ghi
*** 3. removes all after the last space, and the trailing spaces
set abc def
*** Search all variables beginning with abc def

这篇关于将检查什么语法,如果被定义包含空格的变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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