是否可以在XTerm或Konsole中使stdout和stderr输出具有不同的颜色? [英] Is it possible to make stdout and stderr output be of different colors in XTerm or Konsole?

查看:116
本文介绍了是否可以在XTerm或Konsole中使stdout和stderr输出具有不同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它甚至可以实现吗?

我希望命令stderr的输出以不同于stdout的颜色呈现(例如,红色)。

I would like the output from a command’s stderr to be rendered in a different color than stdout (for example, in red).

我需要进行这样的修改才能在Linux上的Konsole,XTerm或GNOME Terminal终端仿真器中使用Bash shell。

I need such a modification to work with the Bash shell in the Konsole, XTerm, or GNOME Terminal terminal emulators on Linux.

推荐答案

这是一个结合了已经提出的一些好主意的解决方案。

Here's a solution that combines some of the good ideas already presented.

在bash脚本中创建一个函数:

Create a function in a bash script:

color() ( set -o pipefail; "$@" 2>&1>&3 | sed $'s,.*,\e[31m&\e[m,' >&2 ) 3>&1

$ color command -program -args

它将以红色显示命令的 stderr

It will show the command's stderr in red.

请继续阅读以了解其工作原理。此命令演示了一些有趣的功能。

Keep reading for an explanation of how it works. There are some interesting features demonstrated by this command.


  • color()... —创建一个bash函数,称为color。

  • set -o pipefail —这是一个shell选项,可保留其输出的错误返回代码被传送到另一个命令。这是在由括号创建的子外壳中完成的,以免更改外壳中的pipefail选项。

  • $ @ —将函数的参数作为新命令执行。 $ @ 等同于 $ 1 $ 2 ...

  • 2>& 1 —重定向 stderr 命令到 stdout ,使其成为 sed stdin

  • & 3 1>&的简写; 3 ,这会将 stdout 重定向到新的临时文件描述符 3 3 之后又路由回 stdout

  • sed ... —由于上面的重定向, sed stdin 为已执行命令的 stderr 。它的功能是用颜色代码包围每行。

  • $'...'一种bash构造,可使其理解反斜杠。 -转义字符

  • 。* —匹配整行。

  • \e [31m —使以下字符变为红色的ANSI转义序列

  • & sed 替换字符,扩展为整个匹配的字符串(在本例中为整行)。

  • \e [m —重设颜色的ANSI转义序列。

  • & 2 1>& 2 的简写,这将重定向 sed stdout stderr

  • 3& 1 —将临时文件描述符 3 重定向回 stdout

  • color()... — Creates a bash function called color.
  • set -o pipefail — This is a shell option that preserves the error return code of a command whose output is piped into another command. This is done in a subshell, which is created by the parentheses, so as not to change the pipefail option in the outer shell.
  • "$@" — Executes the arguments to the function as a new command. "$@" is equivalent to "$1" "$2" ...
  • 2>&1 — Redirects the stderr of the command to stdout so that it becomes sed's stdin.
  • >&3 — Shorthand for 1>&3, this redirects stdout to a new temporary file descriptor 3. 3 gets routed back into stdout later.
  • sed ... — Because of the redirects above, sed's stdin is the stderr of the executed command. Its function is to surround each line with color codes.
  • $'...' A bash construct that causes it to understand backslash-escaped characters
  • .* — Matches the entire line.
  • \e[31m — The ANSI escape sequence that causes the following characters to be red
  • & — The sed replace character that expands to the entire matched string (the entire line in this case).
  • \e[m — The ANSI escape sequence that resets the color.
  • >&2 — Shorthand for 1>&2, this redirects sed's stdout to stderr.
  • 3>&1 — Redirects the temporary file descriptor 3 back into stdout.

这里是同一概念的扩展,也使STDOUT变为绿色:

Here's an extension of the same concept that also makes STDOUT green:

function stdred() (
    set -o pipefail;
    (
        "$@" 2>&1>&3 |
        sed $'s,.*,\e[31m&\e[m,' >&2
    ) 3>&1 |
    sed $'s,.*,\e[32m&\e[m,'
)

这篇关于是否可以在XTerm或Konsole中使stdout和stderr输出具有不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆