我不知道wintee 命令,但我想我可以帮你解决重定向问题:
您应该写以下内容,而不是 test_name.bat [parameters] 2>&1 | wtee log.txt:
(test_name.bat [parameters] 2>&1 1> con) | wtee log.txt
这会将 STDOUT 处的文本写入控制台,将 STDERR 处的数据重定向到 STDOUT,然后将其传递给 @ 987654326@命令。
请注意,控制台会在任何 STDERR 数据之前显示所有原始 STDOUT,因为前者是立即显示的,而后者是通过 wtee 传递的。使用纯重定向黑客,不可能保留返回数据的原始顺序。 如果您坚持这样做,则需要使用具有所需功能的wintee 以外的工具。 编辑: 特别是,管道是瓶颈,因为只有一个通道,即 STDIN,它将数据传递到该通道。所以如果你坚持STDOUT和SRDERR数据按原顺序显示,同时将一个流的数据保存到文件,你别无选择,只能修改脚本test_name.bat 并避免管道。
我试图解释使用管道左侧的命令dir ":",它会在 STDOUT 和 STDERR 处产生输出(因为无效路径":"):
D:\Data> dir ":"
Volume in drive D has no label.
Volume Seriel Number is 0000-0000
Directory of D:\Data
File Not Found
File Not Found 消息出现在 STDERR,而其余消息出现在 STDOUT(您可以通过像 2> nul 或 1> nul 这样的重定向来证明流)。
在管道的右侧,我正在使用命令find /V "",它只是将它在STDIN 接收到的所有数据传递并显示在控制台上:
D:\Data> dir ":" | find /V ""
File Not Found
Volume in drive D has no label.
Volume Seriel Number is 0000-0000
Directory of D:\Data
控制台输出的更改顺序说明了发生了什么:STDERR 立即显示,而 STDOUT 在显示之前先通过管道。
现在让我们从您的命令行应用重定向2>&1:
D:\Data> (dir ":" 2>&1) | find /V ""
Volume in drive D has no label.
Volume Seriel Number is 0000-0000
Directory of D:\Data
File Not Found
这会将 STDERR 重定向到 STDOUT,因此原始 STDOUT 数据与重定向的数据一起通过管道传输。将find /V "" 替换为(> nul find /V "") 证明管道的右侧确实接收到了所有数据。
现在让我们添加 1> con 部分,它构成了 STDOUT 到控制台的显式重定向:
D:\Data> (dir ":" 2>&1 1> con) | find /V ""
Volume in drive D has no label.
Volume Seriel Number is 0000-0000
Directory of D:\Data
File Not Found
输出包含所有原始数据。再次将find /V "" 替换为(> nul find /V "") 证明这一次,管道的右侧确实只收到File Not Found 消息,该消息最初出现在STDERR,但STDOUT 数据未通过管道传输。
只是一个旁注:
如果你想用纯batch-file 做类似wintee 的事情,事情就会变得非常复杂——参见this example...