LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 5443|回复: 13

关于如何设定控制台的彩色显示?

[复制链接]
发表于 2003-9-29 21:24:40 | 显示全部楼层 |阅读模式
比如这样的PS1="\[\033[1;34m\][\$(date +%H%M)][\u@\h:\w]$\[\033[0;37m\] "
里面那些[\033[0;37m的代码
发表于 2003-9-29 22:24:36 | 显示全部楼层
to Freebird
我很少在字符下使用什么颜色 :sorry
不过你看看下面的链接,不知道是否对你有帮助
http://www.linuxsir.cn/bbs/showt ... hlight=%C4%A7%CA%F5
发表于 2003-9-29 23:13:14 | 显示全部楼层
Freebird兄,我说一下不知道给不给呵~~~~~,抢了javalee兄的风头,不好意思

使用颜色:
使用色彩需要在echo语句中嵌入控制字符,只要是带有彩色终端的系统都可以使用,
我们知道在linux里用echo语句用选项-e可以使转义的字符产生效果,(ps:Freebird兄爱好的FreeBSD也是这样的,但在System V中不一样,不用使用-e选项),也可以不使用-e选项,但要这样做ctrl+v,之后按ESC,得到转义序列(这句为什么这样我也不懂???? ,有兄弟懂的指点一下啊?!谢谢!!:thank )

使用终端颜色(前景和背景)的格式为:
^[ [背景色代码;前景色代码m
或是 \033[背景色代码;前景色代码m
其中^[要这样产生ctrl+v,之后按ESC.

一些常用的颜色代码:(这些颜色是ANSI标准颜色)
前景色:30黑 31红 32绿 33黄 34蓝 35紫 36青 37白
背景色:40黑 41红 42绿 43黄 44青 45蓝 46青 47白

举个例子,可以这样做马上使终端的行变成红后绿前
#echo '^[[41;32m'
#echo "^[[41;32m"
#echo -e "\033[41;32m"
#echo -e '\033[41;32m'

以上四种方法都是可以的

要定制自己的颜色终端,可以把上面的类似行加入到.bash_profile中去,每次启动终端后都会是自己定制的颜色了。
我曾经写过一个失败的脚本,
http://www.linuxsir.cn/bbs/showt ... highlight=100%CC%FB
里面有一个函数是color(),定制了几种颜色,有兴趣的兄弟可以看看那个函数
虽然整个脚本是失败的,但那个函数可不是失败的,不要误解啊!!:p

最后,倒要请教javalee兄整理一下关于转义字符方面的内容,以上的几种方式为什么都可以,我其实是不明白的,有没有这方面的内容介绍啊?!:thank
发表于 2003-9-29 23:14:45 | 显示全部楼层
附带说一下,那个PS1的不懂:(
惭愧!!!!
发表于 2003-9-29 23:35:02 | 显示全部楼层
我系统(RH)中有个文件:/etc/termcap,看看它吧~~(E文的)
PS1,主提示符.
(我更惭愧)
发表于 2003-9-29 23:42:55 | 显示全部楼层
YES!!!主提示符,真是惭愧,我隐约记起我看过这方面的介绍,可惜不全,忘的也快

E文?惨,硬着头皮看了(正好我的也是RH)
发表于 2003-9-30 00:41:44 | 显示全部楼层
这个帖子里有详细的序列转义的说明
http://www.linuxsir.cn/bbs/showt ... hlight=%C4%A7%CA%F5
# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
上述引用来自
cat /etc/DIR_COLORS文件则介绍了不同的颜色转义序列值
发表于 2003-9-30 01:12:00 | 显示全部楼层

感动ing,javalee兄的责任心和无比的耐心~~~~

谢谢指教!!认真阅读ing
 楼主| 发表于 2003-9-30 08:27:05 | 显示全部楼层
感谢两位大哥!!大恩铭记在心,感动ing........呵呵,这个极好!
 楼主| 发表于 2003-9-30 08:43:53 | 显示全部楼层
As mentioned before, non-printing escape sequences have to be enclosed in \[\033[ and \]. For colour escape sequences, they should also be followed by a lowercase m.



If you try out the following prompts in an xterm and find that you aren't seeing the colours named, check out your ?/.Xdefaults file (and possibly its bretheren) for lines like "XTerm*Foreground: BlanchedAlmond". This can be commented out by placing an exclamation mark ("!") in front of it. Of course, this will also be dependent on what terminal emulator you're using. This is the likeliest place that your term foreground colours would be overridden.



To include blue text in the prompt:


PS1="\[\033[34m\][\$(date +%H%M)][\u@\h:\w]$ "



The problem with this prompt is that the blue colour that starts with the 34 colour code is never switched back to the regular gray, so any text you type after the prompt is still in the colour of the prompt. This is also a dark shade of blue, so combining it with the bold code might help:


PS1="\[\033[1;34m\][\$(date +%H%M)][\u@\h:\w]$\[\033[0;37m\] "




The prompt is now in light blue, and it ends by switching the colour back to gray, which is the colour most of us expect to type in.



Note: I should point out that, if you're working in an Xterm, and you've set your colours with something like "xterm -bg midnightblue -fg yellow", switching the colour back as I've described above over-rides the "-fg" that you used. I'm not aware of a way around this, please let me know if you have a solution. If you use a light-coloured background in your xterms, the gray text used above may be unreadable. If you want to use a prompt on both the console and xterms, but you use light background in your xterms, you may have to do some colour setting with a "case $TERM in ..." as shown in the Title Bar section.



Here are the rest of the colour equivalences:


Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37




You can also set background colours by using 44 for Blue background, 41 for a Red background, etc. There are no bold background colours. Combinations can be used, like Light Red text on a Blue background: \[\033[44;1;31m\], although setting the colours separately seems to work better (ie. \[\033[44m\]\[\033[1;31m\]). Other codes available include 4: Underscore, 5: Blink, 7: Inverse, and 8: Concealed.



Based on a prompt called "elite2" in the Bashprompt package (which I have modified to work better on a standard console, rather than with the special xterm fonts required to view the original properly), this is a prompt I've used a lot:



function elite
{

local GRAY="\[\033[1;30m\]"
local LIGHT_GRAY="\[\033[0;37m\]"
local CYAN="\[\033[0;36m\]"
local LIGHT_CYAN="\[\033[1;36m\]"

case $TERM in
    xterm*)
        local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
        ;;
    *)
        local TITLEBAR=""
        ;;
esac

local GRAD1=$(tty|cut -d/ -f3)
PS1="$TITLEBAR\
$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\u$GRAY@$CYAN\h\
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\#$GRAY/$CYAN$GRAD1\
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\$(date +%H%M)$GRAY/$CYAN\$(date +%d-%b-%y)\
$LIGHT_CYAN)$CYAN-$GRAY-\
$LIGHT_GRAY\n\
$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\$$GRAYCYAN\w\
$LIGHT_CYAN)$CYAN-$GRAY-$LIGHT_GRAY "
PS2="$LIGHT_CYAN-$CYAN-$GRAY-$LIGHT_GRAY "
}




I define the colours as temporary shell variables in the name of readability. It's easier to work with. The "GRAD1" variable is a check to determine what terminal you're on. Like the test to determine if you're working in an Xterm, it only needs to be done once. The prompt you see look like this, except in colour:


--(giles@nikola)-(75/ttyp7)-(1908/12-Oct-98)--
--($:~/tmp)--
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表