如何在 Windows 命令行中显示输出文本,同时重定向文件中的输出文本?

发布于 2024-12-03 11:44:30 字数 229 浏览 1 评论 0 原文

目前我有一个 make 文件,它将使用 gnumake 构建我的软件,并且当前我在文件中重定向我的输出文本(如构建日志、警告、错误)。但现在我认为在构建和重定向文件中的输出文本时显示所有输出文本非常有帮助。以下是我当前的命令,

gnumake -f Build.mak  1>Logs.txt 2>>&1

是否可以在重定向文件中的输出文本时显示输出文本?

Currently I have a make file that will build my software using gnumake and currently I redirect my output text (like build logs, warnings, errors) in a file. But now I think it's very helpful to also display all the output text while building and while redirecting the output text in a file. Below is my current command,

gnumake -f Build.mak  1>Logs.txt 2>>&1

Is it possible to display the output text while redirecting the output text in a file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫九 2024-12-10 11:44:30

我知道这是一篇旧帖子,但从未得到正确的回答。
这是给其他未来观众的一个答案。
您不需要 tee 就能够记录文件并显示文本。还有另一种方法。

这个问题在 StackOverflow 上得到了解答。这里有 2 个链接:

如果您不想使用 tee,请检查:当在命令行中添加 TextLog 到 Echo 时,输入不会显示

对于 tee 你可以看到这个: 显示 Windows 命令提示符输出和将其重定向到文件

只是将答案放在这里。

在 dos 中,您可以在不使用 tee 的情况下使用它。

set LogFile=path\logfile.txt
set TempLog=path\temp
set logg=^> %TempLog%^&^& type %TempLog%^&^&type %TempLog%^>^>%LogFile%

如果您需要创建目录或/和 logfile.txt,请执行此操作。

if not exist "path" mkdir "path" >>nul
echo. 2>  %LogFile%  >nul
echo. 2>  %TempLog%  >nul

现在只需使用 echo 即可,如下所示:

echo This will show me a text and logg  %logg%

I know this is a old post, but was never answered correctly.
Here is one answer for other future viewers.
You don't need tee to be able to log a file and show the text. There is another way.

This question was answered on StackOverflow. Here are the 2 links:

If you don't want to use tee check this: When adding a TextLog to Echo in Command Line, Input wont show up

For tee you can see this: Displaying Windows command prompt output and redirecting it to a file

Just going to throw the answer here.

In dos yo can use this without using tee.

set LogFile=path\logfile.txt
set TempLog=path\temp
set logg=^> %TempLog%^&^& type %TempLog%^&^&type %TempLog%^>^>%LogFile%

If you need to create directory or/and logfile.txt do this.

if not exist "path" mkdir "path" >>nul
echo. 2>  %LogFile%  >nul
echo. 2>  %TempLog%  >nul

Now just use echo like this:

echo This will show me a text and logg  %logg%
蓝海 2024-12-10 11:44:30

如果你安装了cygwin(ms windows上的unix工具),你可以使用“tee”命令:
gnumake -f Build.mak | gnumake -f Build.mak | tee Logs.txt

(这会将输出保存到Logs.txt,同时将输出显示到控制台)。

if you install cygwin (unix tools on ms windows), you can use the "tee" command:
gnumake -f Build.mak | tee Logs.txt

(this will save the output to Logs.txt and at the same time show the output to the console).

甜警司 2024-12-10 11:44:30

看看这个 SOq:

基本上,您可以使用 tee 命令,例如从这里:

Take a look at this SOq:

Basically, you can use tee command e.g. from here:

草莓酥 2024-12-10 11:44:30

Avrumi Sherman 的增强版本,具有处理 STDERR STDOUT

D:\>dir ERROR 1>log.txt 2>&1 & type log.txt
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\Projekte\UDG_TMP\build-deploy

 Datei nicht gefunden

因此它会在屏幕上显示 STDTOUT(第一行) STDERR(最后一行) 日志文件。

对于批处理文件中的使用,只需像 Avrumi Sherman 那样操作,但使用单个 & 以便捕获 ERROR 情况:

D:\>set logFile=log.txt
D:\>set tmpLog=tmp.txt
D:\>set logCmd=1^>%tmpLog% 2^>^&1 ^& type %tmpLog% ^& type %tmpLog%^>^>%logFile%

现在您可以像 logCmd 一样使用 logCmd 变量;)

请注意,第一个 dir 和第二个 rmdir 的错误消息显示在屏幕上以及日志文件中:

D:\>echo START %logCmd%
START

D:\>dir DIRECTORY %logCmd%
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\

Datei nicht gefunden

D:\>mkdir DIRECTORY %logCmd%

D:\>dir DIRECTORY %logCmd%
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\DIRECTORY

26.08.2020  17:50    <DIR>          .
26.08.2020  17:50    <DIR>          ..
               0 Datei(en),              0 Bytes
               2 Verzeichnis(se), 68.391.989.248 Bytes frei

D:\>rmdir DIRECTORY %logCmd%

D:\>rmdir DIRECTORY %logCmd%
Das System kann die angegebene Datei nicht finden.

D:\>echo STOP %logCmd%
STOP

在批处理文件的末尾,您可以可以/应该删除%tmpLog%。这里是使用上面示例的 %logFile% 的内容:

START
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\

Datei nicht gefunden
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\DIRECTORY

26.08.2020  17:50    <DIR>          .
26.08.2020  17:50    <DIR>          ..
               0 Datei(en),              0 Bytes
               2 Verzeichnis(se), 68.391.989.248 Bytes frei
Das System kann die angegebene Datei nicht finden.
STOP

Enhanced version of Avrumi Sherman with handling of STDERR and STDOUT:

D:\>dir ERROR 1>log.txt 2>&1 & type log.txt
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\Projekte\UDG_TMP\build-deploy

 Datei nicht gefunden

So it displays the STDTOUT (first lines) and the STDERR (last line) on screen and the log-file.

For usage in a batch file just do it like Avrumi Sherman did but use a single & in order to catch the ERROR-cases as well:

D:\>set logFile=log.txt
D:\>set tmpLog=tmp.txt
D:\>set logCmd=1^>%tmpLog% 2^>^&1 ^& type %tmpLog% ^& type %tmpLog%^>^>%logFile%

Now you can use the logCmd variable like a logCmd ;)

Note the ERROR messages of the first dir and the second rmdir are displayed on screen as well as appearing in the log-file:

D:\>echo START %logCmd%
START

D:\>dir DIRECTORY %logCmd%
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\

Datei nicht gefunden

D:\>mkdir DIRECTORY %logCmd%

D:\>dir DIRECTORY %logCmd%
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\DIRECTORY

26.08.2020  17:50    <DIR>          .
26.08.2020  17:50    <DIR>          ..
               0 Datei(en),              0 Bytes
               2 Verzeichnis(se), 68.391.989.248 Bytes frei

D:\>rmdir DIRECTORY %logCmd%

D:\>rmdir DIRECTORY %logCmd%
Das System kann die angegebene Datei nicht finden.

D:\>echo STOP %logCmd%
STOP

At the end of your batch file you can/should delete %tmpLog%. Here the content of %logFile% using the example above:

START
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\

Datei nicht gefunden
 Datenträger in Laufwerk D: ist DATA
 Volumeseriennummer: 4653-A096

 Verzeichnis von D:\DIRECTORY

26.08.2020  17:50    <DIR>          .
26.08.2020  17:50    <DIR>          ..
               0 Datei(en),              0 Bytes
               2 Verzeichnis(se), 68.391.989.248 Bytes frei
Das System kann die angegebene Datei nicht finden.
STOP
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文