bash 如何将标准输出写入带有颜色的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27397865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write stdout to file with colors?
提问by AAlvz
A lot of times (not always) the stdout is displayed in colors. Normally I keep every output log in a different file too. Naturally in the file, the colors are not displayed anymore.
很多时候(并非总是)标准输出以颜色显示。通常我也将每个输出日志保存在不同的文件中。自然在文件中,颜色不再显示。
I'd like to know if there's a way (in Linux) to write the output to a file with colors. I'm trying to use tee
to write the output of vagrant to a file, this way I can still see the output (when it applies). I want to use it specifically for vagrant (it may change in the future, of course...)
我想知道是否有办法(在 Linux 中)将输出写入带有颜色的文件。我正在尝试将tee
vagrant 的输出写入文件,这样我仍然可以看到输出(适用时)。我想专门用于vagrant(当然以后可能会改变……)
回答by Wintermute
Since many programs will only output color sequences if their stdout is a terminal, a general solution to this problem requires tricking them into believing that the pipe they write to is a terminal. This is possible with the script
command from bsdutils:
由于许多程序仅在其 stdout 是终端时才输出颜色序列,因此该问题的一般解决方案需要诱使它们相信它们写入的管道是终端。这可以通过script
bsdutils的命令实现:
script -q -c "vagrant up" filename.txt
This will write the output from vagrant up
to filename.txt (and the terminal). If echoing is not desirable,
这会将输出写入vagrant up
filename.txt(和终端)。如果不希望回声,
script -q -c "vagrant up" filename > /dev/null
will write it only to the file.
只会将其写入文件。
回答by choroba
You can save the ANSI sequences that colourise your output to a file:
您可以将输出着色的 ANSI 序列保存到文件中:
echo a | grep --color=always . > colour.txt
cat colour.txt
Some programs, though, tend not touse them if their output doesn't go to the terminal (that's why I had to use --color-always
with grep
).
但是,如果某些程序的输出没有到达终端,则往往不会使用它们(这就是我必须使用--color-always
with的原因grep
)。
回答by repzero
You can also color your output with echo with different colours and save the coloured output in file. Example
您还可以使用不同颜色的 echo 为您的输出着色,并将彩色输出保存在文件中。例子
echo -e '\E[37;44m'"Hello World" > my_file
Also You would have to be acquainted with the terminal colour codes
您还必须熟悉终端颜色代码
Using tee
使用三通
< command line > |tee -a 'my_colour_file'
Open your file in cat
在 cat 中打开您的文件
cat 'my_colour_file'
Using a named pipe can also work to redirect all output from the pipe with colors to another file
使用命名管道还可以将带有颜色的管道的所有输出重定向到另一个文件
for example
例如
Create a named pipe
创建命名管道
mkfifo pipe.fifo
each command line redirect it to the pipe as follows
每个命令行将其重定向到管道,如下所示
<command line> > pipe.fifo
In another terminal redirect all messages from the pipe to your file
在另一个终端中,将所有消息从管道重定向到您的文件
cat pipe.fifo > 'my_log_file_with_colours'
open your file with cat and see the expected results.
用 cat 打开你的文件,看看预期的结果。
回答by Pini Cheyni
I found out that using the tool called ansi2html.sh
我发现使用名为ansi2html.sh的工具
Is the most simple way to export colorful terminal data to html file,
是最简单的将多彩终端数据导出到html文件的方法,
The commands to use it are:
使用它的命令是:
ls --color=always | ansi2html.sh --palette=solarized > ~/Desktop/ls.html
- All is needed is to send the output using a pipe and then output the stdout to simple html file
- 所需要的只是使用管道发送输出,然后将标准输出输出到简单的 html 文件
回答by Ivan Ogai
In Ubuntu, you can install the package bsdutils
to output to a text file with ANSI color codes:
在 Ubuntu 中,您可以安装包bsdutils
以输出到带有 ANSI 颜色代码的文本文件:
script -q -c "ls --color=always" /tmp/t
Install kbtin
to generate a clean HTMLfile:
安装kbtin
以生成干净的HTML文件:
ls --color=always | ansi2html > /tmp/t.html
Install aha
and wkhtmltopdf
to generate a nice PDF:
安装aha
并wkhtmltopdf
生成一个不错的PDF:
ls --color=always | aha | wkhtmltopdf - /tmp/t.pdf
Use any of the above with tee
to display the output also on the console or to save a copy in another file. Example:
使用以上任何一种方式tee
也可以在控制台上显示输出或将副本保存在另一个文件中。例子:
ls --color=always | tee /dev/stderr | aha | wkhtmltopdf - /tmp/test.pdf
回答by DerekDick
Solution
解决方案
$ script -q /dev/null -c "your command" > log.txt
$ cat log.txt
Explanation
解释
According to the man pageof script
, the --quit
option only makes sure to be quiet (do not write start and done messages to standard output)
. Which means that the start and done messages will alwaysbe written to the file.
根据该男子页的script
,该--quit
选项仅会确保be quiet (do not write start and done messages to standard output)
。这意味着开始和完成消息将始终写入文件。
In order to utilize script and discard the output file at the same file, we can simply specify the null device /dev/null
to it! Also, redirect the output to our desired destination and the color content will be written to the destination.
为了利用脚本并在同一个文件中丢弃输出文件,我们可以简单地/dev/null
为其指定空设备!此外,将输出重定向到我们想要的目的地,颜色内容将被写入目的地。