📄 colorizing.html
字号:
><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e '\E[34;47mThis prints in blue.'; tput sgr0</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e '\E[1;33;44m'"BOLD yellow text on blue background"; tput sgr0</B></TT> </PRE></TD></TR></TABLE> </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>It's usually advisable to set the <ICLASS="FIRSTTERM">bold</I> attribute for light-colored foreground text.</P></TD></TR></TABLE></DIV><P>The <BCLASS="COMMAND">tput sgr0</B> restores the terminal settings to normal. Omitting this lets all subsequent output from that particular terminal remain blue.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Since <BCLASS="COMMAND">tput sgr0</B> fails to restore terminal settings under certain circumstances, <BCLASS="COMMAND">echo -ne \E[0m</B> may be a better choice.</P></TD></TR></TABLE></DIV><P><ANAME="COLORIZTEMPL"></A></P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN18752"></A><P>Use the following template for writing colored text on a colored background.</P><P> <TTCLASS="USERINPUT"><B>echo -e '\E[COLOR1;COLOR2mSome text goes here.'</B></TT> </P><P>The <SPANCLASS="QUOTE">"\E["</SPAN> begins the escape sequence. The semicolon-separated numbers <SPANCLASS="QUOTE">"COLOR1"</SPAN> and <SPANCLASS="QUOTE">"COLOR2"</SPAN> 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 <SPANCLASS="QUOTE">"m"</SPAN> terminates the escape sequence, and the text begins immediately after that.</P><P>Note also that <AHREF="variables.html#SNGLQUO">single quotes</A> enclose the remainder of the command sequence following the <BCLASS="COMMAND">echo -e</B>.</P></DIV></TD></TR></TABLE><P>The numbers in the following table work for an <ICLASS="FIRSTTERM">rxvt</I> terminal. Results may vary for other terminal emulators.</P><P><ANAME="COLORIZTABLE"></A></P><DIVCLASS="TABLE"><HR><ANAME="AEN18768"></A><P><B>Table 33-2. Numbers representing colors in Escape Sequences</B></P><TABLEBORDER="1"CLASS="CALSTABLE"><THEAD><TR><THALIGN="LEFT"VALIGN="TOP">Color</TH><THALIGN="LEFT"VALIGN="TOP">Foreground</TH><THALIGN="LEFT"VALIGN="TOP">Background</TH></TR></THEAD><TBODY><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">black</TT></TD><TDALIGN="LEFT"VALIGN="TOP">30</TD><TDALIGN="LEFT"VALIGN="TOP">40</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">red</TT></TD><TDALIGN="LEFT"VALIGN="TOP">31</TD><TDALIGN="LEFT"VALIGN="TOP">41</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">green</TT></TD><TDALIGN="LEFT"VALIGN="TOP">32</TD><TDALIGN="LEFT"VALIGN="TOP">42</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">yellow</TT></TD><TDALIGN="LEFT"VALIGN="TOP">33</TD><TDALIGN="LEFT"VALIGN="TOP">43</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">blue</TT></TD><TDALIGN="LEFT"VALIGN="TOP">34</TD><TDALIGN="LEFT"VALIGN="TOP">44</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">magenta</TT></TD><TDALIGN="LEFT"VALIGN="TOP">35</TD><TDALIGN="LEFT"VALIGN="TOP">45</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">cyan</TT></TD><TDALIGN="LEFT"VALIGN="TOP">36</TD><TDALIGN="LEFT"VALIGN="TOP">46</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">white</TT></TD><TDALIGN="LEFT"VALIGN="TOP">37</TD><TDALIGN="LEFT"VALIGN="TOP">47</TD></TR></TBODY></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="COLORECHO"></A><P><B>Example 33-13. Echoing colored text</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 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.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="HORSERACEREF"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="HORSERACE"></A><P><B>Example 33-14. A <SPANCLASS="QUOTE">"horserace"</SPAN> game</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # horserace.sh: Very simple horserace simulation. 3 # Author: Stefano Palmeri 4 # Used with permission. 5 6 ################################################################ 7 # Goals of the script: 8 # playing with escape sequences and terminal colors. 9 # 10 # Exercise: 11 # Edit the script to make it run less randomly, 12 #+ set up a fake betting shop . . . 13 # Um . . . um . . . it's starting to remind me of a movie . . . 14 # 15 # The script gives each horse a random handicap. 16 # The odds are calculated upon horse handicap 17 #+ and are expressed in European(?) style. 18 # E.g., odds=3.75 means that if you bet $1 and win, 19 #+ you receive $3.75. 20 # 21 # The script has been tested with a GNU/Linux OS, 22 #+ using xterm and rxvt, and konsole. 23 # On a machine with an AMD 900 MHz processor, 24 #+ the average race time is 75 seconds. 25 # On faster computers the race time would be lower. 26 # So, if you want more suspense, reset the USLEEP_ARG variable. 27 # 28 # Script by Stefano Palmeri. 29 ################################################################ 30 31 E_RUNERR=65 32 33 # Check if md5sum and bc are installed. 34 if ! which bc &> /dev/null; then 35 echo bc is not installed. 36 echo "Can\'t run . . . " 37 exit $E_RUNERR 38 fi 39 if ! which md5sum &> /dev/null; then 40 echo md5sum is not installed. 41 echo "Can\'t run . . . " 42 exit $E_RUNERR 43 fi 44 45 # Set the following variable to slow down script execution. 46 # It will be passed as the argument for usleep (man usleep) 47 #+ and is expressed in microseconds (500000 = half a second). 48 USLEEP_ARG=0 49 50 # Clean up the temp directory, restore terminal cursor and 51 #+ terminal colors -- if script interrupted by Ctl-C. 52 trap 'echo -en "\E[?25h"; echo -en "\E[0m"; stty echo;\ 53 tput cup 20 0; rm -fr $HORSE_RACE_TMP_DIR' TERM EXIT 54 # See the chapter on debugging for an explanation of 'trap.' 55
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -