bash 是否可以通过 shell 重定向捕获彩色输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3515208/
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
Can colorized output be captured via shell redirect?
提问by billc
Various bash commands I use -- fancy diffs, build scripts, etc, produce lots of color output.
我使用的各种 bash 命令——花哨的差异、构建脚本等,产生大量的颜色输出。
When I redirect this output to a file, and then cat
or less
the file later, the colorization is gone -- presumably b/c the act of redirecting the output stripped out the color codes that tell the terminal to change colors.
当我将此输出重定向到一个文件,然后cat
或less
稍后的文件时,着色消失了——大概是 b/c 重定向输出的行为剥离了告诉终端改变颜色的颜色代码。
Is there a way to capture colorized output, including the colorization?
有没有办法捕获彩色输出,包括着色?
采纳答案by ataylor
One way to capture colorized output is with the script
command. Running script
will start a bash session where all of the raw output is captured to a file (named typescript
by default).
捕获彩色输出的一种方法是使用script
命令。运行script
将启动一个 bash 会话,其中所有原始输出都被捕获到一个文件中(typescript
默认命名)。
回答by Walter Mundt
Redirecting doesn't strip colors, but many commands will detect when they are sending output to a terminal, and will not produce colors by default if not. For example, on Linux ls --color=auto
(which is aliased to plain ls
in a lot of places) will not produce color codes if outputting to a pipe or file, but ls --color
will. Many other tools have similar override flags to get them to save colorized output to a file, but it's all specific to the individual tool.
重定向不会去除颜色,但许多命令会检测它们何时将输出发送到终端,如果没有,默认情况下不会产生颜色。例如,在 Linux ls --color=auto
(ls
在很多地方别名为纯色)如果输出到管道或文件时不会产生颜色代码,但ls --color
会产生。许多其他工具都有类似的覆盖标志,可以让它们将彩色输出保存到文件中,但这都是特定于单个工具的。
Even once you have the color codes in a file, to see them you need to use a tool that leaves them intact. less
has a -r
flag to show file data in "raw" mode; this displays color codes. edit:Slightly newer versions also have a -R flag which is specifically aware of color codes and displays them properly, with better support for things like line wrapping/trimming than raw mode because less can tell which things are control codes and which are actually characters going to the screen.
即使您将颜色代码保存在文件中,要查看它们,您也需要使用使它们保持完整的工具。 less
有一个-r
标志以“原始”模式显示文件数据;这将显示颜色代码。 编辑:较新的版本也有一个 -R 标志,它专门识别颜色代码并正确显示它们,与原始模式相比,对换行/修剪等内容的支持更好,因为更少人可以分辨哪些是控制代码,哪些是实际字符走向屏幕。
回答by geekQ
Inspired by the other answers, I started using script
. I had to use -c
to get it working though. All other answers, including tee
, different script
examples did not work for me.
受到其他答案的启发,我开始使用script
. 我不得不使用-c
它来让它工作。所有其他答案,包括tee
不同的script
例子对我都不起作用。
Context:
语境:
- Ubuntu 16.04
- running behavior tests with
behave
and starting shell command during the test with python'ssubprocess.check_call()
- Ubuntu 16.04
behave
在使用 python 的测试期间使用和启动 shell 命令运行行为测试subprocess.check_call()
Solution:
解决方案:
script --flush --quiet --return /tmp/ansible-output.txt --command "my-ansible-command"
Explanation for the switches:
开关说明:
--flush
was needed, because otherwise the output is not well live-observable, coming in big chunks--quiet
supresses the own output of the script tool-c, --command
directly provides the command to execute, piping from my command to script did not work for me (no colors)--return
to make script propagate the exit code of my command so I know if my command has failed
--flush
是需要的,因为否则输出不能很好地实时观察,大块地出现--quiet
抑制脚本工具自己的输出-c, --command
直接提供要执行的命令,从我的命令到脚本的管道对我不起作用(没有颜色)--return
使脚本传播我的命令的退出代码,以便我知道我的命令是否失败
回答by alvherre
some programs remove colorization when they realize the output is not a TTY (i.e. when you redirect them into another program). You can tell some of those to use color forcefully, and tell the pager to turn on colorization, for example use less -R
当某些程序意识到输出不是 TTY 时(即,当您将它们重定向到另一个程序时),它们会删除着色。您可以告诉其中一些强制使用颜色,并告诉寻呼机打开着色,例如使用less -R
回答by Joels Elf
I found that using script
to preserve colors when piping to less
doesn't really work (less is all messed up and on exit, bash is all messed up) because less is interactive. script
seems to really mess up input coming from stdin
even after exiting.
我发现使用script
管道来保留颜色less
并没有真正起作用(less 全部搞砸了,退出时,bash 全部搞砸了),因为 less 是交互式的。即使在退出之后,script
似乎也真的搞砸了来自的输入stdin
。
So instead of running:
所以,而不是运行:
script -q /dev/null cargo build | less -R
I redirect /dev/null
to it before piping to less:
我/dev/null
在管道减少之前重定向到它:
script -q /dev/null cargo build < /dev/null | less -R
So now script
doesn't mess with stdin
and gets me exactly what I want. It's the equivalent of command | less
but it preserves colors while also continuing to read new content appended to the file (other methods I tried wouldn't do that).
所以现在script
不要乱来stdin
,让我得到我想要的。它相当于command | less
但它保留了颜色,同时还继续读取附加到文件的新内容(我尝试过的其他方法不会这样做)。
回答by Richard Wiseman
This questionover on superuser helped me when my other answer(involving tee
) didn't work. It involves using unbuffer
to make the command think it's running from a shell.
当我的其他答案(涉及tee
)不起作用时,这个关于超级用户的问题帮助了我。它涉及使用unbuffer
使命令认为它是从 shell 运行的。
I installed it using sudo apt install expect tcl
rather than sudo apt-get install expect-dev
.
我使用sudo apt install expect tcl
而不是sudo apt-get install expect-dev
.
I needed to use this method when redirecting the output of apt
, ironically.
apt
具有讽刺意味的是,在重定向 的输出时,我需要使用此方法。
回答by Richard Wiseman
I use tee
: pipe the command's output to tee
filename
and it'll keep the colour. And if you don't want to see the output on the screen (which is what tee
is for: showing and redirecting output at the same time) then just send the output of tee
to /dev/null
:
我使用tee
: 将命令的输出通过管道传输到tee
filename
它,它会保持颜色。如果您不想在屏幕上看到输出(这是tee
为了:同时显示和重定向输出),那么只需将输出发送tee
到/dev/null
:
command
| tee
filename
> /dev/null
command
| tee
filename
> /dev/null