『楼 主』:
[讨论]关于SET [variable=[string]]变量名的限制
使用 LLM 解释/回答一下
因为是初学者,所以对SET [variable=[string]]的变量名的限制不是很清楚,用set /?查看帮助之后发现有这样一句 :SET 命令不允许变量名含有等号。
刚开始并没有对这句话注意,做练习的时候发现一个问题,我在命令行解释器测试:
1、C:\Documents and Settings\wang>set var =look
2、C:\Documents and Settings\wang>set name=%var:l=b%
3、C:\Documents and Settings\wang>set name
4、name=%var:l=b%
当时觉得很奇怪,怎么name 没有被var替换呢?然后又用set var发现:
5、C:\Documents and Settings\wang>set var
6、var =look
这时候觉得有点问题(因为这时候还没发现问题),然后用 echo %var%发现:
7、C:\Documents and Settings\wang>echo %var%
8、%var%
突然觉得是var有问题 再用echo %var %
9、C:\Documents and Settings\wang>echo %var %
10、look
这时候才突然明白了,set var =string跟set var=srting这两句不注意看的时候以为一样的(我今天就犯了这个问题),细看会发现,第一句var 后面比第二句var多了一个空格,这样才导致问题。
我在第1句里set var =look,这时候是设置var ,而不是var,所以导致第2句name=%var:l=b%后,name的值为=%var:l=b%,我本意是吧look变成book赋给name.
第5句和第6句里之所以能够用set var看到var 的值,是因为set的另一句帮助:
可仅用一个变量激活 SET 命令,等号或值不显示所有前缀匹配
SET 命令已使用的名称的所有变量的值。例如:
SET P
会显示所有以字母 P 打头的变量
这样,因为前面没有注意到空格的问题,所以这里也不是很留意。
直到7、8两句才感觉到可能是空格的问题,最后发现果然是这个问题。
因为以前用其他语言编写程序的时候总是习惯在变量名后面加上一个空格,这样便于阅读。但是在dos的set里面,有空格跟没有空格是不一样的两个变量。
发现这个问题之后,我想到开头说的那句帮助,那么变量名除了=外,其他的特殊符号都可以用了?
简单测试了一下,发现下面几个问题:
1、变量名里不能使用&,(例如 set po6&9$%^155 =123),使用的话会被认为是两个命令的连接而报错(这只是我的测试,不知道别人是不是)
2、变量名里不能使用成对的引号""(例如 set hha"#%45" rg =456),使用的话也会出错。
3、变量名里如果有^,不会报错,但是会把^过滤掉。
其他的字符也测试了一点,好像没有什么问题,例如可以设置下面的
set [{!}~e ge w4%t %] sd =6
这样变量 [{!}~e ge w4%t %] sd 的值就是6了。
这样的话,并不是变量名只有=不能用了,而且结尾带空格的变量很容易跟不带空格的同名变量被人混用。
以上只是做练习的时候发现的一些问题,希望高手能给予理论层的解释,谢谢。
Because I'm a beginner, I'm not very clear about the restrictions on the variable names in SET ]. After checking the help with set /?, I found this sentence: "The SET command does not allow variable names to contain equal signs."
At first, I didn't pay attention to this sentence. When doing exercises, I found a problem. I tested in the command-line interpreter:
1. C:\Documents and Settings\wang>set var =look
2. C:\Documents and Settings\wang>set name=%var:l=b%
3. C:\Documents and Settings\wang>set name
4. name=%var:l=b%
I was very puzzled why name wasn't replaced by var. Then I used set var and found:
5. C:\Documents and Settings\wang>set var
6. var =look
At this time, I felt a little problem (because I didn't find the problem yet). Then I used echo %var% and found:
7. C:\Documents and Settings\wang>echo %var%
8. %var%
Suddenly, I felt that var was problematic. Then I used echo %var %:
9. C:\Documents and Settings\wang>echo %var %
10. look
At this time, I suddenly understood that when I set var =look in the first sentence, it was setting var (with a space), not var. So after the second sentence name=%var:l=b%, the value of name was =%var:l=b%. My original intention was to replace look with book and assign it to name.
In the 5th and 6th sentences, the reason why I could use set var to see the value of var was because of another help sentence of SET:
You can use only one variable to activate the SET command. If there is no equal sign or value, all prefix-matched variable values of the SET command will be displayed.
For example:
SET P
will display all variables starting with the letter P
In this way, because I didn't pay attention to the space problem before, I didn't pay much attention here.
It wasn't until the 7th and 8th sentences that I felt there might be a space problem. Finally, I found that it was indeed this problem.
Because when I used other languages to write programs before, I always used to add a space after the variable name, which was convenient for reading. But in DOS's set, a variable with a space and one without a space are two different variables.
After finding this problem, I thought of the help sentence at the beginning. Then can other special symbols be used in variable names except =?
I simply tested and found the following problems:
1. Special characters like & cannot be used in variable names (for example, set po6&9$%^155 =123). If used, it will be considered as the connection of two commands and an error will be reported (this is just my test, I don't know about others).
2. Paired quotation marks "" cannot be used in variable names (for example, set hha"#%45" rg =456). If used, an error will also occur.
3. If there is ^ in the variable name, no error will be reported, but ^ will be filtered out.
I also tested some other characters, and there seemed to be no problems. For example, I can set:
set sd =6
In this way, the value of the variable sd is 6.
In this way, it's not that only = cannot be used in variable names, and variables ending with spaces are easily confused with non-space-ending variables of the same name by people.
The above are just some problems I found when doing exercises. I hope experts can give theoretical explanations. Thank you.
|