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

📄 colorizing.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
><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&nbsp;#!/bin/bash   2&nbsp;# color-echo.sh: Echoing text messages in color.   3&nbsp;   4&nbsp;# Modify this script for your own purposes.   5&nbsp;# It's easier than hand-coding color.   6&nbsp;   7&nbsp;black='\E[30;47m'   8&nbsp;red='\E[31;47m'   9&nbsp;green='\E[32;47m'  10&nbsp;yellow='\E[33;47m'  11&nbsp;blue='\E[34;47m'  12&nbsp;magenta='\E[35;47m'  13&nbsp;cyan='\E[36;47m'  14&nbsp;white='\E[37;47m'  15&nbsp;  16&nbsp;  17&nbsp;alias Reset="tput sgr0"      #  Reset text attributes to normal  18&nbsp;                             #+ without clearing screen.  19&nbsp;  20&nbsp;  21&nbsp;cecho ()                     # Color-echo.  22&nbsp;                             # Argument $1 = message  23&nbsp;                             # Argument $2 = color  24&nbsp;{  25&nbsp;local default_msg="No message passed."  26&nbsp;                             # Doesn't really need to be a local variable.  27&nbsp;  28&nbsp;message=${1:-$default_msg}   # Defaults to default message.  29&nbsp;color=${2:-$black}           # Defaults to black, if not specified.  30&nbsp;  31&nbsp;  echo -e "$color"  32&nbsp;  echo "$message"  33&nbsp;  Reset                      # Reset to normal.  34&nbsp;  35&nbsp;  return  36&nbsp;}    37&nbsp;  38&nbsp;  39&nbsp;# Now, let's try it out.  40&nbsp;# ----------------------------------------------------  41&nbsp;cecho "Feeling blue..." $blue  42&nbsp;cecho "Magenta looks more like purple." $magenta  43&nbsp;cecho "Green with envy." $green  44&nbsp;cecho "Seeing red?" $red  45&nbsp;cecho "Cyan, more familiarly known as aqua." $cyan  46&nbsp;cecho "No color passed (defaults to black)."  47&nbsp;       # Missing $color argument.  48&nbsp;cecho "\"Empty\" color passed (defaults to black)." ""  49&nbsp;       # Empty $color argument.  50&nbsp;cecho  51&nbsp;       # Missing $message and $color arguments.  52&nbsp;cecho "" ""  53&nbsp;       # Empty $message and $color arguments.  54&nbsp;# ----------------------------------------------------  55&nbsp;  56&nbsp;echo  57&nbsp;  58&nbsp;exit 0  59&nbsp;  60&nbsp;# Exercises:  61&nbsp;# ---------  62&nbsp;# 1) Add the "bold" attribute to the 'cecho ()' function.  63&nbsp;# 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&nbsp;#!/bin/bash   2&nbsp;# horserace.sh: Very simple horserace simulation.   3&nbsp;# Author: Stefano Palmeri   4&nbsp;# Used with permission.   5&nbsp;   6&nbsp;################################################################   7&nbsp;#  Goals of the script:   8&nbsp;#  playing with escape sequences and terminal colors.   9&nbsp;#  10&nbsp;#  Exercise:  11&nbsp;#  Edit the script to make it run less randomly,  12&nbsp;#+ set up a fake betting shop . . .       13&nbsp;#  Um . . . um . . . it's starting to remind me of a movie . . .  14&nbsp;#  15&nbsp;#  The script gives each horse a random handicap.  16&nbsp;#  The odds are calculated upon horse handicap  17&nbsp;#+ and are expressed in European(?) style.  18&nbsp;#  E.g., odds=3.75 means that if you bet $1 and win,  19&nbsp;#+ you receive $3.75.  20&nbsp;#   21&nbsp;#  The script has been tested with a GNU/Linux OS,  22&nbsp;#+ using xterm and rxvt, and konsole.  23&nbsp;#  On a machine with an AMD 900 MHz processor,  24&nbsp;#+ the average race time is 75 seconds.      25&nbsp;#  On faster computers the race time would be lower.  26&nbsp;#  So, if you want more suspense, reset the USLEEP_ARG variable.  27&nbsp;#  28&nbsp;#  Script by Stefano Palmeri.  29&nbsp;################################################################  30&nbsp;  31&nbsp;E_RUNERR=65  32&nbsp;  33&nbsp;# Check if md5sum and bc are installed.   34&nbsp;if ! which bc &#38;&#62; /dev/null; then  35&nbsp;   echo bc is not installed.    36&nbsp;   echo "Can\'t run . . . "  37&nbsp;   exit $E_RUNERR  38&nbsp;fi  39&nbsp;if ! which md5sum &#38;&#62; /dev/null; then  40&nbsp;   echo md5sum is not installed.    41&nbsp;   echo "Can\'t run . . . "  42&nbsp;   exit $E_RUNERR  43&nbsp;fi  44&nbsp;  45&nbsp;#  Set the following variable to slow down script execution.  46&nbsp;#  It will be passed as the argument for usleep (man usleep)    47&nbsp;#+ and is expressed in microseconds (500000 = half a second).  48&nbsp;USLEEP_ARG=0    49&nbsp;  50&nbsp;#  Clean up the temp directory, restore terminal cursor and   51&nbsp;#+ terminal colors -- if script interrupted by Ctl-C.  52&nbsp;trap 'echo -en "\E[?25h"; echo -en "\E[0m"; stty echo;\  53&nbsp;tput cup 20 0; rm -fr  $HORSE_RACE_TMP_DIR'  TERM EXIT  54&nbsp;#  See the chapter on debugging for an explanation of 'trap.'  55&nbsp;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -