有没有办法跟踪批处理文件的执行情况?

发布于 2024-08-23 22:30:30 字数 503 浏览 4 评论 0原文

我继承了一些大型批处理文件,我想将它们重写为更“开发人员友好”的语言。

我想了解以下内容:

  • 它调用了哪些其他脚本,它
  • 启动了哪些其他进程,它
  • 写入了哪些文件,
  • 它使用了哪些环境变量,它设置了哪些变量

对于最后一点,我知道我可以做在我开始之前:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾忌 2024-08-30 22:30:30

你只需看看它们的内部并弄清楚它们的作用即可。

您还可以删除任何 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.

落在眉间の轻吻 2024-08-30 22:30:30

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

自从 Windows XP/Vista/7 出现以来,good'ole 的 DOS 批处理命令集已经得到了很大的升级,尽管使用它们甚至 RTFM 的人并不多(FOR /??)

因此,我在这里为您提供一个简单的纯批处理 TRACER,它利用 FOR /F 行解析开关:

@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

您可以将其作为开始并随时修改它。

下面是你如何使用它:

DEBUG.BAT TEST.BAT

我还将给你一个测试文件来尝试一下:

@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 东西有一些限制 BUT 如果你在那里打了足够多的 BATCH-fu ,就可以解决这个问题。

  • 它无法处理多行块 :: 这可以通过使用 FOR 命令解析标记并在它们进入时构建行来解决,以及 IF< /code> 它遇到了一个左括号,只需将括号块内容转储到临时文件,然后在临时文件上调用自身,例如 DEBUG tempfile.bat
  • 它无法处理跳转 : :当然,您可以执行 IF 检查 GOTO 标签,然后执行 FOR /F 解析标签本身,然后也许可以利用DEBUG.BAT 的第二个参数 %2 指定要跳转到的标签,在这种情况下,如果指定了这个参数,您只需旋转 FOR /F 直到所需的标签进入视图,然后在下一行继续正常调试。
  • 单个 SET 中的信息太多了 :: 只需执行 SET > 所做的操作即可before.txt 和 after 的事情,但在每一行上执行此操作,然后在文件上运行命令行 DIFF 工具(网上有很多可用的工具)。然后,您将获得自上一步以来已更改的每个变量的差异。您甚至可以避免环境。通过在其中输入 SETLOCALENDLOCAL 变量完全混乱,然后你只会得到本地 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:

@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.

Here's how you'd use it:

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!

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.

  • 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.

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.

魔法唧唧 2024-08-30 22:30:30

不,无法调试旧式批处理文件。但是,您可以删除所有 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.

陌路黄昏 2024-08-30 22:30:30

如果您愿意花一些钱,您应该看看 Running步骤批处理文件IDE及其调试功能。

我没有测试它,但它有一些功能可能会帮助您完成任务:

...

  • 类似 Visual Studio 的调试环境。
  • 丰富的调试命令集(单步进入、单步跳过、单步退出等)
  • 丰富的项目分析器可立即找到您的错误和警告。
  • 对延迟扩展环境的集成支持
    变量。
  • 多类型断点定义,满足您的多种调试需求。
  • 复杂的管道和重定向支持以及多色突出显示。
  • 环境变量可视化和修改支持。
  • 扩展信息窗口,实现真正的变量定义可视化。
  • 令人印象深刻的“命令”展开功能。
  • 交互式调用堆栈和参数窗口。

...

他们还提供试用版。

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:

...

  • Visual Studio-like debugging environment.
  • Rich set of debugging commands (step into, step over, step out, and more)
  • Rich Project analyzer to find your errors and warnings in no time.
  • Integrated support for delayed-expanded environment
    variables.
  • Multi-type breakpoint definitions to fit your multiple debugging needs.
  • Complex pipeline and redirection support with multi-color highlighting.
  • Environment variable visualization and modification support.
  • Expanded information window for true variable definition visualization.
  • Impressive 'For command' unrolling feature.
  • Interactive callstack and Parameters window.

...

They also offer a trial version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文