windows 如何调试 .BAT 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/165938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I debug a .BAT script?
提问by Vhaerun
Is there a way to step through a .bat script? The thing is, I have a build script , which calls a lot of other scripts, and I would like to see what is the order in which they are called, so that I may know where exactly I have to go about and add my modifications.
有没有办法单步执行 .bat 脚本?问题是,我有一个构建脚本,它调用了很多其他脚本,我想看看调用它们的顺序是什么,这样我就可以知道我必须去哪里并添加我的修改.
采纳答案by Eric Schoonover
I don't know of anyway to step through the execution of a .bat file but you can use echo
and pause
to help with debugging.
我不知道如何逐步执行 .bat 文件,但您可以使用echo
并pause
帮助调试。
ECHO
Will echo a message in the batch file. Such as ECHO Hello World will print Hello World on the screen when executed. However, without @ECHO OFF at the beginning of the batch file you'll also get "ECHO Hello World" and "Hello World." Finally, if you'd just like to create a blank line, type ECHO. adding the period at the end creates an empty line.PAUSE
Prompt the user to press any key to continue.
ECHO
将在批处理文件中回显消息。如 ECHO Hello World 在执行时会在屏幕上打印 Hello World。但是,如果批处理文件的开头没有 @ECHO OFF,您还将获得“ECHO Hello World”和“Hello World”。最后,如果您只想创建一个空行,请键入 ECHO。在末尾添加句点会创建一个空行。PAUSE
提示用户按任意键继续。
Source: Batch File Help
来源:批处理文件帮助
@workmad3: answer has more good tips for working with the echo
command.
@workmad3:回答有更多关于使用echo
命令的好技巧。
Another helpful resource... DDB: DOS Batch File Tips
另一个有用的资源... DDB:DOS 批处理文件提示
回答by workmad3
Make sure there are no 'echo off' statements in the scripts and call 'echo on' after calling each script to reset any you have missed.
确保脚本中没有“echo off”语句,并在调用每个脚本后调用“echo on”以重置您错过的任何脚本。
The reason is that if echo is left on, then the command interpreter will output each command (after parameter processing) before executing it. Makes it look really bad for using in production, but very useful for debugging purposes as you can see where output has gone wrong.
原因是如果 echo 保持开启,那么命令解释器将在执行之前输出每个命令(在参数处理之后)。使它在生产中使用看起来非常糟糕,但对于调试目的非常有用,因为您可以看到输出出错的地方。
Also, make sure you are checking the ErrorLevels set by the called batch scripts and programs. Remember that there are 2 different methods used in .bat files for this. If you called a program, the Error level is in %ERRORLEVEL%, while from batch files the error level is returned in the ErrorLevel variable and doesn't need %'s around it.
此外,请确保您正在检查由调用的批处理脚本和程序设置的 ErrorLevels。请记住,.bat 文件中为此使用了 2 种不同的方法。如果你调用了一个程序,错误级别在 %ERRORLEVEL% 中,而从批处理文件中,错误级别在 ErrorLevel 变量中返回,不需要 % 在它周围。
回答by Myobis
Facing similar concern, I found the following tool with a trivial Google search :
面对类似的担忧,我通过简单的 Google 搜索找到了以下工具:
JPSoft's "Take Command" includes a batch file IDE/debugger. Their short presentation videodemonstrates it nicely.
JPSoft 的“ Take Command”包括一个批处理文件 IDE/调试器。他们的简短演示视频很好地展示了这一点。
I'm using the trial version since a few hours. Here is my first humble opinion:
几个小时以来,我一直在使用试用版。这是我的第一个拙见:
- On one side, it indeed allows debugging .bat and .cmd scripts and I'm now convinced it can help in quite some cases
- On the other hand, it sometimes blocks and I had to kill it... specially when debugging subscripts (not always systematically).. it doesn't show a "call stack" nor a "step out" button.
- 一方面,它确实允许调试 .bat 和 .cmd 脚本,我现在确信它在某些情况下可以提供帮助
- 另一方面,它有时会阻塞,我不得不杀死它......特别是在调试下标时(并不总是系统地)......它不显示“调用堆栈”或“退出”按钮。
It deverves a try.
它可以尝试。
回答by Jokkke99BE
I found 'running steps' (win32) software doing exactly what I was looking for: http://www.steppingsoftware.com/
我发现“运行步骤”(win32)软件完全符合我的要求:http: //www.steppingsoftware.com/
You can load a bat file, place breakpoints / start stepping through it while seeing the output and environment variables.
您可以加载 bat 文件,放置断点/在查看输出和环境变量的同时开始单步执行。
The evaluation version only allows to step through 50 lines... Does anyone have a free alternative with similar functionality?
评估版只允许通过 50 行...有没有人有类似功能的免费替代品?
回答by Jokkke99BE
rem out the @ECHO OFF and call your batch file redirectin ALL output to a log file..
rem out @ECHO OFF 并调用您的批处理文件将所有输出重定向到日志文件..
c:> yourbatch.bat (optional parameters) > yourlogfile.txt 2>&1
c:> yourbatch.bat(可选参数)> yourlogfile.txt 2>&1
found at http://www.robvanderwoude.com/battech_debugging.php
在http://www.robvanderwoude.com/battech_debugging.php找到
IT WORKS!! don't forget the 2>&1...
有用!!不要忘记 2>&1...
WIZ
WIZ
回答by Biri
Did you try to reroute the result to a file? Like whatever.bat >log.txt
您是否尝试将结果重新路由到文件?就像whatever.bat>log.txt
You have to make sure that in this case every other called script is also logging to the file like >>log.txt
您必须确保在这种情况下,每个其他被调用的脚本也记录到文件中,例如 >>log.txt
Also if you put a date /T and time /T in the beginning and in the end of that batch file, you will get the times it was at that point and you can map your script running time and order.
此外,如果您在批处理文件的开头和结尾放置日期 /T 和时间 /T,您将获得当时的时间,您可以映射脚本运行时间和顺序。
回答by jop
The only way I can think of is spinkle the code with echo
s and pause
s.
我能想到的唯一方法是用echo
s 和pause
s来修饰代码。
回答by jop
Or.... Call your main .bat file from another .bat file and output the result to a result file i.e.
或者.... 从另一个 .bat 文件调用您的主 .bat 文件并将结果输出到结果文件,即
runner.bat > mainresults.txt
runner.bat > mainresults.txt
Where runner.bat calls the main .bat file
runner.bat 调用主 .bat 文件的地方
You should see all the actions performed in the main .bat file now
您现在应该会看到主 .bat 文件中执行的所有操作
回答by Pwn
or, open a cmd window, then call the batch from there, the output will be on the screen.
或者,打开一个 cmd 窗口,然后从那里调用批处理,输出将显示在屏幕上。