在Linux上使用Bash将所有输出重定向到文件? [英] Redirect all output to file using Bash on Linux?

查看:132
本文介绍了在Linux上使用Bash将所有输出重定向到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所有输出从命令行程序重定向到文件.我正在使用Bash.一些输出定向到文件,但是某些仍然出现在终端中,并且不存储到文件中.

I am trying to redirect all output from a command line programme to a file. I am using Bash. Some of the output is directed to a the file, but some still appears in the terminal and is not stored to the file.

类似的症状在这里描述:

Similar symptoms are described here:

将所有输出重定向到文件

但是我没有成功尝试过建议的解决方案(捕获stderr):

However I have tried the proposed solution (capture stderr) without success:

<cmd> <args> > stdout.txt 2> stderr.txt

文件stderr.txt已创建,但为空.

The file stderr.txt is created but is empty.

一个可能的线索是,命令行程序是与同一台计算机上的服务器进行通信的客户端.可能某些输出来自服务器.

A possible clue is that the command-line programme is a client communicating with a server on the same machine. It may be that some of the output is coming from the server.

是否有一种方法可以捕获来自终端的所有输出,而不论其起源如何?

Is there a way to capture all the output from the terminal, irrespective of its origin?

我已确认缺少的输出是由服务器生成的.在单独的终端上运行命令会在两个终端上产生一些输出,我可以将所有输出从命令终端传送到文件中.这引发了有关如何捕获服务器输出的问题,但这是一个不同的问题.

I've confirmed that the missing output is generated by the server. Running the command in a separate terminal causes some output in both terminals, I can pipe all the output from the command terminal to a file. This raises issues about how to capture the server output, but that's a different question.

推荐答案

如果服务器是在同一终端上启动的,则可能是服务器的stderr被写入终端并且您没有捕获它.

If the server is started on the same terminal, then it's the server's stderr that is presumably being written to the terminal and which you are not capturing.

捕获所有内容的最佳方法是运行:

The best way to capture everything would be to run:

script output.txt

在启动服务器或客户端之前.这将启动一个新的shell,将所有终端输出重定向到output.txt以及终端.然后从该新外壳中启动服务器,然后再启动客户端.您在屏幕上看到的所有内容(从该shell内写入终端的所有内容的输入和输出)都将被写入文件.

before starting up either the server or the client. This will launch a new shell with all terminal output redirected out output.txt as well as the terminal. Then start the server from within that new shell, and then the client. Everything that you see on the screen (both your input and the output of everything writing to the terminal from within that shell) will be written to the file.

完成后,键入"exit"退出由script命令运行的shell.

When you are done, type "exit" to exit the shell run by the script command.

这篇关于在Linux上使用Bash将所有输出重定向到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆