bash colorizing

来源:互联网 发布:java程序员简历项目 编辑:程序博客网 时间:2024/06/06 07:18

ubuntu 11 .bashrc at theend of this passage

http://bbs.chinaunix.net/thread-190816-1-1.html

ESC在字符串中可以以\033或\e表达

一、 锁\开键盘代码 
  代码定义:
    锁键盘:ESC[2h
    开键盘:ESC[2l
  应用:
  1.直接在键盘上顺序敲入Esc+[+2+h四键,键盘即被锁住。
  2.在C语言程序显示字符串中包含锁代码:printf(″%c[2h字串″,'\033')。
  3.在shell程序中用echo命令:echo″^[[2h字串″.注Esc键在vi编辑器中输入方法为:Ctrl+v,然后按一下Esc键,在屏幕上显示^[表示已输入。
  同样显示开锁代码则键盘被解锁。
 二、 删除字符代码 
  代码定义:
  ESC[nX:清除光标右边n个字符,光标不动。
  ESC[K或ESC[OK;清除光标右边全部字符,光标不动。
  ESC[1K:清除光标左边全部字符,光标不动。
  ESC[2K:清除整行,光标不动。
  ESC[J或ESC[OJ:清除光标右下屏所有字符,光标不动。
  ESC[1J:清除光标左上屏所有字符,光标不动。
  ESC[2J或ESCc:清屏,光标移到左上角。
  ESC[nM:删除光标之下n行,剩下行往上移,光标不动。
  ESC[nP:删除光标右边n个字符,剩下部分左移,光标不动。
 三、 插入字符代码 
  代码定义:
  ESC[n@:在当前光标处插入n个字符。
  ESC[nL:在当前光标下插入n行。
 四、 移动光标 
  代码定义:
  ESC[nA:光标上移n行。
  ESC[nB:光标下移n行。
  ESC[nC:光标右移n个字符。
  ESC[nD:光标左移n个字符。
  ESC[n;mH :光标定位到第n行m列(类似代码ESC[n;mf)。
 五、 定义字符显示属性代码 
  代码定义:
  ESC[p;p;p;……m 其中属性值p可以是一个或多个,具体定义如下:
  p 含义
  0 清除所有属性
  1 高亮度显示
  4 下划线(如果硬件支持的话)
  5 闪烁(如果硬件支持的话)
  25 无闪烁
  7 反场(前背景交换)
  27 无反场
  8 隐藏(不显示)
  10 选择基本字体
  11 选择第一替代字体;让ASCII值小于32的字符显示时直接取自ROM芯片内
  12 选择第二替代字体;在作为ROM字符显示之前先压缩扩展高位ASCII码值
  30 前景黑色
  31 前景红色
  32 前景绿色
  33 前景褐色
  34 前景蓝色
  35 前景紫色
  36 前景蓝绿色
  37 前景白色
  38 开启下划线标志;白色前景用白色下划线
  39 关闭下划线标志
  40 背景黑色
  41 背景红色
  42 背景绿色
  43 背景褐色
  44 背景蓝色
  45 背景紫色
  46 背景蓝绿色
  47 背景白色
 六、其它代码 
  代码定义:
  ESC7:保存当前光标位置参数及字符属性。
  ESC8:恢复保存的光标位置参数及字符属性。
  \n :插入换行符
  \t :插入一个Tab键。
  \r :插入回车符。
  ^G :插入响铃符(pc嗽叭鸣叫一次),在vi中按Ctrl+g即可输入。
  \c :用于shell程序中,不换行。



http://blog.csdn.net/qualcent/article/details/7106483

# echo命令介绍
功能说明:显示文字。
语   法:echo [-ne][字符串] / echo [--help][--version]
补充说明:echo会将输入的字符串送往标准输出。输出的字符串间以空白字符隔开, 并在最后加上换行号。
参   数:
-n 不要在最后自动换行
-e 打开反斜杠ESC转义。若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出:
\a 发出警告声;
\b 删除前一个字符;
\c 最后不加上换行符号;
\f 换行但光标仍旧停留在原来的位置;
\n 换行且光标移至行首;
\r 光标移至行首,但不换行;
\t 插入tab;
\v 与\f相同;
\\ 插入\字符;
\nnn 插入nnn(八进制)所代表的ASCII字符;
-E 取消反斜杠ESC转义 (默认)
-help 显示帮助
-version 显示版本信息

############################################################
# echo输出颜色文本
echo命令改变样式,以输出不同颜色的文本,必须有 -e 选项(开启echo中的转义)。
文本终端的显示颜色可以使用“ANSI非常规字符序列”来生成。
例如:echo -e "\033[44;37;5m ME\033[0m COOL"
解释:"\033[44;37;5m ME "设置背景为蓝色,前景为白色,闪烁光标,输出字符“ME”;
"\033[0m COOL"重新设置屏幕到缺省设置,输出字符 “COOL”。
  "e"是命令echo的一个可选项,它用于激活特殊字符的解析器。"\033"引导非常规字符序列(即"\033["表示终端转义字符开始,"\033"即退出键<ESC>的ASCII码)。"m"意味着设置属性然后结束非常规字符序列,这个例子里真正有效的字符是"44;37;5"和"0"。修改"44;37;5"可以生成不同颜色的组合,数值和编码的前后顺序没有关系。

可以选择的编码如下所示(这些颜色是ANSI标准颜色):
编码 颜色/动作
0   重新设置属性到缺省设置
1   设置粗体
2   设置一半亮度(模拟彩色显示器的颜色)
4   设置下划线(模拟彩色显示器的颜色)
5   设置闪烁
7   设置反向图象
22   设置一般密度
24   关闭下划线
25   关闭闪烁
27   关闭反向图象
30   设置黑色前景
31   设置红色前景
32   设置绿色前景
33   设置黄色前景
34   设置蓝色前景
35   设置紫色前景
36   设置青色前景
37   设置白色(灰色)前景
38   在缺省的前景颜色上设置下划线
39   在缺省的前景颜色上关闭下划线
40   设置黑色背景
41   设置红色背景
42   设置绿色背景
43   设置黄色背景
44   设置蓝色背景
45   设置紫色背景
46   设置青色背景
47   设置白色(灰色)背景
49   设置缺省黑色背景
其他有趣的代码还有:
\033[2J   清除屏幕
\033[0q   关闭所有的键盘指示灯
\033[1q   设置"滚动锁定"指示灯(Scroll Lock)
\033[2q   设置"数值锁定"指示灯(Num Lock)
\033[3q   设置"大写锁定"指示灯(Caps Lock)
\033[15:40H 把关闭移动到第15行,40列
\007    发蜂鸣生beep

一些说明:
前景颜色各数字是对应背景颜色减去10。
结束非常规字符序列的"m"要紧跟前面的数字,不能有空格。
命令也可以写成echo -e "^[[44;37;5m ME \033[0m COOL",其中的"^["是先按Ctrl-V,然后再按<ESC>键产生的。

输出带有颜色的文本,echo命令必须带有选项"-e"。

这种方法只能暂时改变echo命令输出的文本的样式,logout后就恢复为默认。修改.bashrc文件,可以修改默认的显示样式。
如:在.bashrc文件的最后面追加一行:echo -e '\033[47;30m'。

#-----------------------------------------------------------
# 建议:在shell文件的最前面,将echo命令的输出样式定义成变量。
# define echo terminal style
# color: 0~6 --> black, red, green, yellow, blue, purple, cyan, grey
export ECHO_STYLE_00="\033[0m" # default style(black background, white foreground)
export ECHO_STYLE_01="\033[41;33;1m" # red background, yellow foregound bold
echo -e "${ECHO_STYLE_01}echo command terminal style example${ECHO_STYLE_00}"

############################################################
# echo命令的其他用法
1).光标跳到第60列,然后显示一个OK。
格式:echo -en '\033[60G' && echo OK
说明:"\033["是终端转义字符开始,60G是命令



http://www.faqs.org/docs/abs/HTML/colorizing.html

Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting

Prev Chapter 34. Miscellany Next

34.5. "Colorizing" Scripts

The ANSI [1] escape sequences set screen attributes, such as bold text, and color of foreground and background. DOS batch files commonly used ANSI escape codes for color output, and so can Bash scripts.

Example 34-8. A "colorized" address database 1 #!/bin/bash
2 # ex30a.sh: "Colorized" version of ex30.sh.
3 # Crude address database
4
5
6 clear # Clear the screen.
7
8 echo -n " "
9 echo -e '\E[37;44m'"\033[1mContact List\033[0m"
10 # White on blue background
11 echo; echo
12 echo -e "\033[1mChoose one of the following persons:\033[0m"
13 # Bold
14 tput sgr0
15 echo "(Enter only the first letter of name.)"
16 echo
17 echo -en '\E[47;34m'"\033[1mE\033[0m" # Blue
18 tput sgr0 # Reset colors to "normal."
19 echo "vans, Roland" # "[E]vans, Roland"
20 echo -en '\E[47;35m'"\033[1mJ\033[0m" # Magenta
21 tput sgr0
22 echo "ones, Mildred"
23 echo -en '\E[47;32m'"\033[1mS\033[0m" # Green
24 tput sgr0
25 echo "mith, Julie"
26 echo -en '\E[47;31m'"\033[1mZ\033[0m" # Red
27 tput sgr0
28 echo "ane, Morris"
29 echo
30
31 read person
32
33 case "$person" in
34 # Note variable is quoted.
35
36 "E" | "e" )
37 # Accept upper or lowercase input.
38 echo
39 echo "Roland Evans"
40 echo "4321 Floppy Dr."
41 echo "Hardscrabble, CO 80753"
42 echo "(303) 734-9874"
43 echo "(303) 734-9892 fax"
44 echo "revans@zzy.net"
45 echo "Business partner & old friend"
46 ;;
47
48 "J" | "j" )
49 echo
50 echo "Mildred Jones"
51 echo "249 E. 7th St., Apt. 19"
52 echo "New York, NY 10009"
53 echo "(212) 533-2814"
54 echo "(212) 533-9972 fax"
55 echo "milliej@loisaida.com"
56 echo "Girlfriend"
57 echo "Birthday: Feb. 11"
58 ;;
59
60 # Add info for Smith & Zane later.
61
62 * )
63 # Default option.
64 # Empty input (hitting RETURN) fits here, too.
65 echo
66 echo "Not yet in database."
67 ;;
68
69 esac
70
71 tput sgr0 # Reset colors to "normal."
72
73 echo
74
75 exit 0


The simplest, and perhaps most useful ANSI escape sequence is bold text, \033[1m ... \033[0m. The \033 represents an escape, the "[1" turns on the bold attribute, while the "[0" switches it off. The "m" terminates each term of the escape sequence. bash$ echo -e "\033[1mThis is bold text.\033[0m"




A similar escape sequence switches on the underline attribute (on an rxvt and and an aterm). bash$ echo -e "\033[4mThis is underlined text.\033[0m"



With an echo, the -e option enables the escape sequences.


Other escape sequences change the text and/or background color. bash$ echo -e '\E[34;47mThis prints in blue.'; tput sgr0


bash$ echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0


The tput sgr0 restores the terminal settings to normal. Omitting this lets all subsequent output from that particular terminal remain blue.

Use the following template for writing colored text on a colored background.

echo -e '\E[COLOR1;COLOR2mSome text goes here.'

The "\E[" begins the escape sequence. The semicolon-separated numbers "COLOR1" and "COLOR2" specify a foreground and a background color, according to the table below. (The order of the numbers does not matter, since the foreground and background numbers fall in non-overlapping ranges.) The "m" terminates the escape sequence, and the text begins immediately after that.

Note also that single quotes enclose the remainder of the command sequence following the echo -e.


The numbers in the following table work for an rxvt terminal. Results may vary for other terminal emulators.

Table 34-1. Numbers Representing Colors in Escape SequencesColor Foreground Background
black 30 40
red 31 41
green 32 42
yellow 33 43
blue 34 44
magenta 35 45
cyan 36 46
white 37 47


Example 34-9. Echoing colored text 1 #!/bin/bash
2 # color-echo.sh: Echoing text messages in color.
3
4 # Modify this script for your own purposes.
5 # It's easier than hand-coding color.
6
7 black='\E[30;47m'
8 red='\E[31;47m'
9 green='\E[32;47m'
10 yellow='\E[33;47m'
11 blue='\E[34;47m'
12 magenta='\E[35;47m'
13 cyan='\E[36;47m'
14 white='\E[37;47m'
15
16
17 alias Reset="tput sgr0" # Reset text attributes to normal
18 #+ without clearing screen.
19
20
21 cecho () # Color-echo.
22 # Argument $1 = message
23 # Argument $2 = color
24 {
25 local default_msg="No message passed."
26 # Doesn't really need to be a local variable.
27
28 message=${1:-$default_msg} # Defaults to default message.
29 color=${2:-$black} # Defaults to black, if not specified.
30
31 echo -e "$color"
32 echo "$message"
33 Reset # Reset to normal.
34
35 return
36 }
37
38
39 # Now, let's try it out.
40 # ----------------------------------------------------
41 cecho "Feeling blue..." $blue
42 cecho "Magenta looks more like purple." $magenta
43 cecho "Green with envy." $green
44 cecho "Seeing red?" $red
45 cecho "Cyan, more familiarly known as aqua." $cyan
46 cecho "No color passed (defaults to black)."
47 # Missing $color argument.
48 cecho "\"Empty\" color passed (defaults to black)." ""
49 # Empty $color argument.
50 cecho
51 # Missing $message and $color arguments.
52 cecho "" ""
53 # Empty $message and $color arguments.
54 # ----------------------------------------------------
55
56 echo
57
58 exit 0
59
60 # Exercises:
61 # ---------
62 # 1) Add the "bold" attribute to the 'cecho ()' function.
63 # 2) Add options for colored backgrounds.

There is, however, a major problem with all this. ANSI escape sequences are emphatically non-portable. What works fine on some terminal emulators (or the console) may work differently, or not at all, on others. A "colorized" script that looks stunning on the script author's machine may produce unreadable output on someone else's. This greatly compromises the usefulness of "colorizing" scripts, and possibly relegates this technique to the status of a gimmick or even a "toy".


Moshe Jacobson's color utility (http://runslinux.net/projects/color) considerably simplifies using ANSI escape sequences. It substitutes a clean and logical syntax for the clumsy constructs just discussed.
Notes[1]
ANSI is, of course, the acronym for the American National Standards Institute.
Prev Home Next

Recursion Up Optimizations


ubuntu 11 .bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'