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

📄 colorizing.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  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;  56&nbsp;# Set a unique (paranoid) name for the temp directory the script needs.  57&nbsp;HORSE_RACE_TMP_DIR=$HOME/.horserace-`date +%s`-`head -c10 /dev/urandom | md5sum | head -c30`  58&nbsp;  59&nbsp;# Create the temp directory and move right in.  60&nbsp;mkdir $HORSE_RACE_TMP_DIR  61&nbsp;cd $HORSE_RACE_TMP_DIR  62&nbsp;  63&nbsp;  64&nbsp;#  This function moves the cursor to line $1 column $2 and then prints $3.  65&nbsp;#  E.g.: "move_and_echo 5 10 linux" is equivalent to  66&nbsp;#+ "tput cup 4 9; echo linux", but with one command instead of two.  67&nbsp;#  Note: "tput cup" defines 0 0 the upper left angle of the terminal,  68&nbsp;#+ echo defines 1 1 the upper left angle of the terminal.  69&nbsp;move_and_echo() {  70&nbsp;          echo -ne "\E[${1};${2}H""$3"   71&nbsp;}  72&nbsp;  73&nbsp;# Function to generate a pseudo-random number between 1 and 9.   74&nbsp;random_1_9 () {  75&nbsp;                head -c10 /dev/urandom | md5sum | tr -d [a-z] | tr -d 0 | cut -c1   76&nbsp;}  77&nbsp;  78&nbsp;#  Two functions that simulate "movement," when drawing the horses.   79&nbsp;draw_horse_one() {  80&nbsp;               echo -n " "//$MOVE_HORSE//  81&nbsp;}  82&nbsp;draw_horse_two(){  83&nbsp;              echo -n " "\\\\$MOVE_HORSE\\\\   84&nbsp;}     85&nbsp;  86&nbsp;  87&nbsp;# Define current terminal dimension.  88&nbsp;N_COLS=`tput cols`  89&nbsp;N_LINES=`tput lines`  90&nbsp;  91&nbsp;# Need at least a 20-LINES X 80-COLUMNS terminal. Check it.  92&nbsp;if [ $N_COLS -lt 80 ] || [ $N_LINES -lt 20 ]; then  93&nbsp;   echo "`basename $0` needs a 80-cols X 20-lines terminal."  94&nbsp;   echo "Your terminal is ${N_COLS}-cols X ${N_LINES}-lines."  95&nbsp;   exit $E_RUNERR  96&nbsp;fi  97&nbsp;  98&nbsp;  99&nbsp;# Start drawing the race field. 100&nbsp; 101&nbsp;# Need a string of 80 chars. See below. 102&nbsp;BLANK80=`seq -s "" 100 | head -c80` 103&nbsp; 104&nbsp;clear 105&nbsp; 106&nbsp;# Set foreground and background colors to white. 107&nbsp;echo -ne '\E[37;47m' 108&nbsp; 109&nbsp;# Move the cursor on the upper left angle of the terminal. 110&nbsp;tput cup 0 0  111&nbsp; 112&nbsp;# Draw six white lines. 113&nbsp;for n in `seq 5`; do 114&nbsp;      echo $BLANK80        # Use the 80 chars string to colorize the terminal.   115&nbsp;done 116&nbsp; 117&nbsp;# Sets foreground color to black.  118&nbsp;echo -ne '\E[30m' 119&nbsp; 120&nbsp;move_and_echo 3 1 "START  1"             121&nbsp;move_and_echo 3 75 FINISH 122&nbsp;move_and_echo 1 5 "|" 123&nbsp;move_and_echo 1 80 "|" 124&nbsp;move_and_echo 2 5 "|" 125&nbsp;move_and_echo 2 80 "|" 126&nbsp;move_and_echo 4 5 "|  2" 127&nbsp;move_and_echo 4 80 "|" 128&nbsp;move_and_echo 5 5 "V  3" 129&nbsp;move_and_echo 5 80 "V" 130&nbsp; 131&nbsp;# Set foreground color to red.  132&nbsp;echo -ne '\E[31m' 133&nbsp; 134&nbsp;# Some ASCII art. 135&nbsp;move_and_echo 1 8 "..@@@..@@@@@...@@@@@.@...@..@@@@..." 136&nbsp;move_and_echo 2 8 ".@...@...@.......@...@...@.@......." 137&nbsp;move_and_echo 3 8 ".@@@@@...@.......@...@@@@@.@@@@...." 138&nbsp;move_and_echo 4 8 ".@...@...@.......@...@...@.@......." 139&nbsp;move_and_echo 5 8 ".@...@...@.......@...@...@..@@@@..." 140&nbsp;move_and_echo 1 43 "@@@@...@@@...@@@@..@@@@..@@@@." 141&nbsp;move_and_echo 2 43 "@...@.@...@.@.....@.....@....." 142&nbsp;move_and_echo 3 43 "@@@@..@@@@@.@.....@@@@...@@@.." 143&nbsp;move_and_echo 4 43 "@..@..@...@.@.....@.........@." 144&nbsp;move_and_echo 5 43 "@...@.@...@..@@@@..@@@@.@@@@.." 145&nbsp; 146&nbsp; 147&nbsp;# Set foreground and background colors to green. 148&nbsp;echo -ne '\E[32;42m' 149&nbsp; 150&nbsp;# Draw  eleven green lines. 151&nbsp;tput cup 5 0 152&nbsp;for n in `seq 11`; do 153&nbsp;      echo $BLANK80 154&nbsp;done 155&nbsp; 156&nbsp;# Set foreground color to black.  157&nbsp;echo -ne '\E[30m' 158&nbsp;tput cup 5 0 159&nbsp; 160&nbsp;# Draw the fences.  161&nbsp;echo "++++++++++++++++++++++++++++++++++++++\ 162&nbsp;++++++++++++++++++++++++++++++++++++++++++" 163&nbsp; 164&nbsp;tput cup 15 0 165&nbsp;echo "++++++++++++++++++++++++++++++++++++++\ 166&nbsp;++++++++++++++++++++++++++++++++++++++++++" 167&nbsp; 168&nbsp;# Set foreground and background colors to white. 169&nbsp;echo -ne '\E[37;47m' 170&nbsp; 171&nbsp;# Draw three white lines. 172&nbsp;for n in `seq 3`; do 173&nbsp;      echo $BLANK80 174&nbsp;done 175&nbsp; 176&nbsp;# Set foreground color to black. 177&nbsp;echo -ne '\E[30m' 178&nbsp; 179&nbsp;# Create 9 files to stores handicaps. 180&nbsp;for n in `seq 10 7 68`; do 181&nbsp;      touch $n 182&nbsp;done   183&nbsp; 184&nbsp;# Set the first type of "horse" the script will draw. 185&nbsp;HORSE_TYPE=2 186&nbsp; 187&nbsp;#  Create position-file and odds-file for every "horse". 188&nbsp;#+ In these files, store the current position of the horse, 189&nbsp;#+ the type and the odds. 190&nbsp;for HN in `seq 9`; do 191&nbsp;      touch horse_${HN}_position 192&nbsp;      touch odds_${HN} 193&nbsp;      echo \-1 &#62; horse_${HN}_position 194&nbsp;      echo $HORSE_TYPE &#62;&#62;  horse_${HN}_position 195&nbsp;      # Define a random handicap for horse. 196&nbsp;       HANDICAP=`random_1_9` 197&nbsp;      # Check if the random_1_9 function returned a good value. 198&nbsp;      while ! echo $HANDICAP | grep [1-9] &#38;&#62; /dev/null; do 199&nbsp;                HANDICAP=`random_1_9` 200&nbsp;      done 201&nbsp;      # Define last handicap position for horse.  202&nbsp;      LHP=`expr $HANDICAP \* 7 + 3` 203&nbsp;      for FILE in `seq 10 7 $LHP`; do 204&nbsp;            echo $HN &#62;&#62; $FILE 205&nbsp;      done    206&nbsp;      207&nbsp;      # Calculate odds. 208&nbsp;      case $HANDICAP in  209&nbsp;              1) ODDS=`echo $HANDICAP \* 0.25 + 1.25 | bc` 210&nbsp;                                 echo $ODDS &#62; odds_${HN} 211&nbsp;              ;; 212&nbsp;              2 | 3) ODDS=`echo $HANDICAP \* 0.40 + 1.25 | bc` 213&nbsp;                                       echo $ODDS &#62; odds_${HN} 214&nbsp;              ;; 215&nbsp;              4 | 5 | 6) ODDS=`echo $HANDICAP \* 0.55 + 1.25 | bc` 216&nbsp;                                             echo $ODDS &#62; odds_${HN} 217&nbsp;              ;;  218&nbsp;              7 | 8) ODDS=`echo $HANDICAP \* 0.75 + 1.25 | bc` 219&nbsp;                                       echo $ODDS &#62; odds_${HN} 220&nbsp;              ;;  221&nbsp;              9) ODDS=`echo $HANDICAP \* 0.90 + 1.25 | bc` 222&nbsp;                                  echo $ODDS &#62; odds_${HN} 223&nbsp;      esac 224&nbsp; 225&nbsp; 226&nbsp;done 227&nbsp; 228&nbsp; 229&nbsp;# Print odds. 230&nbsp;print_odds() { 231&nbsp;tput cup 6 0 232&nbsp;echo -ne '\E[30;42m' 233&nbsp;for HN in `seq 9`; do 234&nbsp;      echo "#$HN odds-&#62;" `cat odds_${HN}` 235&nbsp;done 236&nbsp;} 237&nbsp; 238&nbsp;# Draw the horses at starting line. 239&nbsp;draw_horses() { 240&nbsp;tput cup 6 0 241&nbsp;echo -ne '\E[30;42m' 242&nbsp;for HN in `seq 9`; do 243&nbsp;      echo /\\$HN/\\"                               " 244&nbsp;done 245&nbsp;} 246&nbsp; 247&nbsp;print_odds 248&nbsp; 249&nbsp;echo -ne '\E[47m' 250&nbsp;# Wait for a enter key press to start the race. 251&nbsp;# The escape sequence '\E[?25l' disables the cursor. 252&nbsp;tput cup 17 0 253&nbsp;echo -e '\E[?25l'Press [enter] key to start the race... 254&nbsp;read -s 255&nbsp; 256&nbsp;#  Disable normal echoing in the terminal. 257&nbsp;#  This avoids key presses that might "contaminate" the screen 258&nbsp;#+ during the race.   259&nbsp;stty -echo 260&nbsp; 261&nbsp;# -------------------------------------------------------- 262&nbsp;# Start the race. 263&nbsp; 264&nbsp;draw_horses 265&nbsp;echo -ne '\E[37;47m' 266&nbsp;move_and_echo 18 1 $BLANK80 267&nbsp;echo -ne '\E[30m' 268&nbsp;move_and_echo 18 1 Starting... 269&nbsp;sleep 1 270&nbsp; 271&nbsp;# Set the column of the finish line. 272&nbsp;WINNING_POS=74 273&nbsp; 274&nbsp;# Define the time the race started. 275&nbsp;START_TIME=`date +%s` 276&nbsp; 277&nbsp;# COL variable needed by following "while" construct. 278&nbsp;COL=0     279&nbsp; 280&nbsp;while [ $COL -lt $WINNING_POS ]; do 281&nbsp;                    282&nbsp;          MOVE_HORSE=0      283&nbsp;           284&nbsp;          # Check if the random_1_9 function has returned a good value. 285&nbsp;          while ! echo $MOVE_HORSE | grep [1-9] &#38;&#62; /dev/null; do 286&nbsp;                MOVE_HORSE=`random_1_9` 287&nbsp;          done 288&nbsp;           289&nbsp;          # Define old type and position of the "randomized horse". 290&nbsp;          HORSE_TYPE=`cat  horse_${MOVE_HORSE}_position | tail -1` 291&nbsp;          COL=$(expr `cat  horse_${MOVE_HORSE}_position | head -1`)  292&nbsp;           293&nbsp;          ADD_POS=1 294&nbsp;          # Check if the current position is an handicap position.  295&nbsp;          if seq 10 7 68 | grep -w $COL &#38;&#62; /dev/null; then 296&nbsp;                if grep -w $MOVE_HORSE $COL &#38;&#62; /dev/null; then 297&nbsp;                      ADD_POS=0 298&nbsp;                      grep -v -w  $MOVE_HORSE $COL &#62; ${COL}_new 299&nbsp;                      rm -f $COL 300&nbsp;                      mv -f ${COL}_new $COL 301&nbsp;                      else ADD_POS=1 302&nbsp;                fi  303&nbsp;          else ADD_POS=1 304&nbsp;          fi 305&nbsp;          COL=`expr $COL + $ADD_POS` 306&nbsp;          echo $COL &#62;  horse_${MOVE_HORSE}_position  # Store new position. 307&nbsp;                             308&nbsp;         # Choose the type of horse to draw.          309&nbsp;          case $HORSE_TYPE in  310&nbsp;                1) HORSE_TYPE=2; DRAW_HORSE=draw_horse_two 311&nbsp;                ;; 312&nbsp;                2) HORSE_TYPE=1; DRAW_HORSE=draw_horse_one  313&nbsp;          esac        314&nbsp;          echo $HORSE_TYPE &#62;&#62;  horse_${MOVE_HORSE}_position # Store current type. 315&nbsp;          316&nbsp;          # Set foreground color to black and background to green. 317&nbsp;          echo -ne '\E[30;42m' 318&nbsp;           319&nbsp;          # Move the cursor to new horse position. 320&nbsp;          tput cup `expr $MOVE_HORSE + 5`  `cat  horse_${MOVE_HORSE}_position | head -1`  321&nbsp;           322&nbsp;          # Draw the horse. 323&nbsp;          $DRAW_HORSE 324&nbsp;           usleep $USLEEP_ARG 325&nbsp;           326&nbsp;           # When all horses have gone beyond field line 15, reprint odds.           327&nbsp;           touch fieldline15 328&nbsp;           if [ $COL = 15 ]; then 329&nbsp;             echo $MOVE_HORSE &#62;&#62; fieldline15   330&nbsp;           fi 331&nbsp;           if [ `wc -l fieldline15 | cut -f1 -d " "` = 9 ]; then 332&nbsp;               print_odds 333&nbsp;               : &#62; fieldline15 334&nbsp;           fi            335&nbsp;           336&nbsp;          # Define the leading horse. 337&nbsp;          HIGHEST_POS=`cat *position | sort -n | tail -1`           338&nbsp;           339&nbsp;          # Set background color to white. 340&nbsp;          echo -ne '\E[47m' 341&nbsp;          tput cup 17 0 342&nbsp;          echo -n Current leader: `grep -w $HIGHEST_POS *position | cut -c7`"                              "            343&nbsp; 344&nbsp;done   345&nbsp; 346&nbsp;# Define the time the race finished. 347&nbsp;FINISH_TIME=`date +%s` 348&nbsp; 349&nbsp;# Set background color to green and enable blinking text. 350&nbsp;echo -ne '\E[30;42m' 351&nbsp;echo -en '\E[5m' 352&nbsp; 353&nbsp;# Make the winning horse blink. 354&nbsp;tput cup `expr $MOVE_HORSE + 5` `cat  horse_${MOVE_HORSE}_position | head -1` 355&nbsp;$DRAW_HORSE 356&nbsp; 357&nbsp;# Disable blinking text. 358&nbsp;echo -en '\E[25m' 359&nbsp; 360&nbsp;# Set foreground and background color to white. 361&nbsp;echo -ne '\E[37;47m' 362&nbsp;move_and_echo 18 1 $BLANK80 363&nbsp; 364&nbsp;# Set foreground color to black. 365&nbsp;echo -ne '\E[30m' 366&nbsp; 367&nbsp;# Make winner blink. 368&nbsp;tput cup 17 0 369&nbsp;echo -e "\E[5mWINNER: $MOVE_HORSE\E[25m""  Odds: `cat odds_${MOVE_HORSE}`"\ 370&nbsp;"  Race time: `expr $FINISH_TIME - $START_TIME` secs" 371&nbsp; 372&nbsp;# Restore cursor and old colors. 373&nbsp;echo -en "\E[?25h" 374&nbsp;echo -en "\E[0m" 375&nbsp; 376&nbsp;# Restore echoing. 377&nbsp;stty echo 378&nbsp; 379&nbsp;# Remove race temp directory. 380&nbsp;rm -rf $HORSE_RACE_TMP_DIR 381&nbsp; 382&nbsp;tput cup 19 0 383&nbsp; 384&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="contributed-scripts.html#HASHEXAMPLE">Example A-22</A>.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>There is, however, a major problem with all	  this. <ICLASS="EMPHASIS">ANSI escape sequences are emphatically	  non-portable.</I> What works fine on some terminal	  emulators (or the console) may work differently, or not	  at all, on others. A <SPANCLASS="QUOTE">"colorized"</SPAN> script that	  looks stunning on the script author's machine may produce	  unreadable output on someone else's. This greatly compromises	  the usefulness of <SPANCLASS="QUOTE">"colorizing"</SPAN> scripts, and	  possibly relegates this technique to the status of a gimmick	  or even a <SPANCLASS="QUOTE">"toy"</SPAN>.</P></TD></TR></TABLE></DIV><P>Moshe Jacobson's <BCLASS="COMMAND">color</B> utility  	  (<AHREF="http://runslinux.net/projects.html#color"TARGET="_top">http://runslinux.net/projects.html#color</A>)	  considerably simplifies using ANSI escape sequences. It	  substitutes a clean and logical syntax for the clumsy constructs	  just discussed.</P><P>Henry/teikedvl has likewise created a utility (<AHREF="http://scriptechocolor.sourceforge.net/"TARGET="_top">http://scriptechocolor.sourceforge.net/</A>) to simplify creation of colorized scripts.</P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN15428"HREF="colorizing.html#AEN15428">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><SPANCLASS="ACRONYM">ANSI</SPAN> is, of course, the	     acronym for the American National Standards	     Institute. This august body establishes and maintains	     various technical and industrial standards.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="recursionsct.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="optimizations.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Recursion</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="miscellany.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Optimizations</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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