| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Colorizing Scripts</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="UP" |
| 14 | TITLE="Miscellany" |
| 15 | HREF="miscellany.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Recursion: a script calling itself" |
| 18 | HREF="recursionsct.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Optimizations" |
| 21 | HREF="optimizations.html"><META |
| 22 | HTTP-EQUIV="Content-Style-Type" |
| 23 | CONTENT="text/css"><LINK |
| 24 | REL="stylesheet" |
| 25 | HREF="common/kde-common.css" |
| 26 | TYPE="text/css"><META |
| 27 | HTTP-EQUIV="Content-Type" |
| 28 | CONTENT="text/html; charset=iso-8859-1"><META |
| 29 | HTTP-EQUIV="Content-Language" |
| 30 | CONTENT="en"><LINK |
| 31 | REL="stylesheet" |
| 32 | HREF="common/kde-localised.css" |
| 33 | TYPE="text/css" |
| 34 | TITLE="KDE-English"><LINK |
| 35 | REL="stylesheet" |
| 36 | HREF="common/kde-default.css" |
| 37 | TYPE="text/css" |
| 38 | TITLE="KDE-Default"></HEAD |
| 39 | ><BODY |
| 40 | CLASS="SECT1" |
| 41 | BGCOLOR="#FFFFFF" |
| 42 | TEXT="#000000" |
| 43 | LINK="#AA0000" |
| 44 | VLINK="#AA0055" |
| 45 | ALINK="#AA0000" |
| 46 | STYLE="font-family: sans-serif;" |
| 47 | ><DIV |
| 48 | CLASS="NAVHEADER" |
| 49 | ><TABLE |
| 50 | SUMMARY="Header navigation table" |
| 51 | WIDTH="100%" |
| 52 | BORDER="0" |
| 53 | CELLPADDING="0" |
| 54 | CELLSPACING="0" |
| 55 | ><TR |
| 56 | ><TH |
| 57 | COLSPAN="3" |
| 58 | ALIGN="center" |
| 59 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 60 | ></TR |
| 61 | ><TR |
| 62 | ><TD |
| 63 | WIDTH="10%" |
| 64 | ALIGN="left" |
| 65 | VALIGN="bottom" |
| 66 | ><A |
| 67 | HREF="recursionsct.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | >Chapter 36. Miscellany</TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="optimizations.html" |
| 82 | ACCESSKEY="N" |
| 83 | >Next</A |
| 84 | ></TD |
| 85 | ></TR |
| 86 | ></TABLE |
| 87 | ><HR |
| 88 | ALIGN="LEFT" |
| 89 | WIDTH="100%"></DIV |
| 90 | ><DIV |
| 91 | CLASS="SECT1" |
| 92 | ><H1 |
| 93 | CLASS="SECT1" |
| 94 | ><A |
| 95 | NAME="COLORIZING" |
| 96 | ></A |
| 97 | >36.5. <SPAN |
| 98 | CLASS="QUOTE" |
| 99 | >"Colorizing"</SPAN |
| 100 | > Scripts</H1 |
| 101 | ><P |
| 102 | ><A |
| 103 | NAME="COLORIZINGREF" |
| 104 | ></A |
| 105 | ></P |
| 106 | ><P |
| 107 | >The ANSI |
| 108 | <A |
| 109 | NAME="AEN20259" |
| 110 | HREF="#FTN.AEN20259" |
| 111 | >[1]</A |
| 112 | > |
| 113 | escape sequences set screen attributes, such as bold |
| 114 | text, and color of foreground and background. <A |
| 115 | HREF="dosbatch.html#DOSBATCH1" |
| 116 | >DOS batch files</A |
| 117 | > commonly used |
| 118 | ANSI escape codes for <SPAN |
| 119 | CLASS="emphasis" |
| 120 | ><I |
| 121 | CLASS="EMPHASIS" |
| 122 | >color</I |
| 123 | ></SPAN |
| 124 | > output, |
| 125 | and so can Bash scripts.</P |
| 126 | ><DIV |
| 127 | CLASS="EXAMPLE" |
| 128 | ><HR><A |
| 129 | NAME="EX30A" |
| 130 | ></A |
| 131 | ><P |
| 132 | ><B |
| 133 | >Example 36-13. A <SPAN |
| 134 | CLASS="QUOTE" |
| 135 | >"colorized"</SPAN |
| 136 | > address database</B |
| 137 | ></P |
| 138 | ><TABLE |
| 139 | BORDER="0" |
| 140 | BGCOLOR="#E0E0E0" |
| 141 | WIDTH="100%" |
| 142 | ><TR |
| 143 | ><TD |
| 144 | ><PRE |
| 145 | CLASS="PROGRAMLISTING" |
| 146 | > 1 #!/bin/bash |
| 147 | 2 # ex30a.sh: "Colorized" version of ex30.sh. |
| 148 | 3 # Crude address database |
| 149 | 4 |
| 150 | 5 |
| 151 | 6 clear # Clear the screen. |
| 152 | 7 |
| 153 | 8 echo -n " " |
| 154 | 9 echo -e '\E[37;44m'"\033[1mContact List\033[0m" |
| 155 | 10 # White on blue background |
| 156 | 11 echo; echo |
| 157 | 12 echo -e "\033[1mChoose one of the following persons:\033[0m" |
| 158 | 13 # Bold |
| 159 | 14 tput sgr0 # Reset attributes. |
| 160 | 15 echo "(Enter only the first letter of name.)" |
| 161 | 16 echo |
| 162 | 17 echo -en '\E[47;34m'"\033[1mE\033[0m" # Blue |
| 163 | 18 tput sgr0 # Reset colors to "normal." |
| 164 | 19 echo "vans, Roland" # "[E]vans, Roland" |
| 165 | 20 echo -en '\E[47;35m'"\033[1mJ\033[0m" # Magenta |
| 166 | 21 tput sgr0 |
| 167 | 22 echo "ambalaya, Mildred" |
| 168 | 23 echo -en '\E[47;32m'"\033[1mS\033[0m" # Green |
| 169 | 24 tput sgr0 |
| 170 | 25 echo "mith, Julie" |
| 171 | 26 echo -en '\E[47;31m'"\033[1mZ\033[0m" # Red |
| 172 | 27 tput sgr0 |
| 173 | 28 echo "ane, Morris" |
| 174 | 29 echo |
| 175 | 30 |
| 176 | 31 read person |
| 177 | 32 |
| 178 | 33 case "$person" in |
| 179 | 34 # Note variable is quoted. |
| 180 | 35 |
| 181 | 36 "E" | "e" ) |
| 182 | 37 # Accept upper or lowercase input. |
| 183 | 38 echo |
| 184 | 39 echo "Roland Evans" |
| 185 | 40 echo "4321 Flash Dr." |
| 186 | 41 echo "Hardscrabble, CO 80753" |
| 187 | 42 echo "(303) 734-9874" |
| 188 | 43 echo "(303) 734-9892 fax" |
| 189 | 44 echo "[email protected]" |
| 190 | 45 echo "Business partner & old friend" |
| 191 | 46 ;; |
| 192 | 47 |
| 193 | 48 "J" | "j" ) |
| 194 | 49 echo |
| 195 | 50 echo "Mildred Jambalaya" |
| 196 | 51 echo "249 E. 7th St., Apt. 19" |
| 197 | 52 echo "New York, NY 10009" |
| 198 | 53 echo "(212) 533-2814" |
| 199 | 54 echo "(212) 533-9972 fax" |
| 200 | 55 echo "[email protected]" |
| 201 | 56 echo "Girlfriend" |
| 202 | 57 echo "Birthday: Feb. 11" |
| 203 | 58 ;; |
| 204 | 59 |
| 205 | 60 # Add info for Smith & Zane later. |
| 206 | 61 |
| 207 | 62 * ) |
| 208 | 63 # Default option. |
| 209 | 64 # Empty input (hitting RETURN) fits here, too. |
| 210 | 65 echo |
| 211 | 66 echo "Not yet in database." |
| 212 | 67 ;; |
| 213 | 68 |
| 214 | 69 esac |
| 215 | 70 |
| 216 | 71 tput sgr0 # Reset colors to "normal." |
| 217 | 72 |
| 218 | 73 echo |
| 219 | 74 |
| 220 | 75 exit 0</PRE |
| 221 | ></TD |
| 222 | ></TR |
| 223 | ></TABLE |
| 224 | ><HR></DIV |
| 225 | ><DIV |
| 226 | CLASS="EXAMPLE" |
| 227 | ><HR><A |
| 228 | NAME="DRAW-BOX" |
| 229 | ></A |
| 230 | ><P |
| 231 | ><B |
| 232 | >Example 36-14. Drawing a box</B |
| 233 | ></P |
| 234 | ><TABLE |
| 235 | BORDER="0" |
| 236 | BGCOLOR="#E0E0E0" |
| 237 | WIDTH="100%" |
| 238 | ><TR |
| 239 | ><TD |
| 240 | ><PRE |
| 241 | CLASS="PROGRAMLISTING" |
| 242 | > 1 #!/bin/bash |
| 243 | 2 # Draw-box.sh: Drawing a box using ASCII characters. |
| 244 | 3 |
| 245 | 4 # Script by Stefano Palmeri, with minor editing by document author. |
| 246 | 5 # Minor edits suggested by Jim Angstadt. |
| 247 | 6 # Used in the ABS Guide with permission. |
| 248 | 7 |
| 249 | 8 |
| 250 | 9 ###################################################################### |
| 251 | 10 ### draw_box function doc ### |
| 252 | 11 |
| 253 | 12 # The "draw_box" function lets the user |
| 254 | 13 #+ draw a box in a terminal. |
| 255 | 14 # |
| 256 | 15 # Usage: draw_box ROW COLUMN HEIGHT WIDTH [COLOR] |
| 257 | 16 # ROW and COLUMN represent the position |
| 258 | 17 #+ of the upper left angle of the box you're going to draw. |
| 259 | 18 # ROW and COLUMN must be greater than 0 |
| 260 | 19 #+ and less than current terminal dimension. |
| 261 | 20 # HEIGHT is the number of rows of the box, and must be > 0. |
| 262 | 21 # HEIGHT + ROW must be <= than current terminal height. |
| 263 | 22 # WIDTH is the number of columns of the box and must be > 0. |
| 264 | 23 # WIDTH + COLUMN must be <= than current terminal width. |
| 265 | 24 # |
| 266 | 25 # E.g.: If your terminal dimension is 20x80, |
| 267 | 26 # draw_box 2 3 10 45 is good |
| 268 | 27 # draw_box 2 3 19 45 has bad HEIGHT value (19+2 > 20) |
| 269 | 28 # draw_box 2 3 18 78 has bad WIDTH value (78+3 > 80) |
| 270 | 29 # |
| 271 | 30 # COLOR is the color of the box frame. |
| 272 | 31 # This is the 5th argument and is optional. |
| 273 | 32 # 0=black 1=red 2=green 3=tan 4=blue 5=purple 6=cyan 7=white. |
| 274 | 33 # If you pass the function bad arguments, |
| 275 | 34 #+ it will just exit with code 65, |
| 276 | 35 #+ and no messages will be printed on stderr. |
| 277 | 36 # |
| 278 | 37 # Clear the terminal before you start to draw a box. |
| 279 | 38 # The clear command is not contained within the function. |
| 280 | 39 # This allows the user to draw multiple boxes, even overlapping ones. |
| 281 | 40 |
| 282 | 41 ### end of draw_box function doc ### |
| 283 | 42 ###################################################################### |
| 284 | 43 |
| 285 | 44 draw_box(){ |
| 286 | 45 |
| 287 | 46 #=============# |
| 288 | 47 HORZ="-" |
| 289 | 48 VERT="|" |
| 290 | 49 CORNER_CHAR="+" |
| 291 | 50 |
| 292 | 51 MINARGS=4 |
| 293 | 52 E_BADARGS=65 |
| 294 | 53 #=============# |
| 295 | 54 |
| 296 | 55 |
| 297 | 56 if [ $# -lt "$MINARGS" ]; then # If args are less than 4, exit. |
| 298 | 57 exit $E_BADARGS |
| 299 | 58 fi |
| 300 | 59 |
| 301 | 60 # Looking for non digit chars in arguments. |
| 302 | 61 # Probably it could be done better (exercise for the reader?). |
| 303 | 62 if echo $@ | tr -d [:blank:] | tr -d [:digit:] | grep . &> /dev/null; then |
| 304 | 63 exit $E_BADARGS |
| 305 | 64 fi |
| 306 | 65 |
| 307 | 66 BOX_HEIGHT=`expr $3 - 1` # -1 correction needed because angle char "+" |
| 308 | 67 BOX_WIDTH=`expr $4 - 1` #+ is a part of both box height and width. |
| 309 | 68 T_ROWS=`tput lines` # Define current terminal dimension |
| 310 | 69 T_COLS=`tput cols` #+ in rows and columns. |
| 311 | 70 |
| 312 | 71 if [ $1 -lt 1 ] || [ $1 -gt $T_ROWS ]; then # Start checking if arguments |
| 313 | 72 exit $E_BADARGS #+ are correct. |
| 314 | 73 fi |
| 315 | 74 if [ $2 -lt 1 ] || [ $2 -gt $T_COLS ]; then |
| 316 | 75 exit $E_BADARGS |
| 317 | 76 fi |
| 318 | 77 if [ `expr $1 + $BOX_HEIGHT + 1` -gt $T_ROWS ]; then |
| 319 | 78 exit $E_BADARGS |
| 320 | 79 fi |
| 321 | 80 if [ `expr $2 + $BOX_WIDTH + 1` -gt $T_COLS ]; then |
| 322 | 81 exit $E_BADARGS |
| 323 | 82 fi |
| 324 | 83 if [ $3 -lt 1 ] || [ $4 -lt 1 ]; then |
| 325 | 84 exit $E_BADARGS |
| 326 | 85 fi # End checking arguments. |
| 327 | 86 |
| 328 | 87 plot_char(){ # Function within a function. |
| 329 | 88 echo -e "\E[${1};${2}H"$3 |
| 330 | 89 } |
| 331 | 90 |
| 332 | 91 echo -ne "\E[3${5}m" # Set box frame color, if defined. |
| 333 | 92 |
| 334 | 93 # start drawing the box |
| 335 | 94 |
| 336 | 95 count=1 # Draw vertical lines using |
| 337 | 96 for (( r=$1; count<=$BOX_HEIGHT; r++)); do #+ plot_char function. |
| 338 | 97 plot_char $r $2 $VERT |
| 339 | 98 let count=count+1 |
| 340 | 99 done |
| 341 | 100 |
| 342 | 101 count=1 |
| 343 | 102 c=`expr $2 + $BOX_WIDTH` |
| 344 | 103 for (( r=$1; count<=$BOX_HEIGHT; r++)); do |
| 345 | 104 plot_char $r $c $VERT |
| 346 | 105 let count=count+1 |
| 347 | 106 done |
| 348 | 107 |
| 349 | 108 count=1 # Draw horizontal lines using |
| 350 | 109 for (( c=$2; count<=$BOX_WIDTH; c++)); do #+ plot_char function. |
| 351 | 110 plot_char $1 $c $HORZ |
| 352 | 111 let count=count+1 |
| 353 | 112 done |
| 354 | 113 |
| 355 | 114 count=1 |
| 356 | 115 r=`expr $1 + $BOX_HEIGHT` |
| 357 | 116 for (( c=$2; count<=$BOX_WIDTH; c++)); do |
| 358 | 117 plot_char $r $c $HORZ |
| 359 | 118 let count=count+1 |
| 360 | 119 done |
| 361 | 120 |
| 362 | 121 plot_char $1 $2 $CORNER_CHAR # Draw box angles. |
| 363 | 122 plot_char $1 `expr $2 + $BOX_WIDTH` $CORNER_CHAR |
| 364 | 123 plot_char `expr $1 + $BOX_HEIGHT` $2 $CORNER_CHAR |
| 365 | 124 plot_char `expr $1 + $BOX_HEIGHT` `expr $2 + $BOX_WIDTH` $CORNER_CHAR |
| 366 | 125 |
| 367 | 126 echo -ne "\E[0m" # Restore old colors. |
| 368 | 127 |
| 369 | 128 P_ROWS=`expr $T_ROWS - 1` # Put the prompt at bottom of the terminal. |
| 370 | 129 |
| 371 | 130 echo -e "\E[${P_ROWS};1H" |
| 372 | 131 } |
| 373 | 132 |
| 374 | 133 |
| 375 | 134 # Now, let's try drawing a box. |
| 376 | 135 clear # Clear the terminal. |
| 377 | 136 R=2 # Row |
| 378 | 137 C=3 # Column |
| 379 | 138 H=10 # Height |
| 380 | 139 W=45 # Width |
| 381 | 140 col=1 # Color (red) |
| 382 | 141 draw_box $R $C $H $W $col # Draw the box. |
| 383 | 142 |
| 384 | 143 exit 0 |
| 385 | 144 |
| 386 | 145 # Exercise: |
| 387 | 146 # -------- |
| 388 | 147 # Add the option of printing text within the drawn box.</PRE |
| 389 | ></TD |
| 390 | ></TR |
| 391 | ></TABLE |
| 392 | ><HR></DIV |
| 393 | ><P |
| 394 | >The simplest, and perhaps most useful ANSI escape sequence is |
| 395 | bold text, <B |
| 396 | CLASS="COMMAND" |
| 397 | >\033[1m ... \033[0m</B |
| 398 | >. The |
| 399 | <SPAN |
| 400 | CLASS="TOKEN" |
| 401 | >\033</SPAN |
| 402 | > represents an <A |
| 403 | HREF="escapingsection.html#ESCP" |
| 404 | >escape</A |
| 405 | >, the <SPAN |
| 406 | CLASS="QUOTE" |
| 407 | >"[1"</SPAN |
| 408 | > turns on the |
| 409 | bold attribute, while the <SPAN |
| 410 | CLASS="QUOTE" |
| 411 | >"[0"</SPAN |
| 412 | > switches it off. The |
| 413 | <SPAN |
| 414 | CLASS="QUOTE" |
| 415 | >"m"</SPAN |
| 416 | > terminates each term of the escape sequence. |
| 417 | <TABLE |
| 418 | BORDER="0" |
| 419 | BGCOLOR="#E0E0E0" |
| 420 | WIDTH="100%" |
| 421 | ><TR |
| 422 | ><TD |
| 423 | ><PRE |
| 424 | CLASS="SCREEN" |
| 425 | > <TT |
| 426 | CLASS="PROMPT" |
| 427 | >bash$ </TT |
| 428 | ><TT |
| 429 | CLASS="USERINPUT" |
| 430 | ><B |
| 431 | >echo -e "\033[1mThis is bold text.\033[0m"</B |
| 432 | ></TT |
| 433 | > |
| 434 | </PRE |
| 435 | ></TD |
| 436 | ></TR |
| 437 | ></TABLE |
| 438 | > |
| 439 | </P |
| 440 | ><P |
| 441 | >A similar escape sequence switches on the underline |
| 442 | attribute (on an <I |
| 443 | CLASS="FIRSTTERM" |
| 444 | >rxvt</I |
| 445 | > and an |
| 446 | <I |
| 447 | CLASS="FIRSTTERM" |
| 448 | >aterm</I |
| 449 | >). |
| 450 | <TABLE |
| 451 | BORDER="0" |
| 452 | BGCOLOR="#E0E0E0" |
| 453 | WIDTH="100%" |
| 454 | ><TR |
| 455 | ><TD |
| 456 | ><PRE |
| 457 | CLASS="SCREEN" |
| 458 | > <TT |
| 459 | CLASS="PROMPT" |
| 460 | >bash$ </TT |
| 461 | ><TT |
| 462 | CLASS="USERINPUT" |
| 463 | ><B |
| 464 | >echo -e "\033[4mThis is underlined text.\033[0m"</B |
| 465 | ></TT |
| 466 | > |
| 467 | </PRE |
| 468 | ></TD |
| 469 | ></TR |
| 470 | ></TABLE |
| 471 | > |
| 472 | </P |
| 473 | ><DIV |
| 474 | CLASS="NOTE" |
| 475 | ><TABLE |
| 476 | CLASS="NOTE" |
| 477 | WIDTH="100%" |
| 478 | BORDER="0" |
| 479 | ><TR |
| 480 | ><TD |
| 481 | WIDTH="25" |
| 482 | ALIGN="CENTER" |
| 483 | VALIGN="TOP" |
| 484 | ><IMG |
| 485 | SRC="common/note.png" |
| 486 | HSPACE="5" |
| 487 | ALT="Note"></TD |
| 488 | ><TD |
| 489 | ALIGN="LEFT" |
| 490 | VALIGN="TOP" |
| 491 | ><P |
| 492 | >With an <B |
| 493 | CLASS="COMMAND" |
| 494 | >echo</B |
| 495 | >, the |
| 496 | <TT |
| 497 | CLASS="OPTION" |
| 498 | >-e</TT |
| 499 | > option enables the escape |
| 500 | sequences.</P |
| 501 | ></TD |
| 502 | ></TR |
| 503 | ></TABLE |
| 504 | ></DIV |
| 505 | ><P |
| 506 | >Other escape sequences change the text and/or background |
| 507 | color.</P |
| 508 | ><P |
| 509 | > <TABLE |
| 510 | BORDER="0" |
| 511 | BGCOLOR="#E0E0E0" |
| 512 | WIDTH="100%" |
| 513 | ><TR |
| 514 | ><TD |
| 515 | ><PRE |
| 516 | CLASS="SCREEN" |
| 517 | > <TT |
| 518 | CLASS="PROMPT" |
| 519 | >bash$ </TT |
| 520 | ><TT |
| 521 | CLASS="USERINPUT" |
| 522 | ><B |
| 523 | >echo -e '\E[34;47mThis prints in blue.'; tput sgr0</B |
| 524 | ></TT |
| 525 | > |
| 526 | |
| 527 | |
| 528 | <TT |
| 529 | CLASS="PROMPT" |
| 530 | >bash$ </TT |
| 531 | ><TT |
| 532 | CLASS="USERINPUT" |
| 533 | ><B |
| 534 | >echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0</B |
| 535 | ></TT |
| 536 | > |
| 537 | |
| 538 | |
| 539 | <TT |
| 540 | CLASS="PROMPT" |
| 541 | >bash$ </TT |
| 542 | ><TT |
| 543 | CLASS="USERINPUT" |
| 544 | ><B |
| 545 | >echo -e '\E[1;33;44m'"BOLD yellow text on blue background"; tput sgr0</B |
| 546 | ></TT |
| 547 | > |
| 548 | </PRE |
| 549 | ></TD |
| 550 | ></TR |
| 551 | ></TABLE |
| 552 | > |
| 553 | </P |
| 554 | ><DIV |
| 555 | CLASS="NOTE" |
| 556 | ><TABLE |
| 557 | CLASS="NOTE" |
| 558 | WIDTH="100%" |
| 559 | BORDER="0" |
| 560 | ><TR |
| 561 | ><TD |
| 562 | WIDTH="25" |
| 563 | ALIGN="CENTER" |
| 564 | VALIGN="TOP" |
| 565 | ><IMG |
| 566 | SRC="common/note.png" |
| 567 | HSPACE="5" |
| 568 | ALT="Note"></TD |
| 569 | ><TD |
| 570 | ALIGN="LEFT" |
| 571 | VALIGN="TOP" |
| 572 | ><P |
| 573 | >It's usually advisable to set the |
| 574 | <I |
| 575 | CLASS="FIRSTTERM" |
| 576 | >bold</I |
| 577 | > attribute for light-colored foreground |
| 578 | text.</P |
| 579 | ></TD |
| 580 | ></TR |
| 581 | ></TABLE |
| 582 | ></DIV |
| 583 | ><P |
| 584 | >The <B |
| 585 | CLASS="COMMAND" |
| 586 | >tput sgr0</B |
| 587 | > restores the |
| 588 | terminal settings to normal. Omitting this lets all |
| 589 | subsequent output from that particular terminal remain |
| 590 | blue.</P |
| 591 | ><DIV |
| 592 | CLASS="NOTE" |
| 593 | ><TABLE |
| 594 | CLASS="NOTE" |
| 595 | WIDTH="100%" |
| 596 | BORDER="0" |
| 597 | ><TR |
| 598 | ><TD |
| 599 | WIDTH="25" |
| 600 | ALIGN="CENTER" |
| 601 | VALIGN="TOP" |
| 602 | ><IMG |
| 603 | SRC="common/note.png" |
| 604 | HSPACE="5" |
| 605 | ALT="Note"></TD |
| 606 | ><TD |
| 607 | ALIGN="LEFT" |
| 608 | VALIGN="TOP" |
| 609 | ><P |
| 610 | >Since <B |
| 611 | CLASS="COMMAND" |
| 612 | >tput sgr0</B |
| 613 | > fails to restore |
| 614 | terminal settings under certain circumstances, |
| 615 | <B |
| 616 | CLASS="COMMAND" |
| 617 | >echo -ne \E[0m</B |
| 618 | > may be a better choice.</P |
| 619 | ></TD |
| 620 | ></TR |
| 621 | ></TABLE |
| 622 | ></DIV |
| 623 | ><P |
| 624 | ><A |
| 625 | NAME="COLORIZTEMPL" |
| 626 | ></A |
| 627 | ></P |
| 628 | ><TABLE |
| 629 | CLASS="SIDEBAR" |
| 630 | BORDER="1" |
| 631 | CELLPADDING="5" |
| 632 | ><TR |
| 633 | ><TD |
| 634 | ><DIV |
| 635 | CLASS="SIDEBAR" |
| 636 | ><A |
| 637 | NAME="AEN20311" |
| 638 | ></A |
| 639 | ><P |
| 640 | >Use the following template for writing colored text on a colored |
| 641 | background.</P |
| 642 | ><P |
| 643 | > <TT |
| 644 | CLASS="USERINPUT" |
| 645 | ><B |
| 646 | >echo -e '\E[COLOR1;COLOR2mSome text goes here.'</B |
| 647 | ></TT |
| 648 | > |
| 649 | </P |
| 650 | ><P |
| 651 | >The <SPAN |
| 652 | CLASS="QUOTE" |
| 653 | >"\E["</SPAN |
| 654 | > begins the escape sequence. |
| 655 | The semicolon-separated numbers <SPAN |
| 656 | CLASS="QUOTE" |
| 657 | >"COLOR1"</SPAN |
| 658 | > and |
| 659 | <SPAN |
| 660 | CLASS="QUOTE" |
| 661 | >"COLOR2"</SPAN |
| 662 | > specify a foreground and a background |
| 663 | color, according to the table below. (The order of the |
| 664 | numbers does not matter, since the foreground and background |
| 665 | numbers fall in non-overlapping ranges.) The <SPAN |
| 666 | CLASS="QUOTE" |
| 667 | >"m"</SPAN |
| 668 | > |
| 669 | terminates the escape sequence, and the text begins immediately |
| 670 | after that.</P |
| 671 | ><P |
| 672 | >Note also that <A |
| 673 | HREF="variables.html#SNGLQUO" |
| 674 | >single quotes</A |
| 675 | > |
| 676 | enclose the remainder of the command sequence following the |
| 677 | <B |
| 678 | CLASS="COMMAND" |
| 679 | >echo -e</B |
| 680 | >.</P |
| 681 | ></DIV |
| 682 | ></TD |
| 683 | ></TR |
| 684 | ></TABLE |
| 685 | ><P |
| 686 | >The numbers in the following table work for an |
| 687 | <I |
| 688 | CLASS="FIRSTTERM" |
| 689 | >rxvt</I |
| 690 | > terminal. Results may vary for other |
| 691 | terminal emulators.</P |
| 692 | ><P |
| 693 | ><A |
| 694 | NAME="COLORIZTABLE" |
| 695 | ></A |
| 696 | ></P |
| 697 | ><DIV |
| 698 | CLASS="TABLE" |
| 699 | ><HR><A |
| 700 | NAME="AEN20327" |
| 701 | ></A |
| 702 | ><P |
| 703 | ><B |
| 704 | >Table 36-1. Numbers representing colors in Escape Sequences</B |
| 705 | ></P |
| 706 | ><TABLE |
| 707 | BORDER="1" |
| 708 | CLASS="CALSTABLE" |
| 709 | ><THEAD |
| 710 | ><TR |
| 711 | ><TH |
| 712 | ALIGN="LEFT" |
| 713 | VALIGN="TOP" |
| 714 | >Color</TH |
| 715 | ><TH |
| 716 | ALIGN="LEFT" |
| 717 | VALIGN="TOP" |
| 718 | >Foreground</TH |
| 719 | ><TH |
| 720 | ALIGN="LEFT" |
| 721 | VALIGN="TOP" |
| 722 | >Background</TH |
| 723 | ></TR |
| 724 | ></THEAD |
| 725 | ><TBODY |
| 726 | ><TR |
| 727 | ><TD |
| 728 | ALIGN="LEFT" |
| 729 | VALIGN="TOP" |
| 730 | ><TT |
| 731 | CLASS="OPTION" |
| 732 | >black</TT |
| 733 | ></TD |
| 734 | ><TD |
| 735 | ALIGN="LEFT" |
| 736 | VALIGN="TOP" |
| 737 | >30</TD |
| 738 | ><TD |
| 739 | ALIGN="LEFT" |
| 740 | VALIGN="TOP" |
| 741 | >40</TD |
| 742 | ></TR |
| 743 | ><TR |
| 744 | ><TD |
| 745 | ALIGN="LEFT" |
| 746 | VALIGN="TOP" |
| 747 | ><TT |
| 748 | CLASS="OPTION" |
| 749 | >red</TT |
| 750 | ></TD |
| 751 | ><TD |
| 752 | ALIGN="LEFT" |
| 753 | VALIGN="TOP" |
| 754 | >31</TD |
| 755 | ><TD |
| 756 | ALIGN="LEFT" |
| 757 | VALIGN="TOP" |
| 758 | >41</TD |
| 759 | ></TR |
| 760 | ><TR |
| 761 | ><TD |
| 762 | ALIGN="LEFT" |
| 763 | VALIGN="TOP" |
| 764 | ><TT |
| 765 | CLASS="OPTION" |
| 766 | >green</TT |
| 767 | ></TD |
| 768 | ><TD |
| 769 | ALIGN="LEFT" |
| 770 | VALIGN="TOP" |
| 771 | >32</TD |
| 772 | ><TD |
| 773 | ALIGN="LEFT" |
| 774 | VALIGN="TOP" |
| 775 | >42</TD |
| 776 | ></TR |
| 777 | ><TR |
| 778 | ><TD |
| 779 | ALIGN="LEFT" |
| 780 | VALIGN="TOP" |
| 781 | ><TT |
| 782 | CLASS="OPTION" |
| 783 | >yellow</TT |
| 784 | ></TD |
| 785 | ><TD |
| 786 | ALIGN="LEFT" |
| 787 | VALIGN="TOP" |
| 788 | >33</TD |
| 789 | ><TD |
| 790 | ALIGN="LEFT" |
| 791 | VALIGN="TOP" |
| 792 | >43</TD |
| 793 | ></TR |
| 794 | ><TR |
| 795 | ><TD |
| 796 | ALIGN="LEFT" |
| 797 | VALIGN="TOP" |
| 798 | ><TT |
| 799 | CLASS="OPTION" |
| 800 | >blue</TT |
| 801 | ></TD |
| 802 | ><TD |
| 803 | ALIGN="LEFT" |
| 804 | VALIGN="TOP" |
| 805 | >34</TD |
| 806 | ><TD |
| 807 | ALIGN="LEFT" |
| 808 | VALIGN="TOP" |
| 809 | >44</TD |
| 810 | ></TR |
| 811 | ><TR |
| 812 | ><TD |
| 813 | ALIGN="LEFT" |
| 814 | VALIGN="TOP" |
| 815 | ><TT |
| 816 | CLASS="OPTION" |
| 817 | >magenta</TT |
| 818 | ></TD |
| 819 | ><TD |
| 820 | ALIGN="LEFT" |
| 821 | VALIGN="TOP" |
| 822 | >35</TD |
| 823 | ><TD |
| 824 | ALIGN="LEFT" |
| 825 | VALIGN="TOP" |
| 826 | >45</TD |
| 827 | ></TR |
| 828 | ><TR |
| 829 | ><TD |
| 830 | ALIGN="LEFT" |
| 831 | VALIGN="TOP" |
| 832 | ><TT |
| 833 | CLASS="OPTION" |
| 834 | >cyan</TT |
| 835 | ></TD |
| 836 | ><TD |
| 837 | ALIGN="LEFT" |
| 838 | VALIGN="TOP" |
| 839 | >36</TD |
| 840 | ><TD |
| 841 | ALIGN="LEFT" |
| 842 | VALIGN="TOP" |
| 843 | >46</TD |
| 844 | ></TR |
| 845 | ><TR |
| 846 | ><TD |
| 847 | ALIGN="LEFT" |
| 848 | VALIGN="TOP" |
| 849 | ><TT |
| 850 | CLASS="OPTION" |
| 851 | >white</TT |
| 852 | ></TD |
| 853 | ><TD |
| 854 | ALIGN="LEFT" |
| 855 | VALIGN="TOP" |
| 856 | >37</TD |
| 857 | ><TD |
| 858 | ALIGN="LEFT" |
| 859 | VALIGN="TOP" |
| 860 | >47</TD |
| 861 | ></TR |
| 862 | ></TBODY |
| 863 | ></TABLE |
| 864 | ><HR></DIV |
| 865 | ><DIV |
| 866 | CLASS="EXAMPLE" |
| 867 | ><HR><A |
| 868 | NAME="COLORECHO" |
| 869 | ></A |
| 870 | ><P |
| 871 | ><B |
| 872 | >Example 36-15. Echoing colored text</B |
| 873 | ></P |
| 874 | ><TABLE |
| 875 | BORDER="0" |
| 876 | BGCOLOR="#E0E0E0" |
| 877 | WIDTH="100%" |
| 878 | ><TR |
| 879 | ><TD |
| 880 | ><PRE |
| 881 | CLASS="PROGRAMLISTING" |
| 882 | > 1 #!/bin/bash |
| 883 | 2 # color-echo.sh: Echoing text messages in color. |
| 884 | 3 |
| 885 | 4 # Modify this script for your own purposes. |
| 886 | 5 # It's easier than hand-coding color. |
| 887 | 6 |
| 888 | 7 black='\E[30;47m' |
| 889 | 8 red='\E[31;47m' |
| 890 | 9 green='\E[32;47m' |
| 891 | 10 yellow='\E[33;47m' |
| 892 | 11 blue='\E[34;47m' |
| 893 | 12 magenta='\E[35;47m' |
| 894 | 13 cyan='\E[36;47m' |
| 895 | 14 white='\E[37;47m' |
| 896 | 15 |
| 897 | 16 |
| 898 | 17 alias Reset="tput sgr0" # Reset text attributes to normal |
| 899 | 18 #+ without clearing screen. |
| 900 | 19 |
| 901 | 20 |
| 902 | 21 cecho () # Color-echo. |
| 903 | 22 # Argument $1 = message |
| 904 | 23 # Argument $2 = color |
| 905 | 24 { |
| 906 | 25 local default_msg="No message passed." |
| 907 | 26 # Doesn't really need to be a local variable. |
| 908 | 27 |
| 909 | 28 message=${1:-$default_msg} # Defaults to default message. |
| 910 | 29 color=${2:-$black} # Defaults to black, if not specified. |
| 911 | 30 |
| 912 | 31 echo -e "$color" |
| 913 | 32 echo "$message" |
| 914 | 33 Reset # Reset to normal. |
| 915 | 34 |
| 916 | 35 return |
| 917 | 36 } |
| 918 | 37 |
| 919 | 38 |
| 920 | 39 # Now, let's try it out. |
| 921 | 40 # ---------------------------------------------------- |
| 922 | 41 cecho "Feeling blue..." $blue |
| 923 | 42 cecho "Magenta looks more like purple." $magenta |
| 924 | 43 cecho "Green with envy." $green |
| 925 | 44 cecho "Seeing red?" $red |
| 926 | 45 cecho "Cyan, more familiarly known as aqua." $cyan |
| 927 | 46 cecho "No color passed (defaults to black)." |
| 928 | 47 # Missing $color argument. |
| 929 | 48 cecho "\"Empty\" color passed (defaults to black)." "" |
| 930 | 49 # Empty $color argument. |
| 931 | 50 cecho |
| 932 | 51 # Missing $message and $color arguments. |
| 933 | 52 cecho "" "" |
| 934 | 53 # Empty $message and $color arguments. |
| 935 | 54 # ---------------------------------------------------- |
| 936 | 55 |
| 937 | 56 echo |
| 938 | 57 |
| 939 | 58 exit 0 |
| 940 | 59 |
| 941 | 60 # Exercises: |
| 942 | 61 # --------- |
| 943 | 62 # 1) Add the "bold" attribute to the 'cecho ()' function. |
| 944 | 63 # 2) Add options for colored backgrounds.</PRE |
| 945 | ></TD |
| 946 | ></TR |
| 947 | ></TABLE |
| 948 | ><HR></DIV |
| 949 | ><P |
| 950 | ><A |
| 951 | NAME="HORSERACEREF" |
| 952 | ></A |
| 953 | ></P |
| 954 | ><DIV |
| 955 | CLASS="EXAMPLE" |
| 956 | ><HR><A |
| 957 | NAME="HORSERACE" |
| 958 | ></A |
| 959 | ><P |
| 960 | ><B |
| 961 | >Example 36-16. A <SPAN |
| 962 | CLASS="QUOTE" |
| 963 | >"horserace"</SPAN |
| 964 | > game</B |
| 965 | ></P |
| 966 | ><TABLE |
| 967 | BORDER="0" |
| 968 | BGCOLOR="#E0E0E0" |
| 969 | WIDTH="100%" |
| 970 | ><TR |
| 971 | ><TD |
| 972 | ><PRE |
| 973 | CLASS="PROGRAMLISTING" |
| 974 | > 1 #!/bin/bash |
| 975 | 2 # horserace.sh: Very simple horserace simulation. |
| 976 | 3 # Author: Stefano Palmeri |
| 977 | 4 # Used with permission. |
| 978 | 5 |
| 979 | 6 ################################################################ |
| 980 | 7 # Goals of the script: |
| 981 | 8 # playing with escape sequences and terminal colors. |
| 982 | 9 # |
| 983 | 10 # Exercise: |
| 984 | 11 # Edit the script to make it run less randomly, |
| 985 | 12 #+ set up a fake betting shop . . . |
| 986 | 13 # Um . . . um . . . it's starting to remind me of a movie . . . |
| 987 | 14 # |
| 988 | 15 # The script gives each horse a random handicap. |
| 989 | 16 # The odds are calculated upon horse handicap |
| 990 | 17 #+ and are expressed in European(?) style. |
| 991 | 18 # E.g., odds=3.75 means that if you bet $1 and win, |
| 992 | 19 #+ you receive $3.75. |
| 993 | 20 # |
| 994 | 21 # The script has been tested with a GNU/Linux OS, |
| 995 | 22 #+ using xterm and rxvt, and konsole. |
| 996 | 23 # On a machine with an AMD 900 MHz processor, |
| 997 | 24 #+ the average race time is 75 seconds. |
| 998 | 25 # On faster computers the race time would be lower. |
| 999 | 26 # So, if you want more suspense, reset the USLEEP_ARG variable. |
| 1000 | 27 # |
| 1001 | 28 # Script by Stefano Palmeri. |
| 1002 | 29 ################################################################ |
| 1003 | 30 |
| 1004 | 31 E_RUNERR=65 |
| 1005 | 32 |
| 1006 | 33 # Check if md5sum and bc are installed. |
| 1007 | 34 if ! which bc &> /dev/null; then |
| 1008 | 35 echo bc is not installed. |
| 1009 | 36 echo "Can\'t run . . . " |
| 1010 | 37 exit $E_RUNERR |
| 1011 | 38 fi |
| 1012 | 39 if ! which md5sum &> /dev/null; then |
| 1013 | 40 echo md5sum is not installed. |
| 1014 | 41 echo "Can\'t run . . . " |
| 1015 | 42 exit $E_RUNERR |
| 1016 | 43 fi |
| 1017 | 44 |
| 1018 | 45 # Set the following variable to slow down script execution. |
| 1019 | 46 # It will be passed as the argument for usleep (man usleep) |
| 1020 | 47 #+ and is expressed in microseconds (500000 = half a second). |
| 1021 | 48 USLEEP_ARG=0 |
| 1022 | 49 |
| 1023 | 50 # Clean up the temp directory, restore terminal cursor and |
| 1024 | 51 #+ terminal colors -- if script interrupted by Ctl-C. |
| 1025 | 52 trap 'echo -en "\E[?25h"; echo -en "\E[0m"; stty echo;\ |
| 1026 | 53 tput cup 20 0; rm -fr $HORSE_RACE_TMP_DIR' TERM EXIT |
| 1027 | 54 # See the chapter on debugging for an explanation of 'trap.' |
| 1028 | 55 |
| 1029 | 56 # Set a unique (paranoid) name for the temp directory the script needs. |
| 1030 | 57 HORSE_RACE_TMP_DIR=$HOME/.horserace-`date +%s`-`head -c10 /dev/urandom \ |
| 1031 | 58 | md5sum | head -c30` |
| 1032 | 59 |
| 1033 | 60 # Create the temp directory and move right in. |
| 1034 | 61 mkdir $HORSE_RACE_TMP_DIR |
| 1035 | 62 cd $HORSE_RACE_TMP_DIR |
| 1036 | 63 |
| 1037 | 64 |
| 1038 | 65 # This function moves the cursor to line $1 column $2 and then prints $3. |
| 1039 | 66 # E.g.: "move_and_echo 5 10 linux" is equivalent to |
| 1040 | 67 #+ "tput cup 4 9; echo linux", but with one command instead of two. |
| 1041 | 68 # Note: "tput cup" defines 0 0 the upper left angle of the terminal, |
| 1042 | 69 #+ echo defines 1 1 the upper left angle of the terminal. |
| 1043 | 70 move_and_echo() { |
| 1044 | 71 echo -ne "\E[${1};${2}H""$3" |
| 1045 | 72 } |
| 1046 | 73 |
| 1047 | 74 # Function to generate a pseudo-random number between 1 and 9. |
| 1048 | 75 random_1_9 () |
| 1049 | 76 { |
| 1050 | 77 head -c10 /dev/urandom | md5sum | tr -d [a-z] | tr -d 0 | cut -c1 |
| 1051 | 78 } |
| 1052 | 79 |
| 1053 | 80 # Two functions that simulate "movement," when drawing the horses. |
| 1054 | 81 draw_horse_one() { |
| 1055 | 82 echo -n " "//$MOVE_HORSE// |
| 1056 | 83 } |
| 1057 | 84 draw_horse_two(){ |
| 1058 | 85 echo -n " "\\\\$MOVE_HORSE\\\\ |
| 1059 | 86 } |
| 1060 | 87 |
| 1061 | 88 |
| 1062 | 89 # Define current terminal dimension. |
| 1063 | 90 N_COLS=`tput cols` |
| 1064 | 91 N_LINES=`tput lines` |
| 1065 | 92 |
| 1066 | 93 # Need at least a 20-LINES X 80-COLUMNS terminal. Check it. |
| 1067 | 94 if [ $N_COLS -lt 80 ] || [ $N_LINES -lt 20 ]; then |
| 1068 | 95 echo "`basename $0` needs a 80-cols X 20-lines terminal." |
| 1069 | 96 echo "Your terminal is ${N_COLS}-cols X ${N_LINES}-lines." |
| 1070 | 97 exit $E_RUNERR |
| 1071 | 98 fi |
| 1072 | 99 |
| 1073 | 100 |
| 1074 | 101 # Start drawing the race field. |
| 1075 | 102 |
| 1076 | 103 # Need a string of 80 chars. See below. |
| 1077 | 104 BLANK80=`seq -s "" 100 | head -c80` |
| 1078 | 105 |
| 1079 | 106 clear |
| 1080 | 107 |
| 1081 | 108 # Set foreground and background colors to white. |
| 1082 | 109 echo -ne '\E[37;47m' |
| 1083 | 110 |
| 1084 | 111 # Move the cursor on the upper left angle of the terminal. |
| 1085 | 112 tput cup 0 0 |
| 1086 | 113 |
| 1087 | 114 # Draw six white lines. |
| 1088 | 115 for n in `seq 5`; do |
| 1089 | 116 echo $BLANK80 # Use the 80 chars string to colorize the terminal. |
| 1090 | 117 done |
| 1091 | 118 |
| 1092 | 119 # Sets foreground color to black. |
| 1093 | 120 echo -ne '\E[30m' |
| 1094 | 121 |
| 1095 | 122 move_and_echo 3 1 "START 1" |
| 1096 | 123 move_and_echo 3 75 FINISH |
| 1097 | 124 move_and_echo 1 5 "|" |
| 1098 | 125 move_and_echo 1 80 "|" |
| 1099 | 126 move_and_echo 2 5 "|" |
| 1100 | 127 move_and_echo 2 80 "|" |
| 1101 | 128 move_and_echo 4 5 "| 2" |
| 1102 | 129 move_and_echo 4 80 "|" |
| 1103 | 130 move_and_echo 5 5 "V 3" |
| 1104 | 131 move_and_echo 5 80 "V" |
| 1105 | 132 |
| 1106 | 133 # Set foreground color to red. |
| 1107 | 134 echo -ne '\E[31m' |
| 1108 | 135 |
| 1109 | 136 # Some ASCII art. |
| 1110 | 137 move_and_echo 1 8 "..@@@..@@@@@...@@@@@.@...@..@@@@..." |
| 1111 | 138 move_and_echo 2 8 ".@...@...@.......@...@...@.@......." |
| 1112 | 139 move_and_echo 3 8 ".@@@@@...@.......@...@@@@@.@@@@...." |
| 1113 | 140 move_and_echo 4 8 ".@...@...@.......@...@...@.@......." |
| 1114 | 141 move_and_echo 5 8 ".@...@...@.......@...@...@..@@@@..." |
| 1115 | 142 move_and_echo 1 43 "@@@@...@@@...@@@@..@@@@..@@@@." |
| 1116 | 143 move_and_echo 2 43 "@...@.@...@.@.....@.....@....." |
| 1117 | 144 move_and_echo 3 43 "@@@@..@@@@@.@.....@@@@...@@@.." |
| 1118 | 145 move_and_echo 4 43 "@..@..@...@.@.....@.........@." |
| 1119 | 146 move_and_echo 5 43 "@...@.@...@..@@@@..@@@@.@@@@.." |
| 1120 | 147 |
| 1121 | 148 |
| 1122 | 149 # Set foreground and background colors to green. |
| 1123 | 150 echo -ne '\E[32;42m' |
| 1124 | 151 |
| 1125 | 152 # Draw eleven green lines. |
| 1126 | 153 tput cup 5 0 |
| 1127 | 154 for n in `seq 11`; do |
| 1128 | 155 echo $BLANK80 |
| 1129 | 156 done |
| 1130 | 157 |
| 1131 | 158 # Set foreground color to black. |
| 1132 | 159 echo -ne '\E[30m' |
| 1133 | 160 tput cup 5 0 |
| 1134 | 161 |
| 1135 | 162 # Draw the fences. |
| 1136 | 163 echo "++++++++++++++++++++++++++++++++++++++\ |
| 1137 | 164 ++++++++++++++++++++++++++++++++++++++++++" |
| 1138 | 165 |
| 1139 | 166 tput cup 15 0 |
| 1140 | 167 echo "++++++++++++++++++++++++++++++++++++++\ |
| 1141 | 168 ++++++++++++++++++++++++++++++++++++++++++" |
| 1142 | 169 |
| 1143 | 170 # Set foreground and background colors to white. |
| 1144 | 171 echo -ne '\E[37;47m' |
| 1145 | 172 |
| 1146 | 173 # Draw three white lines. |
| 1147 | 174 for n in `seq 3`; do |
| 1148 | 175 echo $BLANK80 |
| 1149 | 176 done |
| 1150 | 177 |
| 1151 | 178 # Set foreground color to black. |
| 1152 | 179 echo -ne '\E[30m' |
| 1153 | 180 |
| 1154 | 181 # Create 9 files to stores handicaps. |
| 1155 | 182 for n in `seq 10 7 68`; do |
| 1156 | 183 touch $n |
| 1157 | 184 done |
| 1158 | 185 |
| 1159 | 186 # Set the first type of "horse" the script will draw. |
| 1160 | 187 HORSE_TYPE=2 |
| 1161 | 188 |
| 1162 | 189 # Create position-file and odds-file for every "horse". |
| 1163 | 190 #+ In these files, store the current position of the horse, |
| 1164 | 191 #+ the type and the odds. |
| 1165 | 192 for HN in `seq 9`; do |
| 1166 | 193 touch horse_${HN}_position |
| 1167 | 194 touch odds_${HN} |
| 1168 | 195 echo \-1 > horse_${HN}_position |
| 1169 | 196 echo $HORSE_TYPE >> horse_${HN}_position |
| 1170 | 197 # Define a random handicap for horse. |
| 1171 | 198 HANDICAP=`random_1_9` |
| 1172 | 199 # Check if the random_1_9 function returned a good value. |
| 1173 | 200 while ! echo $HANDICAP | grep [1-9] &> /dev/null; do |
| 1174 | 201 HANDICAP=`random_1_9` |
| 1175 | 202 done |
| 1176 | 203 # Define last handicap position for horse. |
| 1177 | 204 LHP=`expr $HANDICAP \* 7 + 3` |
| 1178 | 205 for FILE in `seq 10 7 $LHP`; do |
| 1179 | 206 echo $HN >> $FILE |
| 1180 | 207 done |
| 1181 | 208 |
| 1182 | 209 # Calculate odds. |
| 1183 | 210 case $HANDICAP in |
| 1184 | 211 1) ODDS=`echo $HANDICAP \* 0.25 + 1.25 | bc` |
| 1185 | 212 echo $ODDS > odds_${HN} |
| 1186 | 213 ;; |
| 1187 | 214 2 | 3) ODDS=`echo $HANDICAP \* 0.40 + 1.25 | bc` |
| 1188 | 215 echo $ODDS > odds_${HN} |
| 1189 | 216 ;; |
| 1190 | 217 4 | 5 | 6) ODDS=`echo $HANDICAP \* 0.55 + 1.25 | bc` |
| 1191 | 218 echo $ODDS > odds_${HN} |
| 1192 | 219 ;; |
| 1193 | 220 7 | 8) ODDS=`echo $HANDICAP \* 0.75 + 1.25 | bc` |
| 1194 | 221 echo $ODDS > odds_${HN} |
| 1195 | 222 ;; |
| 1196 | 223 9) ODDS=`echo $HANDICAP \* 0.90 + 1.25 | bc` |
| 1197 | 224 echo $ODDS > odds_${HN} |
| 1198 | 225 esac |
| 1199 | 226 |
| 1200 | 227 |
| 1201 | 228 done |
| 1202 | 229 |
| 1203 | 230 |
| 1204 | 231 # Print odds. |
| 1205 | 232 print_odds() { |
| 1206 | 233 tput cup 6 0 |
| 1207 | 234 echo -ne '\E[30;42m' |
| 1208 | 235 for HN in `seq 9`; do |
| 1209 | 236 echo "#$HN odds->" `cat odds_${HN}` |
| 1210 | 237 done |
| 1211 | 238 } |
| 1212 | 239 |
| 1213 | 240 # Draw the horses at starting line. |
| 1214 | 241 draw_horses() { |
| 1215 | 242 tput cup 6 0 |
| 1216 | 243 echo -ne '\E[30;42m' |
| 1217 | 244 for HN in `seq 9`; do |
| 1218 | 245 echo /\\$HN/\\" " |
| 1219 | 246 done |
| 1220 | 247 } |
| 1221 | 248 |
| 1222 | 249 print_odds |
| 1223 | 250 |
| 1224 | 251 echo -ne '\E[47m' |
| 1225 | 252 # Wait for a enter key press to start the race. |
| 1226 | 253 # The escape sequence '\E[?25l' disables the cursor. |
| 1227 | 254 tput cup 17 0 |
| 1228 | 255 echo -e '\E[?25l'Press [enter] key to start the race... |
| 1229 | 256 read -s |
| 1230 | 257 |
| 1231 | 258 # Disable normal echoing in the terminal. |
| 1232 | 259 # This avoids key presses that might "contaminate" the screen |
| 1233 | 260 #+ during the race. |
| 1234 | 261 stty -echo |
| 1235 | 262 |
| 1236 | 263 # -------------------------------------------------------- |
| 1237 | 264 # Start the race. |
| 1238 | 265 |
| 1239 | 266 draw_horses |
| 1240 | 267 echo -ne '\E[37;47m' |
| 1241 | 268 move_and_echo 18 1 $BLANK80 |
| 1242 | 269 echo -ne '\E[30m' |
| 1243 | 270 move_and_echo 18 1 Starting... |
| 1244 | 271 sleep 1 |
| 1245 | 272 |
| 1246 | 273 # Set the column of the finish line. |
| 1247 | 274 WINNING_POS=74 |
| 1248 | 275 |
| 1249 | 276 # Define the time the race started. |
| 1250 | 277 START_TIME=`date +%s` |
| 1251 | 278 |
| 1252 | 279 # COL variable needed by following "while" construct. |
| 1253 | 280 COL=0 |
| 1254 | 281 |
| 1255 | 282 while [ $COL -lt $WINNING_POS ]; do |
| 1256 | 283 |
| 1257 | 284 MOVE_HORSE=0 |
| 1258 | 285 |
| 1259 | 286 # Check if the random_1_9 function has returned a good value. |
| 1260 | 287 while ! echo $MOVE_HORSE | grep [1-9] &> /dev/null; do |
| 1261 | 288 MOVE_HORSE=`random_1_9` |
| 1262 | 289 done |
| 1263 | 290 |
| 1264 | 291 # Define old type and position of the "randomized horse". |
| 1265 | 292 HORSE_TYPE=`cat horse_${MOVE_HORSE}_position | tail -n 1` |
| 1266 | 293 COL=$(expr `cat horse_${MOVE_HORSE}_position | head -n 1`) |
| 1267 | 294 |
| 1268 | 295 ADD_POS=1 |
| 1269 | 296 # Check if the current position is an handicap position. |
| 1270 | 297 if seq 10 7 68 | grep -w $COL &> /dev/null; then |
| 1271 | 298 if grep -w $MOVE_HORSE $COL &> /dev/null; then |
| 1272 | 299 ADD_POS=0 |
| 1273 | 300 grep -v -w $MOVE_HORSE $COL > ${COL}_new |
| 1274 | 301 rm -f $COL |
| 1275 | 302 mv -f ${COL}_new $COL |
| 1276 | 303 else ADD_POS=1 |
| 1277 | 304 fi |
| 1278 | 305 else ADD_POS=1 |
| 1279 | 306 fi |
| 1280 | 307 COL=`expr $COL + $ADD_POS` |
| 1281 | 308 echo $COL > horse_${MOVE_HORSE}_position # Store new position. |
| 1282 | 309 |
| 1283 | 310 # Choose the type of horse to draw. |
| 1284 | 311 case $HORSE_TYPE in |
| 1285 | 312 1) HORSE_TYPE=2; DRAW_HORSE=draw_horse_two |
| 1286 | 313 ;; |
| 1287 | 314 2) HORSE_TYPE=1; DRAW_HORSE=draw_horse_one |
| 1288 | 315 esac |
| 1289 | 316 echo $HORSE_TYPE >> horse_${MOVE_HORSE}_position |
| 1290 | 317 # Store current type. |
| 1291 | 318 |
| 1292 | 319 # Set foreground color to black and background to green. |
| 1293 | 320 echo -ne '\E[30;42m' |
| 1294 | 321 |
| 1295 | 322 # Move the cursor to new horse position. |
| 1296 | 323 tput cup `expr $MOVE_HORSE + 5` \ |
| 1297 | 324 `cat horse_${MOVE_HORSE}_position | head -n 1` |
| 1298 | 325 |
| 1299 | 326 # Draw the horse. |
| 1300 | 327 $DRAW_HORSE |
| 1301 | 328 usleep $USLEEP_ARG |
| 1302 | 329 |
| 1303 | 330 # When all horses have gone beyond field line 15, reprint odds. |
| 1304 | 331 touch fieldline15 |
| 1305 | 332 if [ $COL = 15 ]; then |
| 1306 | 333 echo $MOVE_HORSE >> fieldline15 |
| 1307 | 334 fi |
| 1308 | 335 if [ `wc -l fieldline15 | cut -f1 -d " "` = 9 ]; then |
| 1309 | 336 print_odds |
| 1310 | 337 : > fieldline15 |
| 1311 | 338 fi |
| 1312 | 339 |
| 1313 | 340 # Define the leading horse. |
| 1314 | 341 HIGHEST_POS=`cat *position | sort -n | tail -1` |
| 1315 | 342 |
| 1316 | 343 # Set background color to white. |
| 1317 | 344 echo -ne '\E[47m' |
| 1318 | 345 tput cup 17 0 |
| 1319 | 346 echo -n Current leader: `grep -w $HIGHEST_POS *position | cut -c7`\ |
| 1320 | 347 " " |
| 1321 | 348 |
| 1322 | 349 done |
| 1323 | 350 |
| 1324 | 351 # Define the time the race finished. |
| 1325 | 352 FINISH_TIME=`date +%s` |
| 1326 | 353 |
| 1327 | 354 # Set background color to green and enable blinking text. |
| 1328 | 355 echo -ne '\E[30;42m' |
| 1329 | 356 echo -en '\E[5m' |
| 1330 | 357 |
| 1331 | 358 # Make the winning horse blink. |
| 1332 | 359 tput cup `expr $MOVE_HORSE + 5` \ |
| 1333 | 360 `cat horse_${MOVE_HORSE}_position | head -n 1` |
| 1334 | 361 $DRAW_HORSE |
| 1335 | 362 |
| 1336 | 363 # Disable blinking text. |
| 1337 | 364 echo -en '\E[25m' |
| 1338 | 365 |
| 1339 | 366 # Set foreground and background color to white. |
| 1340 | 367 echo -ne '\E[37;47m' |
| 1341 | 368 move_and_echo 18 1 $BLANK80 |
| 1342 | 369 |
| 1343 | 370 # Set foreground color to black. |
| 1344 | 371 echo -ne '\E[30m' |
| 1345 | 372 |
| 1346 | 373 # Make winner blink. |
| 1347 | 374 tput cup 17 0 |
| 1348 | 375 echo -e "\E[5mWINNER: $MOVE_HORSE\E[25m"" Odds: `cat odds_${MOVE_HORSE}`"\ |
| 1349 | 376 " Race time: `expr $FINISH_TIME - $START_TIME` secs" |
| 1350 | 377 |
| 1351 | 378 # Restore cursor and old colors. |
| 1352 | 379 echo -en "\E[?25h" |
| 1353 | 380 echo -en "\E[0m" |
| 1354 | 381 |
| 1355 | 382 # Restore echoing. |
| 1356 | 383 stty echo |
| 1357 | 384 |
| 1358 | 385 # Remove race temp directory. |
| 1359 | 386 rm -rf $HORSE_RACE_TMP_DIR |
| 1360 | 387 |
| 1361 | 388 tput cup 19 0 |
| 1362 | 389 |
| 1363 | 390 exit 0</PRE |
| 1364 | ></TD |
| 1365 | ></TR |
| 1366 | ></TABLE |
| 1367 | ><HR></DIV |
| 1368 | ><P |
| 1369 | >See also <A |
| 1370 | HREF="contributed-scripts.html#HASHEXAMPLE" |
| 1371 | >Example A-21</A |
| 1372 | >, <A |
| 1373 | HREF="contributed-scripts.html#HOMEWORK" |
| 1374 | >Example A-44</A |
| 1375 | >, <A |
| 1376 | HREF="contributed-scripts.html#SHOWALLC" |
| 1377 | >Example A-52</A |
| 1378 | >, and <A |
| 1379 | HREF="contributed-scripts.html#PETALS" |
| 1380 | >Example A-40</A |
| 1381 | >.</P |
| 1382 | ><DIV |
| 1383 | CLASS="CAUTION" |
| 1384 | ><TABLE |
| 1385 | CLASS="CAUTION" |
| 1386 | WIDTH="100%" |
| 1387 | BORDER="0" |
| 1388 | ><TR |
| 1389 | ><TD |
| 1390 | WIDTH="25" |
| 1391 | ALIGN="CENTER" |
| 1392 | VALIGN="TOP" |
| 1393 | ><IMG |
| 1394 | SRC="common/caution.png" |
| 1395 | HSPACE="5" |
| 1396 | ALT="Caution"></TD |
| 1397 | ><TD |
| 1398 | ALIGN="LEFT" |
| 1399 | VALIGN="TOP" |
| 1400 | ><P |
| 1401 | >There is, however, a major problem with all |
| 1402 | this. <SPAN |
| 1403 | CLASS="emphasis" |
| 1404 | ><I |
| 1405 | CLASS="EMPHASIS" |
| 1406 | >ANSI escape sequences are emphatically |
| 1407 | <A |
| 1408 | HREF="portabilityissues.html" |
| 1409 | >non-portable</A |
| 1410 | >.</I |
| 1411 | ></SPAN |
| 1412 | > |
| 1413 | What works fine on some terminal emulators (or the |
| 1414 | console) may work differently, or not at all, on others. |
| 1415 | A <SPAN |
| 1416 | CLASS="QUOTE" |
| 1417 | >"colorized"</SPAN |
| 1418 | > script that looks stunning on the |
| 1419 | script author's machine may produce unreadable output on |
| 1420 | someone else's. This somewhat compromises the usefulness of |
| 1421 | colorizing scripts, and possibly relegates this technique |
| 1422 | to the status of a gimmick. Colorized scripts are probably |
| 1423 | inappropriate in a commercial setting, i.e., your supervisor |
| 1424 | might disapprove.</P |
| 1425 | ></TD |
| 1426 | ></TR |
| 1427 | ></TABLE |
| 1428 | ></DIV |
| 1429 | ><P |
| 1430 | >Alister's <A |
| 1431 | HREF="http://code.google.com/p/ansi-color/" |
| 1432 | TARGET="_top" |
| 1433 | > ansi-color</A |
| 1434 | > utility (based on <A |
| 1435 | HREF="http://bash.deta.in/color-1.1.tar.gz" |
| 1436 | TARGET="_top" |
| 1437 | >Moshe |
| 1438 | Jacobson's color utility</A |
| 1439 | > considerably simplifies using |
| 1440 | ANSI escape sequences. It substitutes a clean and logical |
| 1441 | syntax for the clumsy constructs just discussed.</P |
| 1442 | ><P |
| 1443 | >Henry/teikedvl has likewise created a utility (<A |
| 1444 | HREF="http://scriptechocolor.sourceforge.net/" |
| 1445 | TARGET="_top" |
| 1446 | >http://scriptechocolor.sourceforge.net/</A |
| 1447 | >) to simplify creation of colorized scripts.</P |
| 1448 | ></DIV |
| 1449 | ><H3 |
| 1450 | CLASS="FOOTNOTES" |
| 1451 | >Notes</H3 |
| 1452 | ><TABLE |
| 1453 | BORDER="0" |
| 1454 | CLASS="FOOTNOTES" |
| 1455 | WIDTH="100%" |
| 1456 | ><TR |
| 1457 | ><TD |
| 1458 | ALIGN="LEFT" |
| 1459 | VALIGN="TOP" |
| 1460 | WIDTH="5%" |
| 1461 | ><A |
| 1462 | NAME="FTN.AEN20259" |
| 1463 | HREF="colorizing.html#AEN20259" |
| 1464 | >[1]</A |
| 1465 | ></TD |
| 1466 | ><TD |
| 1467 | ALIGN="LEFT" |
| 1468 | VALIGN="TOP" |
| 1469 | WIDTH="95%" |
| 1470 | ><P |
| 1471 | ><SPAN |
| 1472 | CLASS="ACRONYM" |
| 1473 | >ANSI</SPAN |
| 1474 | > is, of course, the |
| 1475 | acronym for the American National Standards |
| 1476 | Institute. This august body establishes and maintains |
| 1477 | various technical and industrial standards.</P |
| 1478 | ></TD |
| 1479 | ></TR |
| 1480 | ></TABLE |
| 1481 | ><DIV |
| 1482 | CLASS="NAVFOOTER" |
| 1483 | ><HR |
| 1484 | ALIGN="LEFT" |
| 1485 | WIDTH="100%"><TABLE |
| 1486 | SUMMARY="Footer navigation table" |
| 1487 | WIDTH="100%" |
| 1488 | BORDER="0" |
| 1489 | CELLPADDING="0" |
| 1490 | CELLSPACING="0" |
| 1491 | ><TR |
| 1492 | ><TD |
| 1493 | WIDTH="33%" |
| 1494 | ALIGN="left" |
| 1495 | VALIGN="top" |
| 1496 | ><A |
| 1497 | HREF="recursionsct.html" |
| 1498 | ACCESSKEY="P" |
| 1499 | >Prev</A |
| 1500 | ></TD |
| 1501 | ><TD |
| 1502 | WIDTH="34%" |
| 1503 | ALIGN="center" |
| 1504 | VALIGN="top" |
| 1505 | ><A |
| 1506 | HREF="index.html" |
| 1507 | ACCESSKEY="H" |
| 1508 | >Home</A |
| 1509 | ></TD |
| 1510 | ><TD |
| 1511 | WIDTH="33%" |
| 1512 | ALIGN="right" |
| 1513 | VALIGN="top" |
| 1514 | ><A |
| 1515 | HREF="optimizations.html" |
| 1516 | ACCESSKEY="N" |
| 1517 | >Next</A |
| 1518 | ></TD |
| 1519 | ></TR |
| 1520 | ><TR |
| 1521 | ><TD |
| 1522 | WIDTH="33%" |
| 1523 | ALIGN="left" |
| 1524 | VALIGN="top" |
| 1525 | >Recursion: a script calling itself</TD |
| 1526 | ><TD |
| 1527 | WIDTH="34%" |
| 1528 | ALIGN="center" |
| 1529 | VALIGN="top" |
| 1530 | ><A |
| 1531 | HREF="miscellany.html" |
| 1532 | ACCESSKEY="U" |
| 1533 | >Up</A |
| 1534 | ></TD |
| 1535 | ><TD |
| 1536 | WIDTH="33%" |
| 1537 | ALIGN="right" |
| 1538 | VALIGN="top" |
| 1539 | >Optimizations</TD |
| 1540 | ></TR |
| 1541 | ></TABLE |
| 1542 | ></DIV |
| 1543 | ></BODY |
| 1544 | ></HTML |
| 1545 | > |