[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[其他] 为什么开启延迟变量扩展需要两个^^!转义!字符,而不开只是一个^,转义!字符

本帖最后由 GNGW 于 2024-3-20 16:04 编辑

起因是因为一串代码,我在论坛上看到的,原帖地址http://bbs.bathome.net/viewthread.php?tid=15680
  1. set str=!我潜心向神学习智慧之道!
  2. echo;        1)!str!【%str%】为什么值为空呢?因这个变量“^!我潜心向神学习智慧之道^!”没有SET。
复制代码
这是源代码,我发现“!我潜心向神学习智慧之道!”这句话并没有显示,碎片记忆中想加个^试试,于是改成了这样
  1. set str=!我潜心向神学习智慧之道!
  2. echo;        1)!str!【%str%】为什么值为空呢?因这个变量“^^!我潜心向神学习智慧之道^^!”没有SET。
复制代码
成功显示,联系到很多地方都需要用双字符,在我的上篇帖子中:http://www.bathome.net/redirect. ... 8649&pid=279265,又好奇为什么本身使用^脱字符,就可以转义特殊字符了,为什么还要加一个^脱字符将后面的脱字符进行转义为普通字符,才能显示“!我潜心向神学习智慧之道!”呢?根据这一原因,进行了深入的研究,提问,发现在说,脱字符,这个问题跟到这,阅读了部分帖子,发现一个逻辑或者机制,或者说理解,^与任何字符在一起此字符都是普通字符,在开启延迟变量后,特殊符号!需要两个^^才转化成普通字符,我想这和延迟变量扩展机制有关,有没有大佬普及科普一下。

回复 2# 77七


    原帖地址http://bbs.bathome.net/viewthread.php?tid=15680

TOP

本帖最后由 GNGW 于 2024-3-20 15:58 编辑

回复 4# buyiyang


    可以详述一下机理吗大佬可以说一下,去掉脱字符的原因吗?我发现预处理这种情况和双符号的情况,都会去掉一个符号,这是为什么?什么机理?

TOP

本帖最后由 GNGW 于 2024-3-21 11:34 编辑

回复 6# Five66


    这个帖子中关于
If the command token begins with :, and this is the first round of phase 2 (not a restart due to CALL in phase 6) then
The token is normally treated as an Unexecuted Label.
The remainder of the line is parsed, however ), <, >, & and | no longer have special meaning. The entire remainder of the line is considered to be part of the label "command".
The ^ continues to be special, meaning that line continuation can be used to append the subsequent line to the label.
An Unexecuted Label within a parenthesized block will result in a fatal syntax error unless it is immediately followed by a command or Executed Label on the next line.
( no longer has special meaning for the first command that follows the Unexecuted Label.
The command is aborted after label parsing is complete. Subsequent phases do not take place for the label
There are three exceptions that can cause a label found in phase 2 to be treated as an Executed Label that continues parsing through phase 7.
There is redirection that precedes the label token, and there is a | pipe or &, &&, or || command concatenation on the line.
There is redirection that precedes the label token, and the command is within a parenthesized block.
The label token is the very first command on a line within a parenthesized block, and the line above ended with an Unexecuted Label.
The following occurs when an Executed Label is discovered in phase 2
The label, its arguments, and its redirection are all excluded from any echo output in phase 3
Any subsequent concatenated commands on the line are fully parsed and executed.
For more information about Executed Labels vs. Unexecuted Labels, see https://www.dostips.com/forum/vi ... &p=55405#p55405
这一段中给出的链接并没有解答^脱字符的完全原理,同时并没有解开我的疑惑 (再次讨教)https://stackoverflow.com/questi ... pts/7970912#7970912

只有这一小段,解释了解释器在开启延迟变量拓展之后的扫描过程,去掉^脱字符,但对于,后面^! 作为普通字符之后,没有详细描述对^!第一个^脱字符的处理。
No change is made to tokens that do not contain !.

For each token that does contain at least one !, scan each character from left to right for ^ or !, and if found, then

5.1 (caret escape) Needed for ! or ^ literals
If character is a caret ^ then
Remove the ^
Scan the next character and preserve it as a literal
Continue the scan
吹毛求疵的人,找问题的人,好奇心强的人,动手能力强的人,虚心的人,以及一个简单的研究技术的人。

TOP

本帖最后由 GNGW 于 2024-3-21 17:15 编辑

回复 8# Five66


    但是这个上面没有说明流程走了两次,进行了两次扩展。我的意思是没有任何的证明.....能够证明转义了两次,在没有证明或验证的情况下,任何的猜测都有可能是错的。
吹毛求疵的人,找问题的人,好奇心强的人,动手能力强的人,虚心的人,以及一个简单的研究技术的人。

TOP

回复 10# Five66


    噢噢,看到了。“自己根据步骤模拟一下代码的解析和执行流程不就行了”这个要怎么执行?
吹毛求疵的人,找问题的人,好奇心强的人,动手能力强的人,虚心的人,以及一个简单的研究技术的人。

TOP

返回列表