有什么办法通过批处理文件的执行跟踪? [英] Is there any way to trace through the execution of a batch file?

查看:86
本文介绍了有什么办法通过批处理文件的执行跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些大的批处理文件,我想他们改写到一个更开发商友好的语言。

I inherited some large batch files, and I'd like to rewrite them to a more "developer friendly" language.

我想找出以下的事情:


  • 这所谓的其他脚本

  • 启动什么其他进程

  • 它写入到哪些文件

  • 将它设置什么样的环境变量,它使用的,哪些确实

对于最后一点,我知道我能做到这一点,我开始之前:

For the last point, I'm aware I can do this before I start:

set > original_environment.txt

后,我跑了,我可以这样做:

And after I run it, I can do this:

set > new_environment.txt

和只是做它们之间的差异......但我可能会错过一些变量时,脚本完成,可能会被取消设置(或者甚至是所有的人,如果的下运行脚本 SETLOCAL )。

and just do a diff between them ... but I'll probably miss some variables that may be unset when the script finishes ( or even all of them if the script's ran under setlocal ).

时发现有没有我整个脚本code加入万吨echo语句的所有这些事情的方法吗?
有这样一个工具,可以监视批处理文件启动的进程,并告诉我所做的一切?

Is there any way of finding all those things without me adding tons of echo statements throughout the script code? Is there such a tool that can monitor the process started by the batch file and tell me everything it did?

推荐答案

有没有直接的方法来做到这一点。但是,这不是不可能创建一个。

由于Windows XP / Vista / 7的出来了good'ole一套DOS批处理命令得到了极大的提升,虽然不是很多使用它们,甚至RTFM( FOR /??)

Since Windows XP/Vista/7 came out the good'ole set of DOS batch commands has been greatly upgraded, although not many uses them or even RTFM (FOR /??)

所以我在这里给你一个简单的纯批TRACER是利用 FOR / F 行解析开关:

So here I give you, a simple pure-batch TRACER that utilizes the FOR /F line-parsing switch:

@ECHO OFF

FOR /F "delims=" %%L IN (%1) DO (

  CLS

  ECHO __________________________________________________
  ECHO                            ENV. VARIABLES *BEFORE*
  SET

  ECHO __________________________________________________
  ECHO                                               LINE
  ECHO %%L

  ECHO __________________________________________________
  ECHO Hit any key to execute the line ...
  PAUSE > NUL

  ECHO __________________________________________________
  ECHO                                            EXECUTE
  %%L

  ECHO __________________________________________________
  ECHO Hit any key to fetch the next line...
  PAUSE > NUL

)

ECHO END OF FILE

您可以把它作为开始和你去修改它。

You can take it as a start and modify it as you go.

下面是你如何使用它:

DEBUG.BAT TEST.BAT

和我也会给你一个测试文件来尝试一下:

And I'll also give you a test file to try it out:

@ECHO OFF

ECHO Hello World! 
SET aaa=1
SET bbb=2

ECHO Doing step 2
SET aaa=
SET ccc=3

ECHO Doing step 3
SET bbb=
SET ccc=

ECHO Finished!


DEBUG.BAT 的事情,但是,它的简单的原因,有一定的局限性的的可左右,如果你够巴掌批工作-fu在那里。


This DEBUG.BAT thing, however, due of its simplicity, has some limitations BUT which can be worked around if you slap enough BATCH-fu in there.


  • 它不能处理多行块 ::这可以围绕由具有命令解析令牌和建行为只能自己进来,和如果它遇到了一个开括号,只转储括号块内容到一个临时文件,然后调用本身上的临时文件如: DEBUG tempfile.bat

  • 它不能处理跳转 ::您当然可以,做一个如果查了 GOTO标签然后执行 FOR / F 来解析出标签本身,那么也许使用第二个参数%2 DEBUG.BAT 来指定标签跳转到,在这种情况下,如果指定了这个非常参数,你刚刚纺 FOR / F ,直到所需的标签映入眼帘,然后在下一行调试正常进行。

  • 有从单一的设置太多的信息 ::只是你的集&gt做了什么; before.txt 及之后的事情,但这样做的每一行,然后运行的文件的CMD线DIFF工具(很多都可以在网络上)。然后你会得到从去年的步骤,改变了每个变量的DIFF。你甚至可能能够避免ENV。变量乱用干脆在那里拍打在 SETLOCAL ENDLOCAL ,然后你会得到的只是当地的体育游戏但情况因人而异。

  • It cannot process multi-line blocks :: This can be worked around by having the FOR commands parse tokens and build the lines as they come in, and IF it encountered an open parenthesis, just dump the parenthesis block content to a temporary file and then call itself on the tempfile e.g. DEBUG tempfile.bat
  • It cannot process jumps :: You can of course, do an IF check for a GOTO label then do a FOR /F to parse out label itself, then maybe utilize the second argument %2of DEBUG.BAT to specify the label to jump to, in which case if this very argument is specified you'd just spin the FOR /F until the desired label came into view and then proceeds with normal debugging on the next line.
  • There is too much information from a single SET :: Just do what you did with the SET > before.txt and after thing but do it on each line and then run a cmd-line DIFF tools on the files (plenty are available on the net). Then you'll get a DIFF of each variable that has changed since last step. You might even be able to avoid the env. variables mess altogether by slapping in SETLOCAL and ENDLOCAL in there and then you'd get just the local SETs ... but YMMV.

这些都是一些。如果您发现了一些表演停止限制或其他增强功能将帮助您钉,去年的bug,请随时只是让我知道(通过评论),如果我可以,我会尽力帮助你。

Those are some. If you've found some show-stopping limitation or whatever enhancements would help you nail that last bug, feel free to just let me know (via the comments) and I'll try to help you if I can.

希望这有助于。

这篇关于有什么办法通过批处理文件的执行跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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