简体   繁体   English

Windows批处理脚本:将所有输出重定向到文件

[英]Windows Batch Script: Redirect ALL output to a file

I am running various Java benchmarks and would like to archive the results. 我正在运行各种Java基准测试,并希望存档结果。 I execute the (dacapo) benchmark like this: 我像这样执行(dacapo)基准测试:

C:\VM\jre\bin\java  -jar C:\benchmarks\dacapo-9.12-bach.jar %arg1% > %time::=%

I pass the type of benchmark in over a parameter, thats what %arg1% is. 我在一个参数中传递了基准类型,这就是%arg1%。

You can see that I am redirecting the output to a textfile. 您可以看到我将输出重定向到文本文件。 Unfortunately, the first and last line of the output is still printed in the console and not into the textfile: 不幸的是,输出的第一行和最后一行仍然在控制台中打印而不是打印到文本文件中:

===== DaCapo 9.12 luindex starting =====
===== DaCapo 9.12 luindex PASSED in 2000 msec =====

Especially the last line would be important to have in the text file :) 特别是最后一行在文本文件中很重要:)

Is there a trick to force this behavior? 是否有强制这种行为的技巧?

You must redirect STDOUT and STDERR . 您必须重定向STDOUTSTDERR

command > logfile 2>&1

STDIN is file descriptor #0, STDOUT is file descriptor #1 and STDERR is file descriptor #2. STDIN是文件描述符#0, STDOUT是文件描述符#1, STDERR是文件描述符#2。
Just as "command > file" redirects STDOUT to a file, you may also redirect arbitrary file descriptors to each other. 就像“command> file”将STDOUT重定向到文件一样,您也可以将任意文件描述符重定向到彼此。 The >& operator redirects between file descriptors. >&运算符在文件描述符之间重定向。 So, 2 >& 1 redirects all STDERR output to STDOUT . 因此, 2 >& 1将所有STDERR输出重定向到STDOUT

Furthermore, take care to add 2>&1 at the end of the instruction because on Windows, the order of redirection is important as command 2>&1 > logfile will produce an empty file, as Dawid added in the comments. 此外,请注意在指令末尾添加2>&1 ,因为在Windows上,重定向的顺序很重要,因为command 2>&1 > logfile将生成一个空文件,因为Dawid在注释中添加了。

Add 2>&1 to your command: 在命令中添加2>&1:

 C:\VM\jre\bin\java  -jar C:\benchmarks\dacapo-9.12-bach.jar %arg1% 2>&1 > %time::=% 

This will redirect STDERR to STDOUT which is then redirected into your textfile. 这会将STDERR重定向到STDOUT,然后将其重定向到您的文本文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM