⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorizing.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!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&nbsp;#!/bin/bash   2&nbsp;# ex30a.sh: "Colorized" version of ex30.sh.   3&nbsp;#            Crude address database   4&nbsp;   5&nbsp;   6&nbsp;clear                                   # Clear the screen.   7&nbsp;   8&nbsp;echo -n "          "   9&nbsp;echo -e '\E[37;44m'"\033[1mContact List\033[0m"  10&nbsp;                                        # White on blue background  11&nbsp;echo; echo  12&nbsp;echo -e "\033[1mChoose one of the following persons:\033[0m"  13&nbsp;                                        # Bold  14&nbsp;tput sgr0  15&nbsp;echo "(Enter only the first letter of name.)"  16&nbsp;echo  17&nbsp;echo -en '\E[47;34m'"\033[1mE\033[0m"   # Blue  18&nbsp;tput sgr0                               # Reset colors to "normal."  19&nbsp;echo "vans, Roland"                     # "[E]vans, Roland"  20&nbsp;echo -en '\E[47;35m'"\033[1mJ\033[0m"   # Magenta  21&nbsp;tput sgr0  22&nbsp;echo "ones, Mildred"  23&nbsp;echo -en '\E[47;32m'"\033[1mS\033[0m"   # Green  24&nbsp;tput sgr0  25&nbsp;echo "mith, Julie"  26&nbsp;echo -en '\E[47;31m'"\033[1mZ\033[0m"   # Red  27&nbsp;tput sgr0  28&nbsp;echo "ane, Morris"  29&nbsp;echo  30&nbsp;  31&nbsp;read person  32&nbsp;  33&nbsp;case "$person" in  34&nbsp;# Note variable is quoted.  35&nbsp;  36&nbsp;  "E" | "e" )  37&nbsp;  # Accept upper or lowercase input.  38&nbsp;  echo  39&nbsp;  echo "Roland Evans"  40&nbsp;  echo "4321 Floppy Dr."  41&nbsp;  echo "Hardscrabble, CO 80753"  42&nbsp;  echo "(303) 734-9874"  43&nbsp;  echo "(303) 734-9892 fax"  44&nbsp;  echo "revans@zzy.net"  45&nbsp;  echo "Business partner &#38; old friend"  46&nbsp;  ;;  47&nbsp;  48&nbsp;  "J" | "j" )  49&nbsp;  echo  50&nbsp;  echo "Mildred Jones"  51&nbsp;  echo "249 E. 7th St., Apt. 19"  52&nbsp;  echo "New York, NY 10009"  53&nbsp;  echo "(212) 533-2814"  54&nbsp;  echo "(212) 533-9972 fax"  55&nbsp;  echo "milliej@loisaida.com"  56&nbsp;  echo "Girlfriend"  57&nbsp;  echo "Birthday: Feb. 11"  58&nbsp;  ;;  59&nbsp;  60&nbsp;# Add info for Smith &#38; Zane later.  61&nbsp;  62&nbsp;          * )  63&nbsp;   # Default option.	    64&nbsp;   # Empty input (hitting RETURN) fits here, too.  65&nbsp;   echo  66&nbsp;   echo "Not yet in database."  67&nbsp;  ;;  68&nbsp;  69&nbsp;esac  70&nbsp;  71&nbsp;tput sgr0                               # Reset colors to "normal."  72&nbsp;  73&nbsp;echo  74&nbsp;  75&nbsp;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&nbsp;#!/bin/bash   2&nbsp;# Draw-box.sh: Drawing a box using ASCII characters.   3&nbsp;   4&nbsp;# Script by Stefano Palmeri, with minor editing by document author.   5&nbsp;# Minor edits suggested by Jim Angstadt.   6&nbsp;# Used in the "ABS Guide" with permission.   7&nbsp;   8&nbsp;   9&nbsp;######################################################################  10&nbsp;###  draw_box function doc  ###  11&nbsp;  12&nbsp;#  The "draw_box" function lets the user  13&nbsp;#+ draw a box into a terminal.         14&nbsp;#  15&nbsp;#  Usage: draw_box ROW COLUMN HEIGHT WIDTH [COLOR]   16&nbsp;#  ROW and COLUMN represent the position          17&nbsp;#+ of the upper left angle of the box you're going to draw.  18&nbsp;#  ROW and COLUMN must be greater than 0  19&nbsp;#+ and less than current terminal dimension.  20&nbsp;#  HEIGHT is the number of rows of the box, and must be &#62; 0.   21&nbsp;#  HEIGHT + ROW must be &#60;= than current terminal height.   22&nbsp;#  WIDTH is the number of columns of the box and must be &#62; 0.  23&nbsp;#  WIDTH + COLUMN must be &#60;= than current terminal width.  24&nbsp;#  25&nbsp;# E.g.: If your terminal dimension is 20x80,  26&nbsp;#  draw_box 2 3 10 45 is good  27&nbsp;#  draw_box 2 3 19 45 has bad HEIGHT value (19+2 &#62; 20)  28&nbsp;#  draw_box 2 3 18 78 has bad WIDTH value (78+3 &#62; 80)  29&nbsp;#  30&nbsp;#  COLOR is the color of the box frame.  31&nbsp;#  This is the 5th argument and is optional.  32&nbsp;#  0=black 1=red 2=green 3=tan 4=blue 5=purple 6=cyan 7=white.  33&nbsp;#  If you pass the function bad arguments,  34&nbsp;#+ it will just exit with code 65,  35&nbsp;#+ and no messages will be printed on stderr.  36&nbsp;#  37&nbsp;#  Clear the terminal before you start to draw a box.  38&nbsp;#  The clear command is not contained within the function.  39&nbsp;#  This allows the user to draw multiple boxes, even overlapping ones.  40&nbsp;  41&nbsp;###  end of draw_box function doc  ###   42&nbsp;######################################################################  43&nbsp;  44&nbsp;draw_box(){  45&nbsp;  46&nbsp;#=============#  47&nbsp;HORZ="-"  48&nbsp;VERT="|"  49&nbsp;CORNER_CHAR="+"  50&nbsp;  51&nbsp;MINARGS=4  52&nbsp;E_BADARGS=65  53&nbsp;#=============#  54&nbsp;  55&nbsp;  56&nbsp;if [ $# -lt "$MINARGS" ]; then                 # If args are less than 4, exit.  57&nbsp;    exit $E_BADARGS  58&nbsp;fi  59&nbsp;  60&nbsp;# Looking for non digit chars in arguments.  61&nbsp;# Probably it could be done better (exercise for the reader?).  62&nbsp;if echo $@ | tr -d [:blank:] | tr -d [:digit:] | grep . &#38;&#62; /dev/null; then  63&nbsp;   exit $E_BADARGS  64&nbsp;fi  65&nbsp;  66&nbsp;BOX_HEIGHT=`expr $3 - 1`   #  -1 correction needed because angle char "+" is   67&nbsp;BOX_WIDTH=`expr $4 - 1`    #+ a part of both box height and width.  68&nbsp;T_ROWS=`tput lines`        #  Define current terminal dimension   69&nbsp;T_COLS=`tput cols`         #+ in rows and columns.  70&nbsp;           71&nbsp;if [ $1 -lt 1 ] || [ $1 -gt $T_ROWS ]; then    #  Start checking if arguments  72&nbsp;   exit $E_BADARGS                             #+ are correct.  73&nbsp;fi  74&nbsp;if [ $2 -lt 1 ] || [ $2 -gt $T_COLS ]; then  75&nbsp;   exit $E_BADARGS  76&nbsp;fi  77&nbsp;if [ `expr $1 + $BOX_HEIGHT + 1` -gt $T_ROWS ]; then  78&nbsp;   exit $E_BADARGS  79&nbsp;fi  80&nbsp;if [ `expr $2 + $BOX_WIDTH + 1` -gt $T_COLS ]; then  81&nbsp;   exit $E_BADARGS  82&nbsp;fi  83&nbsp;if [ $3 -lt 1 ] || [ $4 -lt 1 ]; then  84&nbsp;   exit $E_BADARGS  85&nbsp;fi                                 # End checking arguments.  86&nbsp;  87&nbsp;plot_char(){                       # Function within a function.  88&nbsp;   echo -e "\E[${1};${2}H"$3  89&nbsp;}  90&nbsp;  91&nbsp;echo -ne "\E[3${5}m"               # Set box frame color, if defined.  92&nbsp;  93&nbsp;# start drawing the box  94&nbsp;  95&nbsp;count=1                                         #  Draw vertical lines using  96&nbsp;for (( r=$1; count&#60;=$BOX_HEIGHT; r++)); do      #+ plot_char function.  97&nbsp;  plot_char $r $2 $VERT  98&nbsp;  let count=count+1  99&nbsp;done  100&nbsp; 101&nbsp;count=1 102&nbsp;c=`expr $2 + $BOX_WIDTH` 103&nbsp;for (( r=$1; count&#60;=$BOX_HEIGHT; r++)); do 104&nbsp;  plot_char $r $c $VERT 105&nbsp;  let count=count+1 106&nbsp;done  107&nbsp; 108&nbsp;count=1                                        #  Draw horizontal lines using 109&nbsp;for (( c=$2; count&#60;=$BOX_WIDTH; c++)); do      #+ plot_char function. 110&nbsp;  plot_char $1 $c $HORZ 111&nbsp;  let count=count+1 112&nbsp;done  113&nbsp; 114&nbsp;count=1 115&nbsp;r=`expr $1 + $BOX_HEIGHT` 116&nbsp;for (( c=$2; count&#60;=$BOX_WIDTH; c++)); do 117&nbsp;  plot_char $r $c $HORZ 118&nbsp;  let count=count+1 119&nbsp;done  120&nbsp; 121&nbsp;plot_char $1 $2 $CORNER_CHAR                   # Draw box angles. 122&nbsp;plot_char $1 `expr $2 + $BOX_WIDTH` $CORNER_CHAR 123&nbsp;plot_char `expr $1 + $BOX_HEIGHT` $2 $CORNER_CHAR 124&nbsp;plot_char `expr $1 + $BOX_HEIGHT` `expr $2 + $BOX_WIDTH` $CORNER_CHAR 125&nbsp; 126&nbsp;echo -ne "\E[0m"             #  Restore old colors. 127&nbsp; 128&nbsp;P_ROWS=`expr $T_ROWS - 1`    #  Put the prompt at bottom of the terminal. 129&nbsp; 130&nbsp;echo -e "\E[${P_ROWS};1H" 131&nbsp;}       132&nbsp; 133&nbsp; 134&nbsp;# Now, let's try drawing a box. 135&nbsp;clear                       # Clear the terminal. 136&nbsp;R=2      # Row 137&nbsp;C=3      # Column 138&nbsp;H=10     # Height 139&nbsp;W=45     # Width  140&nbsp;col=1    # Color (red) 141&nbsp;draw_box $R $C $H $W $col   # Draw the box. 142&nbsp; 143&nbsp;exit 0 144&nbsp; 145&nbsp;# Exercise: 146&nbsp;# -------- 147&nbsp;# 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 + -