How can I write colorful message on Linux Console?

How can I write colorful message on Linux Console? , mostly this kind of question is asked by newcomers (Specially those who are learning shell programming!). As you know in Linux everything is considered as a file, our console is one of such special file. You can write special character sequences to console, which control every aspects of the console like Colors on screen, Bold or Blinking text effects, clearing the screen, showing text boxes etc. For this purpose we have to use special code called escape sequence code .  Our Linux console is based on the DEC VT100 serial terminals which support ANSI escape sequence code.

What is special character sequence and how to write it to Console?

By default what ever you send to console it is printed as its. For e.g. consider following echo statement,
$ echo "Hello World"
Hello World
Above echo statement prints sequence of character on screen, but if there is any special escape sequence (control character) in sequence , then first some action is taken according to escape sequence (or control character) and then normal character is printed on console. For e.g. following echo command prints message in Blue color on console
$ echo -e "/033[34m   Hello Colorful  World!"
Hello Colorful  World!

Above echo statement uses ANSI escape sequence (/033[34m ), above entire string ( i.e.  "/033[34m   Hello Colorful  World!" ) is process as follows

1) First /033 , is escape character, which causes to take some action
2) Here it set screen foreground color to Blue using [34m escape code.
3) Then it prints our normal message Hello Colorful  World! in blue color.

Note that ANSI escape sequence begins with /033 (Octal value) which is represented as ^[ in termcap and terminfo files of terminals and documentation.

You can use echo statement to print message, to use ANSI escape sequence you must use -e option (switch) with echo statement, general syntax is as follows
Syntax
echo   -e  "/033[ escape-code     your-message "

In above syntax you have to use/033[ as its with different escape-code for different operations. As soon as console receives the message it start to process/read it, and if it found escape character (/033 ) it moves to escape mode, then it read "[ " character and moves into Command Sequence Introduction (CSI) mode. In CSI mode console reads a series of ASCII-coded decimal numbers (know as parameter) which are separated by semicolon (; ) . This numbers are read until console action letter or character is not found (which determines what action to take). In above example

/033 Escape character
[ Start of CSI
34 34 is parameter
m m is letter (specifies action)

Following table show important list of such escape-code/action letter or character

Character or letter Use in CSI Examples
hSet the ANSI modeecho -e "/033[h"
lClears the ANSI modeecho -e "/033[l"
mUseful to show characters in different colors or effects such as BOLD and Blink, see below for parameter taken by m.echo -e  "/033[35m Hello World"
qTurns keyboard num lock, caps lock, scroll lock LED on or off, see below.echo -e "/033[2q"
sStores the current cursor x,y position (col , row position) and attributesecho -e "/033[7s"
uRestores cursor position and attributesecho -e "/033[8u"

m understand following parameters

Parameter Meaning Example
0Sets default color scheme (White foreground and Black background), normal intensity, no blinking etc. 
1Set BOLD intensity$ echo -e "I am /033[1m BOLD /033[0m Person"
I am BOLD Person
Prints BOLD word in bold intensity and next ANSI Sequence remove bold effect (/033[0m )
2Set dim intensity$ echo -e "/033[1m  BOLD /033[2m DIM  /033[0m"
5Blink Effect$ echo -e "/033[5m Flash!  /033[0m"
7Reverse video effect i.e. Black foreground and white background in default color scheme$ echo -e "/033[7m Linux OS! Best OS!! /033[0m"
11Shows special control character as graphics character. For e.g. Before issuing this command press alt key (hold down it) from numeric key pad press 178 and leave both key; nothing will be printed. Now give --> command shown in example and try the above, it works. (Hey you must know extended ASCII Character for this!!!)$ press alt + 178
$ echo -e "/033[11m"
$ press alt + 178
$ echo -e "/033[0m"
$ press alt + 178

25Removes/disables blink effect 
27Removes/disables reverse effect 
30 - 37Set foreground color
31 - RED
32 - Green
xx - Try to find yourself this left as exercise for you :-)
$ echo -e "/033[31m I am in Red"
40 - 47Set background color
xx - Try to find yourself this left as exercise for you :-)
$ echo -e "/033[44m Wow!!!"

q understand following parameters

Parameters Meaning
0Turns off all LEDs on Keyboard
1Scroll lock LED on and others off
2Num lock LED on and others off
3Caps lock LED on and others off

Click here to see example of q command.

Click here to see example of m command.

Click here to see example of s and u command.

This is just quick introduction about Linux Console and what you can do using this Escape sequence. Above table does not contains entire CSI sequences. My up-coming tutorial series on C Programming Language will defiantly have entire story with S-Lang and curses (?). What ever knowledge you gain here will defiantly first step towards the serious programming using c. This much knowledge is sufficient for  Shell Programming, now try the following exercise :-) I am Hungry give me More Programming Exercise & challenges! :-)

1) Write function box(),  that will draw box on screen (In shell Script)
    box (left, top, height, width)
    For e.g. box (20,5,7,40)

   

Hint : Use ANSI Escape sequence
1) Use of 11 parameter to m
2) Use following for cursor movement
   row;col H
      or
   rowl;col f
  
  For e.g.
  $ echo   -e "/033[5;10H Hello"
  $ echo   -e "/033[6;10f Hi"

In Above example prints Hello message at row 5 and column 6 and Hi at 6th row and 10th Column.

link:http://www.freeos.com/guides/lsst/misc.htm#colorfunandmore

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值