有没有办法在Windows中的脚本持续时间内将stderror重定向到stdout?
作为一名长期 UNIX shell 脚本编写者/发布工程师,我最近的工作全职进入 Windows 环境,有些事情我不可避免地会怀念。
其中之一是我值得信赖的旧“exec 2>&1”,它在 Bourne shell 中在脚本运行期间永久地将 stderr 和 stdout 结合在一起,以便日志记录将捕获所有好的和坏的东西。
我意识到您可以将 2>foo.log 附加到每个命令行调用的末尾,但我想知道是否有更好的方法。
Windows环境下有没有类似的东西? 我们在这里使用 .BAT 文件脚本和 Perl 脚本(主要用于构建自动化,但也用于其他一些事情),我非常想知道这是否可行。
As a long time UNIX shell scripter / release engineer coming into the Windows environment full time with my latest job, there are some things I inevitably miss.
One of them is my trusty old 'exec 2>&1' which in the Bourne shell permently knots together stderr and stdout for the duration of the script, so that logging will capture everything good and bad.
I realize you can append 2>foo.log to the end of every command line invocation, but I'm wondering if there's a better way.
Is there any equivalent in the Windows environment? We're using both .BAT file scripts and Perl scripts here (Mostly for build automation but also some other things) and I'd dearly love to know if this is possible at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Perl 中,您可以
将脚本的 STDERR 输出重定向到 STDOUT。 有关详细信息,请参阅
open
文档。 我不希望这会影响系统调用的输出,即system
或反引号。In Perl you can do
to redirect the STDERR output of the script to STDOUT. See the
open
documentation for more info. I wouldn't expect this to effect the output of system calls, i.e.system
or backticks.您可以将实际工作的 .BAT 嵌套在重定向流以进行日志记录的 .BAT 中。
像这样的东西
可能会起作用。 您也许可以使用 CALL 而不是直接调用第二个 CMD.EXE,我没有检查过。 如果这确实有效,那么解决方案可以是自包含的:只需使用额外的参数来调用自己,以指示日志记录有效并且流已重定向。
You can nest the .BAT that does the real work inside a .BAT that redirects the streams for logging.
Something like
might do the trick. You might be able to use CALL instead of directly invoking a second CMD.EXE, I haven't checked. If that does work, then the solution can be self contained: just call yourself with an extra argument to indicate the logging is in effect and with the streams redirected.
您可以将重定向应用于命令块以及单个命令,
但请注意,无法在命令块内部使用标签和 goto。
You can apply redirection to command blocks as well as single commands
Note that it is impossible to use labels and goto inside of command blocks, though.
请阅读 MS 的这篇知识库文章。 样本取自上述文章:
Read this KB article from MS. Sample taken from the said article:
2>&1
的事情似乎对我的脚本有用:你到底在做什么以及当你这样做时你期待什么?
The
2>&1
thing seems to be working for me for scripts:What exactly are you doing and what are you expecting when you're doing it?
并且,如果您的批处理脚本具有命令行参数:
And, if your batch script has command line parameters: