windows 如何在子 shell 中运行 cmd.exe 批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4215405/
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 to run a cmd.exe batch file in a sub shell
提问by Martin
I have a batch file that I usually invoke like this:
我有一个批处理文件,我通常像这样调用它:
longjob.cmd >result.txt 2>&1
This works fine, but the script changes directory during its execution leaving my shell in that directory - which is a nuisance.
这工作正常,但脚本在执行过程中更改了目录,将我的 shell 留在该目录中 - 这很麻烦。
Is there a way to run the command within a sub-shell - while still allowing the output to be captured ?
有没有办法在子 shell 中运行命令 - 同时仍然允许捕获输出?
I have tried
我试过了
cmd longjob.cmd >result.txt 2>&1
which just sits waiting for an exit command.
它只是等待退出命令。
Also I tried
我也试过
start longjob.cmd >result.txt 2>&1
which does run the script, but in a new window and all output is sent to that window instead of the file.
它确实运行脚本,但在一个新窗口中,所有输出都发送到该窗口而不是文件。
回答by Leo Davidson
Try
尝试
CMD /C longjob.cmd >result.txt 2>&1
Not sure how it'll deal with the redirection, but CMD /C lets you tell CMD what to run and that it should exit when done. (CMD /K lets you tell it to run something but stick around when done.) It will re-use the existing console window if run within one.
不确定它将如何处理重定向,但是 CMD /C 可以让您告诉 CMD 要运行什么以及它应该在完成后退出。(CMD /K 可以让你告诉它运行一些东西,但在完成后坚持下去。)如果在其中运行,它将重新使用现有的控制台窗口。