如何在bash shell中自动为程序输出着色? [英] How do you automatically colorize program outputs in a bash shell?
问题描述
我想使用任何输出到屏幕的程序,捕获输出,并在将某些关键字输出到屏幕之前为其着色.例如,这是正常程序的输出:
I want to take any program that outputs to the screen, catch the output, and colorize certain keywords before they are output to the screen. For example, here's the normal program output:
bash# <program>
blah blah blah <-- this output has no color
vs.
bash# <program>
blah blah blah <-- this output is colorful
理想情况下,程序是什么都没有关系.我只是在寻找一种将更多颜色融合到我的Konsole中的好方法.
Ideally it doesn't matter what the program is. I'm just looking for a good way to incorporate more color into my konsoles.
对不起,应该已经清楚了.我不只是在给shell脚本输出着色.
Sorry, should've been clear. I'm not trying to just colorize shell script outputs.
推荐答案
#!/bin/sh
redf=$(tput setaf 1)
redb=$(tput setab 1)
reset=$(tput op)
echo "${redf}red${reset} in front, ${redb}red${reset} in back"
有关终端功能的详细列表,请参见 terminfo .后缀为 -m
的 $ TERM
(例如 ansi-m
)表示屏幕为单色,但是只要颜色有效,以下字符串功能不能为空:
See terminfo for a long listing of terminal capabilities. A $TERM
with suffix -m
(e.g. ansi-m
) means the screen is monochrome, but as long as color works, the following string capabilities should be non-empty:
enter_bold_mode bold md turn on bold (extra
bright) mode
enter_italics_mode sitm ZH Enter italic mode
enter_reverse_mode rev mr turn on reverse
video mode
orig_pair op op Set default pair to
its original value
set_a_background setab AB Set background
color to #1, using
ANSI escape
set_a_foreground setaf AF Set foreground
color to #1, using
ANSI escape
颜色0-7几乎是标准颜色:黑色,红色,绿色,黄色,蓝色,洋红色,青色,白色.除此之外,可能不存在,或者可能更多.
Colors 0-7 are pretty much standard: black, red, green, yellow, blue, magenta, cyan, white. Beyond that may not exist or may be more variable.
这篇关于如何在bash shell中自动为程序输出着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!