向前猛击颜色

发布于 2024-12-03 06:15:41 字数 469 浏览 7 评论 0原文

有没有办法导出命令输出的颜色?

我们用一个小例子来解释一下:

ls -alh --color=auto

会打印目录的彩色内容,而

ls -alh --color=auto | cat

不会打印某些颜色。我想知道的是一个技巧或工具,我们称之为magic,它可以恢复这些命令,例如\033[1m,以便颜色可用于后续处理:

ls -alh --color=auto | magic | cat

ls -alh --color=auto | magic >> file

更新:
我仅在本示例中使用 ls ,但想知道是否存在一般可能性。

Is there any way to export the colors of an output of a command?

Let's explain it with a small example:

ls -alh --color=auto

will print the colored contents of the directory, while

ls -alh --color=auto | cat

won't print some color. What I want to know is a trick or a tool, let's call it magic, that restores these commands like \033[1m, so that colors are available for latter processing:

ls -alh --color=auto | magic | cat

or

ls -alh --color=auto | magic >> file

Update:
I'm using ls just for this example, but want to know whether there is a general possibility.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心未许 2024-12-10 06:15:41
script outputfile command 

会成功的。例如:

script capture.txt ls --color=always
script outputfile command 

will do the trick. e.g:

script capture.txt ls --color=always
战皆罪 2024-12-10 06:15:41

由于颜色代码实际上是 ls 输出的一部分,因此无法“恢复”它们(因为它们一开始就不存在)。

但如果您使用 ls --color=always,即使在非交互模式下使用,ls 也会输出颜色代码。

Since the color codes are actually part of ls output, there is no way to "restore" them (since they are not there in the first place).

But if you use ls --color=always, ls will output color codes even when used in non-interactive mode.

永言不败 2024-12-10 06:15:41

基本上,ls 很智能,可以检测输出何时不发送到终端。如果您想让它不那么重要,请尝试ls --color=always

默认情况下和 --color=never 都禁用使用颜色来区分文件类型。使用 --color=auto 时,ls 仅当标准输出连接到终端时才发出颜色代码。 LS_COLORS 环境变量可以更改设置。使用dircolors命令进行设置。

Basically, ls is being smart, and detecting when the output isn't going to a terminal. If you want to tell it to be less so, try ls --color=always.

Using color to distinguish file types is disabled both by default and with --color=never. With --color=auto, ls emits color codes only when standard output is connected to a terminal. The LS_COLORS environment variable can change the settings. Use the dircolors command to set it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文