有没有办法跟踪批处理文件的执行情况?
我继承了一些大型批处理文件,我想将它们重写为更“开发人员友好”的语言。
我想了解以下内容:
- 它调用了哪些其他脚本,它
- 启动了哪些其他进程,它
- 写入了哪些文件,
- 它使用了哪些环境变量,它设置了哪些变量
对于最后一点,我知道我可以做在我开始之前:
set > original_environment.txt
在运行它之后,我可以这样做:
set > new_environment.txt
并在它们之间进行比较...但是我可能会错过一些在脚本完成时可能未设置的变量(或者甚至所有变量,如果脚本在 setlocal
下运行)。
有没有办法找到所有这些东西,而无需我在整个脚本代码中添加大量的 echo 语句? 有没有这样的工具可以监视批处理文件启动的进程并告诉我它所做的一切?
I inherited some large batch files, and I'd like to rewrite them to a more "developer friendly" language.
I'd like to find out the following things:
- what other scripts it calls
- what other processes it starts
- what files does it write to
- what environment variables it uses, which ones does it set
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
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
).
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你只需看看它们的内部并弄清楚它们的作用即可。
您还可以删除任何
echo off
语句和@
前面的命令;这样,每个命令都会在运行之前输出,您可以将输出重定向到文件以便稍后研究。据我所知,没有用于批处理文件的调试工具,但我考虑编写一个。
You can just look inside them and figure out what they do.
You can also remove any
echo off
statements and@
preceding commands; that way every command is output before it's run and you can redirect the output to a file to study it later.There is no debugging tool for batch files that I am aware of—but I contemplated writing one once.
没有直接的方法可以做到这一点。但创建一个也不是不可能。
自从 Windows XP/Vista/7 出现以来,good'ole 的 DOS 批处理命令集已经得到了很大的升级,尽管使用它们甚至 RTFM 的人并不多(
FOR /?
?)因此,我在这里为您提供一个简单的纯批处理 TRACER,它利用
FOR /F
行解析开关:您可以将其作为开始并随时修改它。
下面是你如何使用它:
我还将给你一个测试文件来尝试一下:
但是,由于它的简单性,这个
DEBUG.BAT
东西有一些限制 BUT 如果你在那里打了足够多的 BATCH-fu ,就可以解决这个问题。FOR
命令解析标记并在它们进入时构建行来解决,以及IF< /code> 它遇到了一个左括号,只需将括号块内容转储到临时文件,然后在临时文件上调用自身,例如
DEBUG tempfile.bat
IF
检查GOTO 标签
,然后执行FOR /F
解析标签本身,然后也许可以利用DEBUG.BAT
的第二个参数%2
指定要跳转到的标签,在这种情况下,如果指定了这个参数,您只需旋转FOR /F
直到所需的标签进入视图,然后在下一行继续正常调试。SET
中的信息太多了 :: 只需执行SET > 所做的操作即可before.txt
和 after 的事情,但在每一行上执行此操作,然后在文件上运行命令行 DIFF 工具(网上有很多可用的工具)。然后,您将获得自上一步以来已更改的每个变量的差异。您甚至可以避免环境。通过在其中输入SETLOCAL
和ENDLOCAL
变量完全混乱,然后你只会得到本地 SET...但是 YMMV。这些是一些。如果您发现一些令人停止的限制或任何可以帮助您解决最后一个错误的增强功能,请随时告诉我(通过评论),我会尽力帮助您。
希望这有帮助。
There is no direct way to do it. But it's not impossible to create one.
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 /?
?)So here I give you, a simple pure-batch TRACER that utilizes the
FOR /F
line-parsing switch:You can take it as a start and modify it as you go.
Here's how you'd use it:
And I'll also give you a test file to try it out:
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.FOR
commands parse tokens and build the lines as they come in, andIF
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
IF
check for aGOTO label
then do aFOR /F
to parse out label itself, then maybe utilize the second argument%2
ofDEBUG.BAT
to specify the label to jump to, in which case if this very argument is specified you'd just spin theFOR /F
until the desired label came into view and then proceeds with normal debugging on the next line.SET
:: Just do what you did with theSET > 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 inSETLOCAL
andENDLOCAL
in there and then you'd get just the local SETs ... but YMMV.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.
Hope this helps.
不,无法调试旧式批处理文件。但是,您可以删除所有
ECHO OFF
语句,然后在运行它们时,所有命令和输出都将回显到控制台。No, there is no way to debug old style batch files. You can, however, just get rid of all the
ECHO OFF
statements, and then all the commands and output will be echo'd to the console when you run them.如果您愿意花一些钱,您应该看看 Running步骤批处理文件IDE及其调试功能。
我没有测试它,但它有一些功能可能会帮助您完成任务:
他们还提供试用版。
If you are willing to spend some money you should take a look at the Running Steps batch file IDE and its debugging capabilities.
I didn't test it, but it has some features that might help you in your task:
They also offer a trial version.