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

📄 colorizing.html

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