• 2008-10-29

    在bash下输出彩色的文本 - [Linux Home]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://junger.blogbus.com/logs/30756958.html

    我想在Bash里写些彩色的字!这样才漂亮嘛!于是我找了很久,终于找到一篇英文的东东,我把它消化了介绍给大家。

    我们使用的命令是echo。大家都知道echo可以显示一些字符串,可以显示一些变量。例如:

    程序代码:
    [root@VMFedora5 ~]# echo 'Hello! Here is the content!'
    Hello! Here is the content!
    [root@VMFedora5 ~]# var1="The number"
    [root@VMFedora5 ~]# var2=7
    [root@VMFedora5 ~]#echo "$var1 is $var2 ."
    The number is 7 .

    首先我们来了解一下-n这个选项,它的意思是让echo别换行。例如

    程序代码:
    [root@VMFedora5 ~]#echo -n "Don't return:"
    Don't return:[root@VMFedora5 ~]#

    这个选项很有用哦~这样就可以在一行里显示五彩缤纷的字了。

    然后我们来了解一下-e这个选项。这个选项是彩色的灵魂哦~加了这个选项,echo就要开始认一些转义字符。用英语说就叫enable escape sequence.

    你可以试一下命令

    程序代码:
    echo -e "\034"

    怎么样?呵呵,这就是-e的作用了,你可以写个脚本试试,从\000试到\777。例如:
    程序代码:
    for (( i=0;i<=7;i=i+1 ))
    do
    for (( j=0;j<=7;j=j+1 ))
    do
    for (( k=0;k<=7;k=k+1 ))
    do
    echo -n "$i$j$k   "
    echo -en "\\$i$j$k   "
    done
    echo
    done
    done

    接下来要介绍我是怎么介绍一个命令的。黑体的,代表命令中固定的部份;下标的只是一个说明,代表这里这里会有几种选择,我会在后面注明所有的选择。

    好了,这就是,彩色的完整命令:

    echo -e "\E[3FgColor;4BgColorm""\033[turn_onmcontent\033[turn_offm"

    看到有点晕吧?
    \E[ 的意思是:嗯,我要开始定义前景背景色了。
    3代表是前景色,4代表是背景色。顺序不能颠倒。

    FgColor 和 BgColor 的选择有:
    0     黑色
    1     红色
    2     绿色
    3     黄色
    4     蓝色
    5     洋红
    6     青
    7     灰色
    8、9   白色

    是用来分割前景色和背景色的。
    m是说,嗯,前景背景色定义完了。

    \033[加上turn_on和m,意思是:嗯,我要开始用刚才定义的前景背景色写字了。后面紧跟着要写的内容content。
    \033[加上turn_off和m,意思是:嗯,我写完了。

    turn_on的选择有:
    1     淡些、粗些
    4     下划线
    7     反相(颠倒前景色和背景色)
    9     删除线
    2、3、5、6、8     正常

    turn_off的选择,只有一个:0

    给大家一个例子,输出前景红色背景白色的"Red"

    [root@VMFedora5 ~]# echo -e "\E[31;49m""\033[5mRed\033[0m"
    Red

    难道我们每次都些这么辛苦吗?会死人的……(虽然我已经习惯了)
    我发现,下面这段代码的效果与上面是一样的

    [root@VMFedora5 ~]#$i=31
    [root@VMFedora5 ~]#$j=49
    [root@VMFedora5 ~]# echo -e "\E[""$i;$j""m""\033[""1""m""Red""\033[""0""m"
    Red

    这就意味着我们可以将每个控制单元分别设成变量,然后写一个Shell脚本,来替代这种笨拙的方式。

    先写一个简单的,实现刚才那个效果的Shell脚本。

    [root@VMFedora5 ~]# vi Color.sh
    [root@VMFedora5 ~]# cat Color.sh
    #"Colorizing" Scripts

    function Color()
    {
    Fg="3""$1"
    Bg="4""$2"
    SetColor="\E[""$Fg;$Bg""m"
    UseColor="\033[""$3""m"
    Content="$4"
    EndColor="\033[0m"

    echo -en "$SetColor""$UseColor""$Content""$EndColor"

    }

    Color 1 9 5 Red
    [root@VMFedora5 ~]# chmod +x Color.sh
    [root@VMFedora5 ~]# ./Color.sh
    Red[root@VMFedora5 ~]#


    然后我们改进(这下 工程就大了……要能容错,还要处理长的句子)

    程序代码:
    #"Colorizing" Scripts

    function subColor()
    {
    Fg=$1
    Bg=$2
    SetColor="\E[""$Fg;$Bg""m"
    UseColor="\033[""$3""m"
    EndColor="\033[0m"
    Content="$e"
    echo -en "$SetColor""$UseColor"$Content"$EndColor"

    }
    function ShowHelp()
    {
    echo "Error!Show help file or continue?(h/c)"
    read Choice
    case $Choice in
    h|H)
    echo "This is a script for coloring characters and strings."
    echo "There are four parameters.Parameters are seperated by spacebars."
    echo "The frst parameter is a number ranged from 1 to 10,represents the foreground color."
    echo "The second parameter is  a number ranged from 1 to 10,represents the background color."
    Color 1 8 2 "1    red";echo
    Color 2 8 2 "2    green";echo
    Color 3 8 2 "3    yellow";echo
    Color 4 8 2 "4    blue";echo
    Color 5 8 2 "5    magenta";echo
    Color 6 8 2 "6    cyan";echo
    Color 7 8 2 "7    gray";echo
    Color 8 8 2 "8    white";echo
    Color 9 8 2 "9    white";echo
    Color 10 8 2 "10   black";echo
    echo "The third parameter is a number ranged from 1 to 9,represents the style of the characters."
    Color 10 8 1 "1    lighter,and bold";echo
    Color 10 8 4 "4    draw a line under the string.";echo
    Color 10 8 7 "7    swap the foreground color and the background color";echo
    Color 10 8 9 "9    draw a deleting line";echo
    echo "The fourth parameter is the content you wanna clolor,a string."
    echo "Thanks for using this script! "

    ;;
    c|C)
    echo "Script halted."
    exit 1
    ;;
    *)
    ShowHelp
    esac

    }

    function Color()
    {
    declare -i a=$1
    declare -i b=$2
    c=$3

    if [ $a -le 0 ] || [ $b -le 0 ] || [ $c -le 0 ] || [ $a -gt 10 ] || [ $b -gt 10 ] || [ $c -gt 9 ]
    then
    ShowHelp
    else
    if [ $a -eq 10 ]
    then
    a=30
    else
    a=30+$a
    fi
    if [ $b -eq 10 ]
    then
    b=40
    else
    b=40+$b
    fi

    e=""
    declare -i f=1

    for d in $@
    do
    if [ $f -eq 1 ] || [ $f -eq 2 ] || [ $f -eq 3 ]
    then
    e=$e
    else
    e="$e $d"
    fi

    let f=$f+1
    done

    subColor $a $b $c $e

    fi
    }

    Color $@


    测试一下:

    [root@VMFedora5 ~]# chmod +x Color.sh
    [root@VMFedora5 ~]# ./Color.sh 2 8 2 This is Green.
     This is Green.[root@VMFedora5 ~]# ./Color.sh 2 8 2 This is Green.;echo
    This is Green.

    这说明程序能正常工作。再看看出了错的话,程序是怎么处理的(还有彩色的帮助文件的哦~):

    [root@VMFedora5 ~]# ./Color.sh uwei wjeu wieuf woiewoe
    Error!Show help file or continue?(h/c)
    h
    This is a script for coloring characters and strings.
    There are four parameters.Parameters are seperated by spacebars.
    The frst parameter is a number ranged from 1 to 10,represents the foreground color.
    The second parameter is  a number ranged from 1 to 10,represents the background color.
    1 red
    2 green
    3 yellow
    4 blue
    5 magenta
    6 cyan
    7 gray
    8 white
    9 white
    10 black
    The third parameter is a number ranged from 1 to 9,represents the style of the characters.
    1 lighter,and bold
    4 draw a line under the string.

    7 swap the foreground color and the background color

    9 draw a deleting line
    The fourth parameter is the content you wanna clolor,a string.
    Thanks for using this script!
    [root@VMFedora5 ~]# ./Color.sh
    Error!Show help file or continue?(h/c)
    c
    Script halted.
    [root@VMFedora5 ~]# ./Color.sh
    Error!Show help file or continue?(h/c)
    f
    Error!Show help file or continue?(h/c)
    c
    Script halted.
    [root@VMFedora5 ~]#

    呵呵~Enjoy! 眨眼

     

     


    历史上的今天:


    收藏到:Del.icio.us




    评论

  • 为了便于老师调试我在期末课程设计中完善的此脚本,特将源代码发布在此。此脚本已经成为一个支持命令行参数、管道和stdin重定向为文件,并具有较高容错性能的脚本。


    程序代码:
    #!/bin/bash
    #"Colorizing" Scripts

    #First,define functions.


    function subColor()
    {
    Fg=$1
    Bg=$2
    SetColor="\E[""$Fg;$Bg""m"
    UseColor="\033[""$3""m"
    EndColor="\033[0m"
    Content=$e
    echo -en "$SetColor""$UseColor"$Content"$EndColor"

    }


    function ShowHelp()
    {
    echo "Error!"
    echo "Your parameters were $a,$b,$c,they are unexpected parameters."
    echo "Show help file or continue?(h|c)"
    if [ -c /dev/stdin ]
    then
    read Choice
    else
    Choice="h"
    fi

    case $Choice in
    h|H)
    echo
    echo "This is a script for coloring characters and strings."
    echo "There are four parameters.Parameters are seperated by spacebars."
    echo "The frst parameter is a number ranged from 1 to 10,represents the foreground color."
    echo "The second parameter is a number ranged from 1 to 10,represents the background color."
    Color 1 8 2 "1 red" <&-;echo
    Color 2 8 2 "2 green" <&-;echo
    Color 3 8 2 "3 yellow" <&-;echo
    Color 4 8 2 "4 blue" <&-;echo
    Color 5 8 2 "5 magenta" <&-;echo
    Color 6 8 2 "6 cyan" <&-;echo
    Color 7 8 2 "7 gray" <&-;echo
    Color 8 8 2 "8 white" <&-;echo
    Color 9 8 2 "9 white" <&-;echo
    Color 10 8 2 "10 black" <&-;echo
    echo "The third parameter is a number ranged from 1 to 9,represents the style of the characters."
    Color 10 8 1 "1 lighter,and bold" <&-;echo
    Color 10 8 4 "4 draw a line under the string." <&-;echo
    Color 10 8 7 "7 swap the foreground color and the background color" <&-;echo
    Color 10 8 9 "9 draw a deleting line" <&-;echo
    echo "The fourth parameter is the content you wanna clolor,a string."
    echo "Thanks for using this script! "
    echo "Script halted."
    exit 1


    ;;
    c|C)
    echo "Script halted."
    exit 1
    ;;
    *)
    ShowHelp
    esac


    }


    function ParaCheck()
    {
    if [ $a -le 0 ] || [ $b -le 0 ] || [ $c -le 0 ] || [ $a -gt 10 ] || [ $b -gt 10 ] || [ $c -gt 9 ]
    then
    ShowHelp
    else
    if [ $a -eq 10 ]
    then
    a=30
    else
    a=30+$a
    fi
    if [ $b -eq 10 ]
    then
    b=40
    else
    b=40+$b
    fi
    fi
    }


    function GenString()
    {
    e=""
    declare -i f=1

    for d in $@
    do
    if [ $f -eq 1 ] || [ $f -eq 2 ] || [ $f -eq 3 ] || [ $d = "" ]
    then
    e=$e
    elif [ $f -eq 4 ]
    then
    e="$e$d"
    else
    e="$e $d"
    fi

    let f=$f+1
    done

    }


    function AwkGenL()
    {
    echo "BEGIN {" 1>op1.awk
    echo 'FS="\n"' 1>>op1.awk
    echo 'RS="EOF"' 1>>op1.awk
    echo "}" 1>>op1.awk
    Item=$1
    echo "{print \$"$Item"}" 1>>op1.awk
    }


    function AwkGenC()
    {
    Item=$1
    echo "{print \$"$Item"}" 1>op2.awk
    }


    function ReadRich()
    {
    declare -i lines=`wc -l sth.sth|awk '{print $1}'`
    for ((i=1; i<=$lines;i=$i+1))
    do
    AwkGenL $i
    AwkGenC 1
    awk -f op1.awk sth.sth 1>tmp.tmp
    declare -i a=`awk -f op2.awk tmp.tmp`
    AwkGenC 2
    declare -i b=`awk -f op2.awk tmp.tmp`
    AwkGenC 3
    declare -i c=`awk -f op2.awk tmp.tmp`
    ParaCheck
    declare -i words=`wc -m tmp.tmp|awk '{print $1}'`
    words=$words-1
    declare -i ParaPos=`awk '{print $1" "$2" "$3}' tmp.tmp|wc -m|awk '{print $1}'`
    ParaPos=$ParaPos+1
    declare -i w=$words-1
    Tn=`cut -c$w-$words tmp.tmp`

    if [ "$Tn" = "Tn" ]
    then
    words=$words-2
    e=`cut -c$ParaPos-$words tmp.tmp`
    subColor $a $b $c $e
    echo
    else
    e=`cut -c$ParaPos-$words tmp.tmp`
    subColor $a $b $c $e
    fi
    done
    }


    function Color()
    {
    declare -i a=$1
    declare -i b=$2
    declare -i c=$3


    if [ -c /dev/stdin ]
    then
    ParaCheck
    GenString $@
    subColor $a $b $c $e
    else
    if [ -p /dev/stdin ]
    then
    ParaCheck
    e=`cat /dev/stdin`
    subColor $a $b $c $e
    else
    if [ -f /dev/stdin ]
    then
    cat /dev/stdin 1>sth.sth
    ReadRich
    yes|rm sth.sth 2>/dev/null
    yes|rm tmp.tmp 2>/dev/null
    yes|rm op1.awk 2>/dev/null
    yes|rm op2.awk 2>/dev/null
    else
    ParaCheck
    GenString $@
    subColor $a $b $c $e
    fi
    fi
    fi


    }

    #Here is where the script begins.

    Color $@