简体   繁体   English

如何启用包含在脚本中的 shell 命令的彩色输出?

[英]How to enable colored output from shell command wrapped in a script?

Basically, say I have a command like grep which outputs color-coded results with a flag like -r .基本上,假设我有一个像grep这样的命令,它输出带有-r标志的颜色编码结果。 If I were to wrap this command within a shell script, upon running the script, the output won't have the same color-coded effect.如果我将此命令包装在 shell 脚本中,则在运行脚本时,输出将不会具有相同的颜色编码效果。

Is there any other way of making the shell script spit out results (on terminal) in the same color-coding without resorting to using tput or manual color coding?是否有任何其他方法可以使 shell 脚本以相同的颜色编码(在终端上)吐出结果,而无需使用tput或手动颜色编码?

To force color output from grep :强制从grep输出颜色:

grep --color=always

From man grep :man grep

   --color[=WHEN], --colour[=WHEN]
          Surround   the   matched   (non-empty)   strings,
          matching lines, context lines, file  names,  line
          numbers, byte offsets, and separators (for fields
          and  groups  of  context   lines)   with   escape
          sequences   to  display  them  in  color  on  the
          terminal.   The  colors  are   defined   by   the
          environment variable GREP_COLORS.  The deprecated
          environment   variable   GREP_COLOR   is    still
          supported,   but   its   setting  does  not  have
          priority.  WHEN is never, always, or auto.

just use Bash functions, like, say this one:只需使用 Bash 函数,比如,说这个:

make()
{
  pathpat="(/[^/]*)+:[0-9]+"
  ccred=$(echo -e "\033[0;31m")
  ccyellow=$(echo -e "\033[0;33m")
  ccend=$(echo -e "\033[0m")
  /usr/bin/make "$@" 2>&1 | sed -E -e "/[Ee]rror[: ]/ s%$pathpat%$ccred&$ccend%g" -e "/[Ww]arning[: ]/ s%$pathpat%$ccyellow&$ccend%g"
  return ${PIPESTATUS[0]}
}

How to turn on or off colors in bash如何在 bash 中打开或关闭颜色

by NIX CRAFT on NOVEMBER 4, 2006 · 1 COMMENT· LAST UPDATED NOVEMBER 4, 2006作者:NIX CRAFT 于 2006 年 11 月 4 日 · 1 条评论· 最后更新于 2006 年 11 月 4 日

in BASH SHELL在 BASH SHELL 中

Q. How do I turn on or off file name colors in bash shell?问:如何在 bash shell 中打开或关闭文件名颜色?

A. Most modern Linux distro comes with alias that defines colors for your file.答:大多数现代 Linux 发行版都带有为您的文件定义颜色的别名。 However ls command is responsible for displaying color on screen for files, directories and other object.但是 ls 命令负责在屏幕上显示文件、目录和其他对象的颜色。

By default, color is not used to distinguish types of files.默认情况下,不使用颜色来区分文件类型。 You need to pass --color option to ls command.您需要将 --color 选项传递给 ls 命令。

Task: Turn off colors任务:关闭颜色

Type the following command键入以下命令

$ ls --color=none

Or just remove alias with unalias command:或者只是使用 unalias 命令删除别名:

$ unalias ls

Task: Turn on colors任务:打开颜色

Use any of the following command:使用以下任一命令:

$ ls --color=auto
$ ls --color=tty

You can add or remove ls alias from ~/.bash_profile or ~/.bashrc file.您可以从~/.bash_profile or ~/.bashrc文件中添加或删除 ls 别名。

this commands for bash that are working well用于 bash 的此命令运行良好

handy tput commands

tput bold - Bold effect
tput rev - Display inverse colors
tput sgr0 - Reset everything
tput setaf {CODE}- Set foreground color, see color {CODE} below
tput setab {CODE}- Set background color, see color {CODE} below
Colors {code} code for tput command

Color {code}    Color
0    Black
1    Red
2    Green
3    Yellow
4    Blue
5    Magenta
6    Cyan
7    White

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM