stty的用法及解析--escape sequence code的用法

来源:互联网 发布:印刷报价小秘书软件 编辑:程序博客网 时间:2024/05/19 15:22

在linux console显示各种颜色的信息可以使用特殊的符号串:escape  sequence code.通过使用escape sequence code可以在linux终端以高亮,粗体,闪烁、多种颜色等方式展示消息

下面以实例说明:

在终端打印hello world


如果加上escape  sequence code就可以调整打印在终端上的消息属性:


\033[34m  就是 escape  sequence code 

\033[34m   Hello Colorful  World\!整个字符串的处理过程为:

1.读到 \033,说明后面的字符是ANSI escape sequence,会特殊处理

2.使用 [34m 来设定终端的前景色为蓝色

3.打印 Hello Colorful World\! ,颜色为蓝色


\033[ 实际上在输入的文本信息中作为一个特殊标识,终端读到 \033 字符的时候,识别出这是一个escape character;将切换到escape模式。然后读取“[” 字符移入到CSI模式(Command Sequence Introduction (CSI) mode),在CSI模式下,终端读取这些ASCII码用分号“;”隔开,直到读入一个完整的动作指令。上面打印Hello Colorful  World!的例子里面

\033   作为escape character, 通知终端切换到escape模式;

[        CSI的开始;

34      读入的参数;

m       制定将要执行的动作


在shell脚本中可能会使用到一些常用的动作及属性,表格表示如下:

Some ANSI escape sequences (not a complete list)CodeNameEffectCSI n ACUU – CUrsor UpMoves the cursor n (default 1) cells in the given direction. If the cursor is already at the edge of the screen, this has no effect.CSI n BCUD – CUrsor DownCSI n CCUF – CUrsor ForwardCSI n DCUB – CUrsor BackCSI n ECNL – Cursor Next LineMoves cursor to beginning of the line n (default 1) lines down.CSI n FCPL – Cursor Previous LineMoves cursor to beginning of the line n (default 1) lines up.CSI n GCHA – Cursor Horizontal AbsoluteMoves the cursor to column n.CSI n ; m HCUP – CUrsor PositionMoves the cursor to row n, column m. The values are 1-based, and default to 1 (top left corner) if omitted. A sequence such as CSI ;5H is a synonym for CSI 1;5H as well as CSI 17;H is the same as CSI 17H and CSI 17;1HCSI n JED – Erase DataClears part of the screen. If n is zero (or missing), clear from cursor to end of screen. If n is one, clear from cursor to beginning of the screen. If n is two, clear entire screen (and moves cursor to upper left on MS-DOS ANSI.SYS).CSI n KEL – Erase in LineErases part of the line. If n is zero (or missing), clear from cursor to the end of the line. If n is one, clear from cursor to beginning of the line. If n is two, clear entire line. Cursor position does not change.CSI n SSU – Scroll UpScroll whole page up by n (default 1) lines. New lines are added at the bottom. (not ANSI.SYS)CSI n TSD – Scroll DownScroll whole page down by n (default 1) lines. New lines are added at the top. (not ANSI.SYS)CSI n ; m fHVP – Horizontal and Vertical PositionMoves the cursor to row n, column m. Both default to 1 if omitted. Same as CUPCSI n [;k] mSGR – Select Graphic RenditionSets SGR parameters, including text color. After CSI can be zero or more parameters separated with ;. With no parameters, CSI m is treated as CSI 0 m (reset / normal), which is typical of most of the ANSI escape sequences.CSI 6 nDSR – Device Status ReportReports the cursor position to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and mis the column. (May not work on MS-DOS.)CSI sSCP – Save Cursor PositionSaves the cursor position.CSI uRCP – Restore Cursor PositionRestores the cursor position.CSI ?25lDECTCEMHides the cursor. (Note: the trailing character is lowercase L.)CSI ?25hDECTCEMShows the cursor.SGR (Select Graphic Rendition) parametersCodeEffectNote0Reset / Normalall attributes off1Bright (increased intensity) or Bold
2Faint (decreased intensity)not widely supported3Italic: onnot widely supported. Sometimes treated as inverse.4Underline: Single
5Blink: Slowless than 150 per minute6Blink: RapidMS-DOS ANSI.SYS; 150 per minute or more; not widely supported7Image: Negativeinverse or reverse; swap foreground and background8Concealnot widely supported9Crossed-outCharacters legible, but marked for deletion. Not widely supported.10Primary(default) font
11–19n-th alternate fontSelect the n-th alternate font. 14 being the fourth alternate font, up to 19 being the 9th alternate font.20Frakturhardly ever supported21Bright/Bold: off or Underline: Doublebold off not widely supported, double underline hardly ever22Normal color or intensityneither bright, bold nor faint23Not italic, not Fraktur
24Underline: Nonenot singly or doubly underlined25Blink: off
26Reserved
27Image: Positive
28Revealconceal off29Not crossed out
30–37Set text color30 + x, where x is from the color table below38Set xterm-256 text color[dubious ]next arguments are 5;x where x is color index (0..255)39Default text colorimplementation defined (according to standard)40–47Set background color40 + x, where x is from the color table below48Set xterm-256 background colornext arguments are 5;x where x is color index (0..255)49Default background colorimplementation defined (according to standard)50Reserved
51Framed
52Encircled
53Overlined
54Not framed or encircled
55Not overlined
56–59Reserved
60ideogram underline or right side linehardly ever supported61ideogram double underline or double line on the right sidehardly ever supported62ideogram overline or left side linehardly ever supported63ideogram double overline or double line on the left sidehardly ever supported64ideogram stress markinghardly ever supported90–99Set foreground color, high intensityaixterm (not in standard)100–109Set background color, high intensityaixterm (not in standard)常用颜色设置如下:




参考:

1.http://www.cnblogs.com/shipfi/articles/375430.html

2.http://en.wikipedia.org/wiki/ANSI_escape_code#Windows_and_DOS