AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 560974
Accepted
Just a learner
Just a learner
Asked: 2020-01-09 02:52:34 +0800 CST2020-01-09 02:52:34 +0800 CST 2020-01-09 02:52:34 +0800 CST

为什么我的颜色没有在所有终端中显示?

  • 772

我有以下一段代码。

bold=''
reset=$(echo -en '\033[0m')
black=$(echo -en '\e[1;30m')
magenta=$(echo -en '\033[00;35m')
blue=$(echo -en '\e[1;34m')
cyan=$(echo -en '\e[1;36m')
green=$(echo -en '\e[1;32m')
orange=$(echo -en '\e[1;33m')
purple=$(echo -en '\e[1;35m')
red=$(echo -en '\e[1;31m')
white=$(echo -en '\e[1;37m')
yellow=$(echo -en '\e[1;33m')
lime_yellow=$(echo -en '\e[1;33m')
power_blue=$(echo -en '\e[1;34m')
blink=$(echo -en '\e[1;31m')
reverse=$(echo -en '\e[1;31m')
underline=$(echo -en '\e[1;31m')

if [ -x /usr/bin/tput ] && tput setaf 1 &>/dev/null; then
    echo "tput color is supported."
    tput sgr0 # Reset colors
    bold=$(tput bold)
    reset=$(tput sgr0)
    black=$(tput setaf 0)
    magenta=$(tput setaf 5)
    blue=$(tput setaf 33)
    cyan=$(tput setaf 37)
    green=$(tput setaf 64)
    orange=$(tput setaf 166)
    purple=$(tput setaf 125)
    red=$(tput setaf 124)
    white=$(tput setaf 15)
    yellow=$(tput setaf 136)
    lime_yellow=$(tput setaf 190)
    power_blue=$(tput setaf 153)
    blink=$(tput blink)
    reverse=$(tput smso)
    underline=$(tput smul)
else
    echo "tput color is not supported. Use old school colors."
fi

echo ${red}RED${green}GREEN${yellow}YELLOW${blue}BLUE${purple}PURPLE${cyan}CYAN${white}WHITE${reset}

基本上有两种颜色,tput生成的或老式的转义字符,如\e[1;32m. 由于tput类型更有趣,例如它支持闪烁和下划线,因此代码tput尽可能使用类型颜色。这是一张证明它在 Oracle Linux 7.6(有点像 RedHat 或 CentOS)GUI 环境中按预期工作的图像。

在此处输入图像描述

当我从其他终端运行它时,它不起作用。例如,下面是在 MobaXterm 中运行时的快照。

在此处输入图像描述

我也试过putty了,也不行。我的代码有什么问题吗?


更新

我echo $TERM在每个终端执行,下面是结果。

带有桌面环境的 Oracle Linux(彩色作品)
输出:xterm-256color

Windows 上的 MobaXterm(颜色不起作用)
输出:xterm

Windows 上的腻子(颜色不起作用)
输出:xterm
terminal gnome
  • 3 3 个回答
  • 2025 Views

3 个回答

  • Voted
  1. JdeBP
    2020-01-09T04:55:30+08:002020-01-09T04:55:30+08:00

    您必须将您的终端类型配置为putty、putty-256color,或者putty-sco在使用 PuTTY 或基于它的东西(例如 MobaXTerm)时。它们是 terminfo 数据库中的条目正确描述 PuTTY的唯一终端类型。

    一个普遍的错误假设是终端仿真器都与 XTerm 兼容,并且 terminfo 数据库中的xterm和xterm-256color条目正确地描述了它们。

    这种错误想法在 Thomas Dickey 的 XTerm FAQ 中被指出,值得注意的是xterm和xterm-256color条目甚至没有描述 XTerm 的所有版本,更不用说其他终端仿真器了。

    The putty entry in the terminfo database describes a terminal that is only capable of 8 ECMA-48 colours. As, indeed, does the xterm entry. But merely switching from xterm to xterm-256colour is wrong. PuTTY differs from XTerm.

    In fact, PuTTY is quite capable of indexed colour (256 colours from a palette) using ISO/IEC 8613 control sequences. Indeed, since 2017 it is quite capable of direct colour (24-bit RGB colour) using ISO/IEC 8613 control sequences. The putty-256colour entry describes the former. terminfo does not have a way to fully describe the latter.

    Use the correct terminal type, and tput will look up the correct control sequences.

    Further reading

    • https://unix.stackexchange.com/a/419092/5132
    • 5
  2. Best Answer
    Philippe
    2020-01-09T04:06:15+08:002020-01-09T04:06:15+08:00

    这些代码应该可以工作:

        magenta=$(tput setaf 5)
        blue=$(tput setaf 4)
        cyan=$(tput setaf 6)
        green="$(tput setaf 2)"
        purple=$(tput setaf 5)
        red=$(tput setaf 1)
        white=$(tput setaf 7)
        yellow=$(tput setaf 3)
    
    • 2
  3. Just a learner
    2020-01-09T04:17:16+08:002020-01-09T04:17:16+08:00

    除了8种基本颜色之外,不支持颜色的是终端的能力。我找到了这段代码来测试所有可用的颜色(来源:tput setaf 颜色表?如何确定颜色代码?):

    # Connector fifos directory
    read TMPDIR < <(mktemp -d /dev/shm/bc_shell_XXXXXXX)
    
    fd=3
    # find next free fd
    nextFd() { while [ -e /dev/fd/$fd ];do ((fd++)) ;done;printf -v $1 %d $fd;}
    
    tputConnector() {
        mkfifo $TMPDIR/tput
        nextFd TPUTIN
        eval "exec $TPUTIN> >(LANG=C exec stdbuf -o0 tput -S - >$TMPDIR/tput 2>&1)"
        nextFd TPUTOUT
        eval "exec $TPUTOUT<$TMPDIR/tput"
    }
    myTput() { echo -e "$1\ncr" 1>&$TPUTIN && IFS= read -r -d $'\r' -u $TPUTOUT $2
    }
    tputConnector
    
    myTput op op
    myTput "setaf 7" grey
    myTput "setaf 16" black
    fore=("$black" "$grey")
    for ((i=0; i<256; i++)) ;do
        myTput "setab $i" bgr
        printf "  %s%s  %3d  %s" "$bgr" "${fore[ i>231 && i<244||(i<17)&& (i%8<2)||
            (i>16&&i<232)&&((i-16)%6*11+(i-16)/6%6*14+(i-16)/36*10)<58
            ? 1 : 0 ]}" $i "$op"
        (( ((i<16||i>231) && ((i+1)%8==0)) || ((i>16&&i<232)&& ((i-15)%6==0)) )) &&
            printf "\n" ''
    done
    

    以下是我测试过的所有三个终端的输出。

    • 带有桌面环境的 Oracle Linux 在此处输入图像描述

    • Windows 上的 MobaXterm 在此处输入图像描述

    • Windows 上的腻子 在此处输入图像描述

    所以它的终端不支持除了 8 种基本颜色之外的颜色。为了安全和便携,请仅使用这 8 种颜色。

    • 0

相关问题

  • 备份 Nand Flash 存储区

  • Parabola (Arch-like) - ICU 更新导致某些程序需要两个不同版本的 ICU 库。两者都无法启动。无法复制

  • Debian Stretch:libgs_plugin_systemd-updates.so 中的 gnome-software 段错误

  • 如何在拼音输入法中输入ü?

  • 在 Gnome3 中禁用窗口的自动最大化

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve