如何在Windows命令国米preTER(CMD.EXE)解析脚本? [英] How does the Windows Command Interpreter (CMD.EXE) parse scripts?

查看:117
本文介绍了如何在Windows命令国米preTER(CMD.EXE)解析脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑进 ss64.com 它提供了关于如何编写批处理脚本的Windows命令国米preTER将运行良好的帮助

I ran into ss64.com which provides good help regarding how to write batch scripts that the Windows Command Interpreter will run.

不过,我一直无法找到语法的一个很好的解释批处理脚本的,事情如何扩大或不扩大,如何脱离的东西。

However, I have been unable to find a good explanation of the grammar of batch scripts, how things expand or do not expand, and how to escape things.

下面是我一直没能解决问题的样本:

Here are sample questions that I have not been able to solve:


  • 如何报价系统进行管理?我做了一个 TinyPerl 脚本结果
    的foreach $ I(@ARGV){打印'*'$ I;} ),编译它,并把它称为是这样的:

    • my_script.exeABC→输出 *一个B * C

    • my_script.exeABC→输出它 *A * B * C

    • How is the quote system managed? I made a TinyPerl script
      ( foreach $i (@ARGV) { print '*' . $i ; } ), compiled it and called it this way :
      • my_script.exe "a ""b"" c" → output is *a "b*c
      • my_script.exe """a b c""" → output it *"a*b*c"

      • 集B = A 回声%A%B%C%% AAC%

      • 设置A = B 回声%A%B%C% BB C%

      • set b=a , echo %a %b% c%%a a c%
      • set a =b, echo %a %b% c%bb c%

      感谢您的灯。

      推荐答案

      我做了一些/许多实验,这似乎是主要的结果。

      I made some/many experiments, and this seems to be the main results.

      要更好地了解如何一批作品,为什么有时会逃避的作品,似乎要失败其他时间。
      我工作了这一点,许多实验,我建立的测试,所以我可以找出离散阶段的顺序。

      To better understand how batch works, and why sometimes escaping works and other times it seems to fail. I work this out by many experiments, and I build tests so I can identify the order of the discrete phases.

      存在多个区域检查。
      我得到了

      There exists multiple areas to examine. I got the


      • BatchLineParser - 批处理文件内的解析器,对线路或块

      • CmdLineParser - 像BatchLineParser,而是直接在命令提示符下,不同的作品

      • LabelParser - CALL / GOTO和标签的工作原理

      • CommandBlockCaching - 如何括号和缓存作品

      • 标记生成器 - 如何单个记号(字符组)建立和哪些阶段

      code在批处理文件中的行有多个阶段(在命令行的扩展是不同的!)。

      A line of code in a batch file has multiple phases (on the command line the expansion is different!).

      这个过程与阶段开始1

      The process starts with phase 1

      相位/顺序结果
      1)第一阶段(百分比):


      • %% 被替换成了一个

      • 参数变量的扩展(%1 %2 等)

      • %VAR%,扩建如果VAR不存在什么也没有取代它

      • 有关完整的说明,从dbenham <读此href=\"http://stackoverflow.com/questions/4094699/how-does-the-windows-command-inter$p$pter-cmd-exe-parse-scripts/7970912#7970912\">Same螺纹:百分号展开

      • A double %% is replaced by a single %
      • Expansion of argument variables (%1, %2, etc.)
      • Expansion of %var%, if var does not exists replace it with nothing
      • For a complete explanation read this from dbenham Same thread: percent expansion

      1.5)删除所有&LT; CR&GT; (CarriageReturn符进行)从线

      1.5) Remove all <CR> (CarriageReturn 0x0d) from the line

      2)第二阶段(特殊字符, &LT; LF&GT; ^ &安培; | &LT; &GT; 看看每个字符

      2) Phase(Special chars, " <LF> ^ & | < > ( ): Look at each character


      • 如果它是一个引号()切换报价标志,如果报价标志有效,下列特殊字符不再是特殊的: ^ &安培; | &LT; &GT;

      • 如果它是一个符号( ^ )的下一个字符没有特殊意义,本身被删除插入符,插入符是该行的最后一个字符时,下一行被追加,下一行的第一个系统字符为转义系统字符总是处理。

        • &LT; LF&GT; 立刻停止解析的,而不是在前面插入符号

        • If it is a quote (") toggle the quote flag, if the quote flag is active, the following special characters are no longer special: ^ & | < > ( ).
        • If it is a caret (^) the next character has no special meaning, the caret itself is removed, if the caret is the last character of the line, the next line is appended, the first charater of the next line is always handled as escaped charater.
          • <LF> stops the parsing immediatly, but not with a caret in front

          • 如果解析器是不是找一个命令令牌,则不是特别的。

          • 如果分析器正在寻找一个命令令牌,并找到,然后开始一个新的复合语句并增加括号计数器

          • 如果括号计数器> 0,那么终止复合语句和递减计数器括号

          • 如果到达行结束和括号计数器> 0,那么下一行会被添加到复合语句
          • 如果括号计数器= 0,解析器寻找一个条命令,那么像功能 REM 声明,并在该行的所有剩余字符都被忽略

          • If the parser is not looking for a command token, then ( is not special.
          • If the parser is looking for a command token and finds (, then start a new compound statement and increment the parenthesis counter
          • If the parenthesis counter is > 0 then ) terminates the compound statement and decrements the parenthesis counter.
          • If the line end is reached and the parenthesis counter is > 0 then the next line will be appended to the compound statement (starts again with phase 1)
          • If the parenthesis counter is = 0, and the parser is looking for a commmand, then ) functions like a REM statement, and all remaining characters on the line are ignored

          3)阶段(回声):如果反响的打印阶段1和2的结果。

          3) Phase(echo): If "echo is on" print the result of phase 1 and 2


          • 有关 - 环 - 块呼应多次,在for循环的上下文中第一次,以未膨胀为 - 环 - 瓦尔

          • 对于每个迭代,块回荡着扩大了 - 环 - 瓦尔

          ----这两个阶段都算不上直接跟随,但它没有什么区别结果
          4)阶段( - 环 - 瓦尔扩展): 扩展%% A

          5)阶段(感叹号):只有当延迟扩展上,看着每一个字符

          5) Phase(Exclamation mark): Only if delayed expansion is on, look at each character


          • 如果它是一个符号( ^ )的下一个字符没有特殊意义,插入符号本身被删除

          • 如果它是一个惊叹号,寻找下一个感叹号(脱字符不能观察到了),扩展到变量的内容

            • 连续开被折叠成一个单一的

            • 任何剩余不能配对被删除

            • If it is a caret (^) the next character has no special meaning, the caret itself is removed
            • If it is an exclamation mark, search for the next exclamation mark (carets are not observed anymore), expand to the content of the variable
              • Consecutive opening ! are collapsed into a single !
              • Any remaining ! that cannot be paired is removed

              6)第一阶段(呼叫/插入符号两倍):只有在CMD令牌CALL

              6) Phase(call/caret doubling): Only if the cmd token is CALL


              • 如果第一个标记是呼叫,开始再次第1阶段,但第二阶段,延迟扩展不会在这里处理的第二次后停止

              • 删除第一个呼叫,所以多个呼叫的可堆叠

              • 双所有插入记号(正常插入记号似乎保持不变,因为在第2阶段,他们被减少到一个,但在引号它们用途不同加倍)

              • If the first token is "call", start with phase 1 again, but stops after phase 2, delayed expansion are not processed a second time here
              • Remove the first CALL, so multiple CALL's can be stacked
              • Double all carets (the normal carets seems to be stay unchanged, because in phase 2 they are reduced to one, but in quotes they are effectivly doubled)

              7)阶段(执行):执行命令


              • 不同的令牌用在这里,取决于执行的内部命令

              • 在一个设置NAME =内容的情况下,第一个等号行的最后一个引号的完整内容被用作内容的道理,如果有等号后无报价,该行的其余部分被使用。

              • Different tokens are used here, depends on the internal command executed
              • In case of a set "name=content", the complete content of the first equal sign to the last quote of the line is used as content-token, if there is no quote after the equal sign, the rest of the line is used.

              作品像BatchLine解析器,但是:

              Works like the BatchLine-Parser, but:


              • 转到/呼叫标签是不允许

              阶段1(百分比):


              • %VAR%会被VAR的内容所取代,如果没有定义的变种,前presssion将保持不变

              • %%的任何特殊处理,第二%的可能是一个变种的开始,设置VAR =内容,%% VAR %%扩大到%含量%

              Phase5(惊叹号):仅在DelayedExpansion已启用

              Phase5(exclamation mark): only if "DelayedExpansion" is enabled


              • !无功!将VAR的内容所取代,如果没有定义的变种,前presssion将保持不变

              例如。 FOR / F有usebackq%%一个IN(命令块)不回应%% A

              该命令块将被解析的两倍,首先,BatchLineParser(循环是间歇内)或CmdLineParser(在CMD线环)是活动的,在第二次运行总是CmdLineParser是活动的。
              在第二次运行,如果它与注册表项启用DelayedExpansion才有效。

              The command block will be parsed two times, at first the BatchLineParser(the loop is inside a batch) or the CmdLineParser(loop on the cmd-line) is active, at the second run always the CmdLineParser is active. In the second run, DelayedExpansion is active only if it is enabled with the registry key

              第二次运行就像调用带有 CMD / C

              The second run is like calling the line with cmd /c

              的变量设置,因此不是永久性的。

              Setting of variables are therefore not persistent.

              希望它可以帮助
              埃里克·扬

              Hope it helps Jan Erik

              这篇关于如何在Windows命令国米preTER(CMD.EXE)解析脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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