【问题标题】:Terminal's font color changed after bash script executingbash脚本执行后终端字体颜色变化
【发布时间】:2015-04-18 09:49:35
【问题描述】:

我有这样一个脚本:

#!/bin/bash

temp=`inxi -xxx -w`
regex="(Conditions:(.+))(Wind:(.+))Humidity"
[[ $temp =~ $regex ]]
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[3]}

而且它很酷,除了这样一个细节 - 字体颜色发生了变化,现在整个文本变成了蓝色。我该如何预防?

【问题讨论】:

    标签: bash ubuntu terminal


    【解决方案1】:

    避免蓝色输出:

    #!/bin/bash
    
    temp=$(inxi -xxx -w | sed -r 's/\x1B\[[0-9;]*[JKmsu]//g')
    regex="(Conditions:(.+))(Wind:(.+))Humidity"
    [[ $temp =~ $regex ]]
    echo ${BASH_REMATCH[1]}
    echo ${BASH_REMATCH[3]}
    

    我所做的更改:

    • 反引号 (`) 已弃用。使用$(command) 而不是`command`
    • 您可以使用sed 正则表达式从inxi 的输出中删除颜色代码到I 管道上。

    正确使用蓝色输出:

    #!/bin/bash
    
    temp=$(inxi -xxx -w)
    regex="(Conditions:(.+))(Wind:(.+))Humidity"
    [[ $temp =~ $regex ]]
    echo ${BASH_REMATCH[1]}
    echo ${BASH_REMATCH[3]}
    printf "\e[0m" # Reset the color.
    
    • 您可以使用\033\x1b 而不是\e。都是一样的。
    • 您可以使用[39;49;00m,而不是[0m
    • 你可以用echo -e代替printf,但我不推荐。

    延伸阅读: http://wiki.bash-hackers.org/scripting/terminalcodes

    【讨论】:

    • 非常感谢 :) 实际上,在脚本输出中保存蓝色会很好,但禁止在终端中更改常用字体颜色。有可能吗?
    猜你喜欢
    • 2013-04-05
    • 2021-10-01
    • 2011-01-11
    • 2021-05-10
    • 2016-07-21
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多