自动在 gedit 中打印终端输出?

use*_*413 4 command-line bash time-command

命令

time ./myprog 
Run Code Online (Sandbox Code Playgroud)

在屏幕上显示运行 myprog 所花费的时间。现在我想将此信息写入文本文件。

如何?

Kee*_*ook 6

没有路径time是 shell 内置的,它的输出不会被重定向。相反,您可以使用该time程序,在/usr/bin/time其中将写入标准错误:

/usr/bin/time -p ./myprog 2> timing.txt && gedit timing.txt
Run Code Online (Sandbox Code Playgroud)

我添加-p所以输出类似于time内置的输出。


Oli*_*Oli 5

编辑:我应该测试这个!

基斯说得很对。time在大多数情况下是内置的 shell,因此它的输出不会像其他程序通常那样通过 STDOUT。您可以使用命令版本,但为了保存两个相同的答案(或者我删除我的答案),这是另一种捕获不以传统方式输出的内容的方法。

bash -c "time ./myprog" 2>&1 > file.txt
Run Code Online (Sandbox Code Playgroud)

存在与以前相同的主体。如果您希望 gedit 在此之后加载,请打开&& gedit file.txt,如果您希望它附加,请更改>>>.

如果您的命令非常嘈杂并且您只想要时间,则可以通过tail以下方式运行输出:

bash -c "time ./myprog" 2>&1 | tail -3 > file.txt && gedit file.txt
Run Code Online (Sandbox Code Playgroud)

旧答案

要将其保存到文件中,只需执行以下操作:

time ./myprog > file.txt
Run Code Online (Sandbox Code Playgroud)

您不需要.txt扩展名,但它可能对您有所帮助。如果要附加到文件(而不是替换它),请交换>for >>

如果您希望它专门显示在 gedit 中,您需要将它保存到一个文件中,然后在 gedit 中打开它。如果不先保存文本,我就看不到将文本直接传送到 gedit 的方法。

time ./myprog > file.txt && gedit file.txt
Run Code Online (Sandbox Code Playgroud)