batch script print the command that would be executed rather than executing

你说的曾经没有我的故事 提交于 2020-01-05 08:51:37

问题


Is it possible to set a cmd.exe shell / batch file to print what would be executed but not actually execute it?

For example, given a batch file that takes some arguments, based on those arguments selects some other batch files to run, those batch files execute some commands, may or may not call other files/commands etc.

I would like to be able to run the top level batch file with all possible combinations of it's input arguments and capture what each arg combination would execute - without actually trying to execute it.

e.g. conceptually would want to be able to produce something like:

mybatchfile.bat 1 2 3 > mybatchfile_1_2_3.bat

mybatchfile.bat 99 3 42 > mybatchfile_99_3_42.bat

where mybatchfile_99_3_42.bat is the list of everything that WOULD be executed when running mybatchfile.bat 99 3 42 (NOT the output of executing those commands)

If this can't be done solely using cmd.exe is there someway to achieve this by running the batch script in cygwin bash shell


回答1:


In bash we would use something like -x to print out all possible commands without executing them. how to make bash scripts print out every command before executing The problem is that to my knowledge there's no exact equivalent command for Batch Scripts. I would suggest you try placing:

@echo on

at the beginning of your script and:

@echo off

at the end of your script, that's the best starting place.




回答2:


If you never want the batch file to actually execute the commands, you can insert echo before each command. It's not a perfect solution by any means, but it may be a work-around for fairly simple scripts.



来源:https://stackoverflow.com/questions/10137404/batch-script-print-the-command-that-would-be-executed-rather-than-executing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!