📄 colorizing.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Colorizing Scripts</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Miscellany"HREF="miscellany.html"><LINKREL="PREVIOUS"TITLE="A script calling itself (recursion)"HREF="recursionsct.html"><LINKREL="NEXT"TITLE="Optimizations"HREF="optimizations.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="recursionsct.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 33. Miscellany</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="optimizations.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="COLORIZING"></A>33.6. <SPANCLASS="QUOTE">"Colorizing"</SPAN> Scripts</H1><P><ANAME="COLORIZINGREF"></A></P><P>The ANSI <ANAME="AEN18700"HREF="#FTN.AEN18700">[1]</A> escape sequences set screen attributes, such as bold text, and color of foreground and background. <AHREF="dosbatch.html#DOSBATCH1">DOS batch files</A> commonly used ANSI escape codes for <SPANCLASS="emphasis"><ICLASS="EMPHASIS">color</I></SPAN> output, and so can Bash scripts.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX30A"></A><P><B>Example 33-11. A <SPANCLASS="QUOTE">"colorized"</SPAN> address database</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 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</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="DRAW-BOX"></A><P><B>Example 33-12. Drawing a box</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Draw-box.sh: Drawing a box using ASCII characters. 3 4 # Script by Stefano Palmeri, with minor editing by document author. 5 # Minor edits suggested by Jim Angstadt. 6 # Used in the "ABS Guide" with permission. 7 8 9 ###################################################################### 10 ### draw_box function doc ### 11 12 # The "draw_box" function lets the user 13 #+ draw a box into a terminal. 14 # 15 # Usage: draw_box ROW COLUMN HEIGHT WIDTH [COLOR] 16 # ROW and COLUMN represent the position 17 #+ of the upper left angle of the box you're going to draw. 18 # ROW and COLUMN must be greater than 0 19 #+ and less than current terminal dimension. 20 # HEIGHT is the number of rows of the box, and must be > 0. 21 # HEIGHT + ROW must be <= than current terminal height. 22 # WIDTH is the number of columns of the box and must be > 0. 23 # WIDTH + COLUMN must be <= than current terminal width. 24 # 25 # E.g.: If your terminal dimension is 20x80, 26 # draw_box 2 3 10 45 is good 27 # draw_box 2 3 19 45 has bad HEIGHT value (19+2 > 20) 28 # draw_box 2 3 18 78 has bad WIDTH value (78+3 > 80) 29 # 30 # COLOR is the color of the box frame. 31 # This is the 5th argument and is optional. 32 # 0=black 1=red 2=green 3=tan 4=blue 5=purple 6=cyan 7=white. 33 # If you pass the function bad arguments, 34 #+ it will just exit with code 65, 35 #+ and no messages will be printed on stderr. 36 # 37 # Clear the terminal before you start to draw a box. 38 # The clear command is not contained within the function. 39 # This allows the user to draw multiple boxes, even overlapping ones. 40 41 ### end of draw_box function doc ### 42 ###################################################################### 43 44 draw_box(){ 45 46 #=============# 47 HORZ="-" 48 VERT="|" 49 CORNER_CHAR="+" 50 51 MINARGS=4 52 E_BADARGS=65 53 #=============# 54 55 56 if [ $# -lt "$MINARGS" ]; then # If args are less than 4, exit. 57 exit $E_BADARGS 58 fi 59 60 # Looking for non digit chars in arguments. 61 # Probably it could be done better (exercise for the reader?). 62 if echo $@ | tr -d [:blank:] | tr -d [:digit:] | grep . &> /dev/null; then 63 exit $E_BADARGS 64 fi 65 66 BOX_HEIGHT=`expr $3 - 1` # -1 correction needed because angle char "+" is 67 BOX_WIDTH=`expr $4 - 1` #+ a part of both box height and width. 68 T_ROWS=`tput lines` # Define current terminal dimension 69 T_COLS=`tput cols` #+ in rows and columns. 70 71 if [ $1 -lt 1 ] || [ $1 -gt $T_ROWS ]; then # Start checking if arguments 72 exit $E_BADARGS #+ are correct. 73 fi 74 if [ $2 -lt 1 ] || [ $2 -gt $T_COLS ]; then 75 exit $E_BADARGS 76 fi 77 if [ `expr $1 + $BOX_HEIGHT + 1` -gt $T_ROWS ]; then 78 exit $E_BADARGS 79 fi 80 if [ `expr $2 + $BOX_WIDTH + 1` -gt $T_COLS ]; then 81 exit $E_BADARGS 82 fi 83 if [ $3 -lt 1 ] || [ $4 -lt 1 ]; then 84 exit $E_BADARGS 85 fi # End checking arguments. 86 87 plot_char(){ # Function within a function. 88 echo -e "\E[${1};${2}H"$3 89 } 90 91 echo -ne "\E[3${5}m" # Set box frame color, if defined. 92 93 # start drawing the box 94 95 count=1 # Draw vertical lines using 96 for (( r=$1; count<=$BOX_HEIGHT; r++)); do #+ plot_char function. 97 plot_char $r $2 $VERT 98 let count=count+1 99 done 100 101 count=1 102 c=`expr $2 + $BOX_WIDTH` 103 for (( r=$1; count<=$BOX_HEIGHT; r++)); do 104 plot_char $r $c $VERT 105 let count=count+1 106 done 107 108 count=1 # Draw horizontal lines using 109 for (( c=$2; count<=$BOX_WIDTH; c++)); do #+ plot_char function. 110 plot_char $1 $c $HORZ 111 let count=count+1 112 done 113 114 count=1 115 r=`expr $1 + $BOX_HEIGHT` 116 for (( c=$2; count<=$BOX_WIDTH; c++)); do 117 plot_char $r $c $HORZ 118 let count=count+1 119 done 120 121 plot_char $1 $2 $CORNER_CHAR # Draw box angles. 122 plot_char $1 `expr $2 + $BOX_WIDTH` $CORNER_CHAR 123 plot_char `expr $1 + $BOX_HEIGHT` $2 $CORNER_CHAR 124 plot_char `expr $1 + $BOX_HEIGHT` `expr $2 + $BOX_WIDTH` $CORNER_CHAR 125 126 echo -ne "\E[0m" # Restore old colors. 127 128 P_ROWS=`expr $T_ROWS - 1` # Put the prompt at bottom of the terminal. 129 130 echo -e "\E[${P_ROWS};1H" 131 } 132 133 134 # Now, let's try drawing a box. 135 clear # Clear the terminal. 136 R=2 # Row 137 C=3 # Column 138 H=10 # Height 139 W=45 # Width 140 col=1 # Color (red) 141 draw_box $R $C $H $W $col # Draw the box. 142 143 exit 0 144 145 # Exercise: 146 # -------- 147 # Add the option of printing text within the drawn box.</PRE></TD></TR></TABLE><HR></DIV><P>The simplest, and perhaps most useful ANSI escape sequence is bold text, <BCLASS="COMMAND">\033[1m ... \033[0m</B>. The <SPANCLASS="TOKEN">\033</SPAN> represents an <AHREF="escapingsection.html#ESCP">escape</A>, the <SPANCLASS="QUOTE">"[1"</SPAN> turns on the bold attribute, while the <SPANCLASS="QUOTE">"[0"</SPAN> switches it off. The <SPANCLASS="QUOTE">"m"</SPAN> terminates each term of the escape sequence. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e "\033[1mThis is bold text.\033[0m"</B></TT> </PRE></TD></TR></TABLE> </P><P>A similar escape sequence switches on the underline attribute (on an <ICLASS="FIRSTTERM">rxvt</I> and an <ICLASS="FIRSTTERM">aterm</I>). <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e "\033[4mThis is underlined text.\033[0m"</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>With an <BCLASS="COMMAND">echo</B>, the <TTCLASS="OPTION">-e</TT> option enables the escape sequences.</P></TD></TR></TABLE></DIV><P>Other escape sequences change the text and/or background color.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -