ls -l --color=auto | tee output.log
Run Code Online (Sandbox Code Playgroud)
没有管道/三通,它是彩色的。我怎样才能使它在使用时保持着色tee(只能在屏幕上着色,我不关心日志中的颜色)。
Eam*_*ain 121
只需unbuffer在任何命令之前插入,使其认为它正在写入交互式输出,即使它实际上是通过管道传输到另一个可执行文件中。在 的情况下,这将保留颜色ls。
例如
unbuffer ls -l --color=auto | tee output.log
Run Code Online (Sandbox Code Playgroud)
如果你还没有安装它,你可以在 Ubuntu 和其他 Debian-ish Linux 发行版上安装unbuffer它。
sudo apt-get install expect-dev
Run Code Online (Sandbox Code Playgroud)
Red*_*ick 15
使用 ls 选项 --color=always
--color=auto 不会彩色输出到管道 - 原因很明显。
主页上写着:
使用 --color=auto,仅当标准输出连接到终端 (tty) 时才会输出颜色代码。
Juu*_*nen 10
我将扩展script已接受答案的评论中给出的解决方案。script如果您不能或不想安装包含该命令的expect包,则使用可能会很有用unbuffer。
使用颜色代码ls将输出打印到标准输出和文件:
script -efq output.log -c "ls -l --color=auto"
Run Code Online (Sandbox Code Playgroud)
其中 ( man script):
Run Code Online (Sandbox Code Playgroud)-e, --return Return the exit code of the child process. Uses the same format as bash termination on signal termination exit code is 128+n. -f, --flush Flush output after each write. This is nice for telecooperation: one person does `mkfifo foo; script -f foo', and another can supervise real-time what is being done using `cat foo'. -q, --quiet Be quiet (do not write start and done messages to either standard output or the typescript file).
查看带有颜色的输出文件:
less -r output.log
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38041 次 |
| 最近记录: |