首页 > 解决方案 > Windows 命令提示符创建但不将输出重定向到文件

问题描述

我在这里看到的很多帖子都遇到了相反的问题。

我正在运行由其他人编写的 perl 命令,尽管使用了“>”命令,但输出都被强制显示在屏幕上。

Windows 清楚地知道我的意图,因为每次执行命令时都会创建全新的文件名,但日志文件的内容/大小为空且长度为 0 字节。

我的 perl 可执行文件与我的 perl 例程/.pl 文件位于不同的位置。

我尝试以管理员身份运行,而不是。

这不是程序有问题。我的一些同事执行得很好,他们的屏幕没有输出。

一般语法是:

F:\git\repoFolderStructure\bin>
F:\git\repoFolderStructure\bin>perl alog.pl param1 param2 commaSeparatedParam3 2020-12-17,18:32:33 2020-12-17,18:33:33 > mylogfile.log

>>>>>Lots and lots of output I wish was in a file

Also attempted in the directory with my perl.exe and gave the path to my repo folder's bin.

窗口是否有一些奇怪的东西可以创建/阻止 > 运算符的行为?

这是踢球者:我做得ipconfig > out.txt很好,虽然......没有写到屏幕上。

感谢您提供有关我可以做些什么来尝试改变行为的任何提示!

标签: perlcmdcommand-lineioio-redirection

解决方案


可能是在您捕获 STDOUT 时将输出发送到 STDERR。追加2>&1以将两者捕获到同一文件中。

>perl -e"print qq{STDOUT\n}; warn qq{STDERR\n};" >stdout
STDERR

>type stdout
STDOUT

>perl -e"print qq{STDOUT\n}; warn qq{STDERR\n};" 2>stderr
STDOUT

>type stderr
STDERR

>perl -e"print qq{STDOUT\n}; warn qq{STDERR\n};" >stdout 2>stderr

>type stdout
STDOUT

>type stderr
STDERR

>perl -e"print qq{STDOUT\n}; warn qq{STDERR\n};" >both 2>&1

>type both
STDERR
STDOUT

请注意,2>&1如果要合并两个流,则必须在重定向 STDOUT 之后进行。


推荐阅读