Debian Codegraph @

HTML/colorizing.html

raw symbols SCIP
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&nbsp;#!/bin/bash
147 2&nbsp;# ex30a.sh: "Colorized" version of ex30.sh.
148 3&nbsp;# Crude address database
149 4&nbsp;
150 5&nbsp;
151 6&nbsp;clear # Clear the screen.
152 7&nbsp;
153 8&nbsp;echo -n " "
154 9&nbsp;echo -e '\E[37;44m'"\033[1mContact List\033[0m"
155 10&nbsp; # White on blue background
156 11&nbsp;echo; echo
157 12&nbsp;echo -e "\033[1mChoose one of the following persons:\033[0m"
158 13&nbsp; # Bold
159 14&nbsp;tput sgr0 # Reset attributes.
160 15&nbsp;echo "(Enter only the first letter of name.)"
161 16&nbsp;echo
162 17&nbsp;echo -en '\E[47;34m'"\033[1mE\033[0m" # Blue
163 18&nbsp;tput sgr0 # Reset colors to "normal."
164 19&nbsp;echo "vans, Roland" # "[E]vans, Roland"
165 20&nbsp;echo -en '\E[47;35m'"\033[1mJ\033[0m" # Magenta
166 21&nbsp;tput sgr0
167 22&nbsp;echo "ambalaya, Mildred"
168 23&nbsp;echo -en '\E[47;32m'"\033[1mS\033[0m" # Green
169 24&nbsp;tput sgr0
170 25&nbsp;echo "mith, Julie"
171 26&nbsp;echo -en '\E[47;31m'"\033[1mZ\033[0m" # Red
172 27&nbsp;tput sgr0
173 28&nbsp;echo "ane, Morris"
174 29&nbsp;echo
175 30&nbsp;
176 31&nbsp;read person
177 32&nbsp;
178 33&nbsp;case "$person" in
179 34&nbsp;# Note variable is quoted.
180 35&nbsp;
181 36&nbsp; "E" | "e" )
182 37&nbsp; # Accept upper or lowercase input.
183 38&nbsp; echo
184 39&nbsp; echo "Roland Evans"
185 40&nbsp; echo "4321 Flash Dr."
186 41&nbsp; echo "Hardscrabble, CO 80753"
187 42&nbsp; echo "(303) 734-9874"
188 43&nbsp; echo "(303) 734-9892 fax"
189 44&nbsp; echo "[email protected]"
190 45&nbsp; echo "Business partner &#38; old friend"
191 46&nbsp; ;;
192 47&nbsp;
193 48&nbsp; "J" | "j" )
194 49&nbsp; echo
195 50&nbsp; echo "Mildred Jambalaya"
196 51&nbsp; echo "249 E. 7th St., Apt. 19"
197 52&nbsp; echo "New York, NY 10009"
198 53&nbsp; echo "(212) 533-2814"
199 54&nbsp; echo "(212) 533-9972 fax"
200 55&nbsp; echo "[email protected]"
201 56&nbsp; echo "Girlfriend"
202 57&nbsp; echo "Birthday: Feb. 11"
203 58&nbsp; ;;
204 59&nbsp;
205 60&nbsp;# Add info for Smith &#38; Zane later.
206 61&nbsp;
207 62&nbsp; * )
208 63&nbsp; # Default option.
209 64&nbsp; # Empty input (hitting RETURN) fits here, too.
210 65&nbsp; echo
211 66&nbsp; echo "Not yet in database."
212 67&nbsp; ;;
213 68&nbsp;
214 69&nbsp;esac
215 70&nbsp;
216 71&nbsp;tput sgr0 # Reset colors to "normal."
217 72&nbsp;
218 73&nbsp;echo
219 74&nbsp;
220 75&nbsp;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&nbsp;#!/bin/bash
243 2&nbsp;# Draw-box.sh: Drawing a box using ASCII characters.
244 3&nbsp;
245 4&nbsp;# Script by Stefano Palmeri, with minor editing by document author.
246 5&nbsp;# Minor edits suggested by Jim Angstadt.
247 6&nbsp;# Used in the ABS Guide with permission.
248 7&nbsp;
249 8&nbsp;
250 9&nbsp;######################################################################
251 10&nbsp;### draw_box function doc ###
252 11&nbsp;
253 12&nbsp;# The "draw_box" function lets the user
254 13&nbsp;#+ draw a box in a terminal.
255 14&nbsp;#
256 15&nbsp;# Usage: draw_box ROW COLUMN HEIGHT WIDTH [COLOR]
257 16&nbsp;# ROW and COLUMN represent the position
258 17&nbsp;#+ of the upper left angle of the box you're going to draw.
259 18&nbsp;# ROW and COLUMN must be greater than 0
260 19&nbsp;#+ and less than current terminal dimension.
261 20&nbsp;# HEIGHT is the number of rows of the box, and must be &#62; 0.
262 21&nbsp;# HEIGHT + ROW must be &#60;= than current terminal height.
263 22&nbsp;# WIDTH is the number of columns of the box and must be &#62; 0.
264 23&nbsp;# WIDTH + COLUMN must be &#60;= than current terminal width.
265 24&nbsp;#
266 25&nbsp;# E.g.: If your terminal dimension is 20x80,
267 26&nbsp;# draw_box 2 3 10 45 is good
268 27&nbsp;# draw_box 2 3 19 45 has bad HEIGHT value (19+2 &#62; 20)
269 28&nbsp;# draw_box 2 3 18 78 has bad WIDTH value (78+3 &#62; 80)
270 29&nbsp;#
271 30&nbsp;# COLOR is the color of the box frame.
272 31&nbsp;# This is the 5th argument and is optional.
273 32&nbsp;# 0=black 1=red 2=green 3=tan 4=blue 5=purple 6=cyan 7=white.
274 33&nbsp;# If you pass the function bad arguments,
275 34&nbsp;#+ it will just exit with code 65,
276 35&nbsp;#+ and no messages will be printed on stderr.
277 36&nbsp;#
278 37&nbsp;# Clear the terminal before you start to draw a box.
279 38&nbsp;# The clear command is not contained within the function.
280 39&nbsp;# This allows the user to draw multiple boxes, even overlapping ones.
281 40&nbsp;
282 41&nbsp;### end of draw_box function doc ###
283 42&nbsp;######################################################################
284 43&nbsp;
285 44&nbsp;draw_box(){
286 45&nbsp;
287 46&nbsp;#=============#
288 47&nbsp;HORZ="-"
289 48&nbsp;VERT="|"
290 49&nbsp;CORNER_CHAR="+"
291 50&nbsp;
292 51&nbsp;MINARGS=4
293 52&nbsp;E_BADARGS=65
294 53&nbsp;#=============#
295 54&nbsp;
296 55&nbsp;
297 56&nbsp;if [ $# -lt "$MINARGS" ]; then # If args are less than 4, exit.
298 57&nbsp; exit $E_BADARGS
299 58&nbsp;fi
300 59&nbsp;
301 60&nbsp;# Looking for non digit chars in arguments.
302 61&nbsp;# Probably it could be done better (exercise for the reader?).
303 62&nbsp;if echo $@ | tr -d [:blank:] | tr -d [:digit:] | grep . &#38;&#62; /dev/null; then
304 63&nbsp; exit $E_BADARGS
305 64&nbsp;fi
306 65&nbsp;
307 66&nbsp;BOX_HEIGHT=`expr $3 - 1` # -1 correction needed because angle char "+"
308 67&nbsp;BOX_WIDTH=`expr $4 - 1` #+ is a part of both box height and width.
309 68&nbsp;T_ROWS=`tput lines` # Define current terminal dimension
310 69&nbsp;T_COLS=`tput cols` #+ in rows and columns.
311 70&nbsp;
312 71&nbsp;if [ $1 -lt 1 ] || [ $1 -gt $T_ROWS ]; then # Start checking if arguments
313 72&nbsp; exit $E_BADARGS #+ are correct.
314 73&nbsp;fi
315 74&nbsp;if [ $2 -lt 1 ] || [ $2 -gt $T_COLS ]; then
316 75&nbsp; exit $E_BADARGS
317 76&nbsp;fi
318 77&nbsp;if [ `expr $1 + $BOX_HEIGHT + 1` -gt $T_ROWS ]; then
319 78&nbsp; exit $E_BADARGS
320 79&nbsp;fi
321 80&nbsp;if [ `expr $2 + $BOX_WIDTH + 1` -gt $T_COLS ]; then
322 81&nbsp; exit $E_BADARGS
323 82&nbsp;fi
324 83&nbsp;if [ $3 -lt 1 ] || [ $4 -lt 1 ]; then
325 84&nbsp; exit $E_BADARGS
326 85&nbsp;fi # End checking arguments.
327 86&nbsp;
328 87&nbsp;plot_char(){ # Function within a function.
329 88&nbsp; echo -e "\E[${1};${2}H"$3
330 89&nbsp;}
331 90&nbsp;
332 91&nbsp;echo -ne "\E[3${5}m" # Set box frame color, if defined.
333 92&nbsp;
334 93&nbsp;# start drawing the box
335 94&nbsp;
336 95&nbsp;count=1 # Draw vertical lines using
337 96&nbsp;for (( r=$1; count&#60;=$BOX_HEIGHT; r++)); do #+ plot_char function.
338 97&nbsp; plot_char $r $2 $VERT
339 98&nbsp; let count=count+1
340 99&nbsp;done
341 100&nbsp;
342 101&nbsp;count=1
343 102&nbsp;c=`expr $2 + $BOX_WIDTH`
344 103&nbsp;for (( r=$1; count&#60;=$BOX_HEIGHT; r++)); do
345 104&nbsp; plot_char $r $c $VERT
346 105&nbsp; let count=count+1
347 106&nbsp;done
348 107&nbsp;
349 108&nbsp;count=1 # Draw horizontal lines using
350 109&nbsp;for (( c=$2; count&#60;=$BOX_WIDTH; c++)); do #+ plot_char function.
351 110&nbsp; plot_char $1 $c $HORZ
352 111&nbsp; let count=count+1
353 112&nbsp;done
354 113&nbsp;
355 114&nbsp;count=1
356 115&nbsp;r=`expr $1 + $BOX_HEIGHT`
357 116&nbsp;for (( c=$2; count&#60;=$BOX_WIDTH; c++)); do
358 117&nbsp; plot_char $r $c $HORZ
359 118&nbsp; let count=count+1
360 119&nbsp;done
361 120&nbsp;
362 121&nbsp;plot_char $1 $2 $CORNER_CHAR # Draw box angles.
363 122&nbsp;plot_char $1 `expr $2 + $BOX_WIDTH` $CORNER_CHAR
364 123&nbsp;plot_char `expr $1 + $BOX_HEIGHT` $2 $CORNER_CHAR
365 124&nbsp;plot_char `expr $1 + $BOX_HEIGHT` `expr $2 + $BOX_WIDTH` $CORNER_CHAR
366 125&nbsp;
367 126&nbsp;echo -ne "\E[0m" # Restore old colors.
368 127&nbsp;
369 128&nbsp;P_ROWS=`expr $T_ROWS - 1` # Put the prompt at bottom of the terminal.
370 129&nbsp;
371 130&nbsp;echo -e "\E[${P_ROWS};1H"
372 131&nbsp;}
373 132&nbsp;
374 133&nbsp;
375 134&nbsp;# Now, let's try drawing a box.
376 135&nbsp;clear # Clear the terminal.
377 136&nbsp;R=2 # Row
378 137&nbsp;C=3 # Column
379 138&nbsp;H=10 # Height
380 139&nbsp;W=45 # Width
381 140&nbsp;col=1 # Color (red)
382 141&nbsp;draw_box $R $C $H $W $col # Draw the box.
383 142&nbsp;
384 143&nbsp;exit 0
385 144&nbsp;
386 145&nbsp;# Exercise:
387 146&nbsp;# --------
388 147&nbsp;# 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&nbsp;#!/bin/bash
883 2&nbsp;# color-echo.sh: Echoing text messages in color.
884 3&nbsp;
885 4&nbsp;# Modify this script for your own purposes.
886 5&nbsp;# It's easier than hand-coding color.
887 6&nbsp;
888 7&nbsp;black='\E[30;47m'
889 8&nbsp;red='\E[31;47m'
890 9&nbsp;green='\E[32;47m'
891 10&nbsp;yellow='\E[33;47m'
892 11&nbsp;blue='\E[34;47m'
893 12&nbsp;magenta='\E[35;47m'
894 13&nbsp;cyan='\E[36;47m'
895 14&nbsp;white='\E[37;47m'
896 15&nbsp;
897 16&nbsp;
898 17&nbsp;alias Reset="tput sgr0" # Reset text attributes to normal
899 18&nbsp; #+ without clearing screen.
900 19&nbsp;
901 20&nbsp;
902 21&nbsp;cecho () # Color-echo.
903 22&nbsp; # Argument $1 = message
904 23&nbsp; # Argument $2 = color
905 24&nbsp;{
906 25&nbsp;local default_msg="No message passed."
907 26&nbsp; # Doesn't really need to be a local variable.
908 27&nbsp;
909 28&nbsp;message=${1:-$default_msg} # Defaults to default message.
910 29&nbsp;color=${2:-$black} # Defaults to black, if not specified.
911 30&nbsp;
912 31&nbsp; echo -e "$color"
913 32&nbsp; echo "$message"
914 33&nbsp; Reset # Reset to normal.
915 34&nbsp;
916 35&nbsp; return
917 36&nbsp;}
918 37&nbsp;
919 38&nbsp;
920 39&nbsp;# Now, let's try it out.
921 40&nbsp;# ----------------------------------------------------
922 41&nbsp;cecho "Feeling blue..." $blue
923 42&nbsp;cecho "Magenta looks more like purple." $magenta
924 43&nbsp;cecho "Green with envy." $green
925 44&nbsp;cecho "Seeing red?" $red
926 45&nbsp;cecho "Cyan, more familiarly known as aqua." $cyan
927 46&nbsp;cecho "No color passed (defaults to black)."
928 47&nbsp; # Missing $color argument.
929 48&nbsp;cecho "\"Empty\" color passed (defaults to black)." ""
930 49&nbsp; # Empty $color argument.
931 50&nbsp;cecho
932 51&nbsp; # Missing $message and $color arguments.
933 52&nbsp;cecho "" ""
934 53&nbsp; # Empty $message and $color arguments.
935 54&nbsp;# ----------------------------------------------------
936 55&nbsp;
937 56&nbsp;echo
938 57&nbsp;
939 58&nbsp;exit 0
940 59&nbsp;
941 60&nbsp;# Exercises:
942 61&nbsp;# ---------
943 62&nbsp;# 1) Add the "bold" attribute to the 'cecho ()' function.
944 63&nbsp;# 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&nbsp;#!/bin/bash
975 2&nbsp;# horserace.sh: Very simple horserace simulation.
976 3&nbsp;# Author: Stefano Palmeri
977 4&nbsp;# Used with permission.
978 5&nbsp;
979 6&nbsp;################################################################
980 7&nbsp;# Goals of the script:
981 8&nbsp;# playing with escape sequences and terminal colors.
982 9&nbsp;#
983 10&nbsp;# Exercise:
984 11&nbsp;# Edit the script to make it run less randomly,
985 12&nbsp;#+ set up a fake betting shop . . .
986 13&nbsp;# Um . . . um . . . it's starting to remind me of a movie . . .
987 14&nbsp;#
988 15&nbsp;# The script gives each horse a random handicap.
989 16&nbsp;# The odds are calculated upon horse handicap
990 17&nbsp;#+ and are expressed in European(?) style.
991 18&nbsp;# E.g., odds=3.75 means that if you bet $1 and win,
992 19&nbsp;#+ you receive $3.75.
993 20&nbsp;#
994 21&nbsp;# The script has been tested with a GNU/Linux OS,
995 22&nbsp;#+ using xterm and rxvt, and konsole.
996 23&nbsp;# On a machine with an AMD 900 MHz processor,
997 24&nbsp;#+ the average race time is 75 seconds.
998 25&nbsp;# On faster computers the race time would be lower.
999 26&nbsp;# So, if you want more suspense, reset the USLEEP_ARG variable.
1000 27&nbsp;#
1001 28&nbsp;# Script by Stefano Palmeri.
1002 29&nbsp;################################################################
1003 30&nbsp;
1004 31&nbsp;E_RUNERR=65
1005 32&nbsp;
1006 33&nbsp;# Check if md5sum and bc are installed.
1007 34&nbsp;if ! which bc &#38;&#62; /dev/null; then
1008 35&nbsp; echo bc is not installed.
1009 36&nbsp; echo "Can\'t run . . . "
1010 37&nbsp; exit $E_RUNERR
1011 38&nbsp;fi
1012 39&nbsp;if ! which md5sum &#38;&#62; /dev/null; then
1013 40&nbsp; echo md5sum is not installed.
1014 41&nbsp; echo "Can\'t run . . . "
1015 42&nbsp; exit $E_RUNERR
1016 43&nbsp;fi
1017 44&nbsp;
1018 45&nbsp;# Set the following variable to slow down script execution.
1019 46&nbsp;# It will be passed as the argument for usleep (man usleep)
1020 47&nbsp;#+ and is expressed in microseconds (500000 = half a second).
1021 48&nbsp;USLEEP_ARG=0
1022 49&nbsp;
1023 50&nbsp;# Clean up the temp directory, restore terminal cursor and
1024 51&nbsp;#+ terminal colors -- if script interrupted by Ctl-C.
1025 52&nbsp;trap 'echo -en "\E[?25h"; echo -en "\E[0m"; stty echo;\
1026 53&nbsp;tput cup 20 0; rm -fr $HORSE_RACE_TMP_DIR' TERM EXIT
1027 54&nbsp;# See the chapter on debugging for an explanation of 'trap.'
1028 55&nbsp;
1029 56&nbsp;# Set a unique (paranoid) name for the temp directory the script needs.
1030 57&nbsp;HORSE_RACE_TMP_DIR=$HOME/.horserace-`date +%s`-`head -c10 /dev/urandom \
1031 58&nbsp;| md5sum | head -c30`
1032 59&nbsp;
1033 60&nbsp;# Create the temp directory and move right in.
1034 61&nbsp;mkdir $HORSE_RACE_TMP_DIR
1035 62&nbsp;cd $HORSE_RACE_TMP_DIR
1036 63&nbsp;
1037 64&nbsp;
1038 65&nbsp;# This function moves the cursor to line $1 column $2 and then prints $3.
1039 66&nbsp;# E.g.: "move_and_echo 5 10 linux" is equivalent to
1040 67&nbsp;#+ "tput cup 4 9; echo linux", but with one command instead of two.
1041 68&nbsp;# Note: "tput cup" defines 0 0 the upper left angle of the terminal,
1042 69&nbsp;#+ echo defines 1 1 the upper left angle of the terminal.
1043 70&nbsp;move_and_echo() {
1044 71&nbsp; echo -ne "\E[${1};${2}H""$3"
1045 72&nbsp;}
1046 73&nbsp;
1047 74&nbsp;# Function to generate a pseudo-random number between 1 and 9.
1048 75&nbsp;random_1_9 ()
1049 76&nbsp;{
1050 77&nbsp; head -c10 /dev/urandom | md5sum | tr -d [a-z] | tr -d 0 | cut -c1
1051 78&nbsp;}
1052 79&nbsp;
1053 80&nbsp;# Two functions that simulate "movement," when drawing the horses.
1054 81&nbsp;draw_horse_one() {
1055 82&nbsp; echo -n " "//$MOVE_HORSE//
1056 83&nbsp;}
1057 84&nbsp;draw_horse_two(){
1058 85&nbsp; echo -n " "\\\\$MOVE_HORSE\\\\
1059 86&nbsp;}
1060 87&nbsp;
1061 88&nbsp;
1062 89&nbsp;# Define current terminal dimension.
1063 90&nbsp;N_COLS=`tput cols`
1064 91&nbsp;N_LINES=`tput lines`
1065 92&nbsp;
1066 93&nbsp;# Need at least a 20-LINES X 80-COLUMNS terminal. Check it.
1067 94&nbsp;if [ $N_COLS -lt 80 ] || [ $N_LINES -lt 20 ]; then
1068 95&nbsp; echo "`basename $0` needs a 80-cols X 20-lines terminal."
1069 96&nbsp; echo "Your terminal is ${N_COLS}-cols X ${N_LINES}-lines."
1070 97&nbsp; exit $E_RUNERR
1071 98&nbsp;fi
1072 99&nbsp;
1073 100&nbsp;
1074 101&nbsp;# Start drawing the race field.
1075 102&nbsp;
1076 103&nbsp;# Need a string of 80 chars. See below.
1077 104&nbsp;BLANK80=`seq -s "" 100 | head -c80`
1078 105&nbsp;
1079 106&nbsp;clear
1080 107&nbsp;
1081 108&nbsp;# Set foreground and background colors to white.
1082 109&nbsp;echo -ne '\E[37;47m'
1083 110&nbsp;
1084 111&nbsp;# Move the cursor on the upper left angle of the terminal.
1085 112&nbsp;tput cup 0 0
1086 113&nbsp;
1087 114&nbsp;# Draw six white lines.
1088 115&nbsp;for n in `seq 5`; do
1089 116&nbsp; echo $BLANK80 # Use the 80 chars string to colorize the terminal.
1090 117&nbsp;done
1091 118&nbsp;
1092 119&nbsp;# Sets foreground color to black.
1093 120&nbsp;echo -ne '\E[30m'
1094 121&nbsp;
1095 122&nbsp;move_and_echo 3 1 "START 1"
1096 123&nbsp;move_and_echo 3 75 FINISH
1097 124&nbsp;move_and_echo 1 5 "|"
1098 125&nbsp;move_and_echo 1 80 "|"
1099 126&nbsp;move_and_echo 2 5 "|"
1100 127&nbsp;move_and_echo 2 80 "|"
1101 128&nbsp;move_and_echo 4 5 "| 2"
1102 129&nbsp;move_and_echo 4 80 "|"
1103 130&nbsp;move_and_echo 5 5 "V 3"
1104 131&nbsp;move_and_echo 5 80 "V"
1105 132&nbsp;
1106 133&nbsp;# Set foreground color to red.
1107 134&nbsp;echo -ne '\E[31m'
1108 135&nbsp;
1109 136&nbsp;# Some ASCII art.
1110 137&nbsp;move_and_echo 1 8 "..@@@..@@@@@...@@@@@.@...@..@@@@..."
1111 138&nbsp;move_and_echo 2 8 ".@...@...@.......@...@...@.@......."
1112 139&nbsp;move_and_echo 3 8 ".@@@@@...@.......@...@@@@@.@@@@...."
1113 140&nbsp;move_and_echo 4 8 ".@...@...@.......@...@...@.@......."
1114 141&nbsp;move_and_echo 5 8 ".@...@...@.......@...@...@..@@@@..."
1115 142&nbsp;move_and_echo 1 43 "@@@@...@@@...@@@@..@@@@..@@@@."
1116 143&nbsp;move_and_echo 2 43 "@...@.@...@.@.....@.....@....."
1117 144&nbsp;move_and_echo 3 43 "@@@@..@@@@@.@.....@@@@...@@@.."
1118 145&nbsp;move_and_echo 4 43 "@..@..@...@.@.....@.........@."
1119 146&nbsp;move_and_echo 5 43 "@...@.@...@..@@@@..@@@@.@@@@.."
1120 147&nbsp;
1121 148&nbsp;
1122 149&nbsp;# Set foreground and background colors to green.
1123 150&nbsp;echo -ne '\E[32;42m'
1124 151&nbsp;
1125 152&nbsp;# Draw eleven green lines.
1126 153&nbsp;tput cup 5 0
1127 154&nbsp;for n in `seq 11`; do
1128 155&nbsp; echo $BLANK80
1129 156&nbsp;done
1130 157&nbsp;
1131 158&nbsp;# Set foreground color to black.
1132 159&nbsp;echo -ne '\E[30m'
1133 160&nbsp;tput cup 5 0
1134 161&nbsp;
1135 162&nbsp;# Draw the fences.
1136 163&nbsp;echo "++++++++++++++++++++++++++++++++++++++\
1137 164&nbsp;++++++++++++++++++++++++++++++++++++++++++"
1138 165&nbsp;
1139 166&nbsp;tput cup 15 0
1140 167&nbsp;echo "++++++++++++++++++++++++++++++++++++++\
1141 168&nbsp;++++++++++++++++++++++++++++++++++++++++++"
1142 169&nbsp;
1143 170&nbsp;# Set foreground and background colors to white.
1144 171&nbsp;echo -ne '\E[37;47m'
1145 172&nbsp;
1146 173&nbsp;# Draw three white lines.
1147 174&nbsp;for n in `seq 3`; do
1148 175&nbsp; echo $BLANK80
1149 176&nbsp;done
1150 177&nbsp;
1151 178&nbsp;# Set foreground color to black.
1152 179&nbsp;echo -ne '\E[30m'
1153 180&nbsp;
1154 181&nbsp;# Create 9 files to stores handicaps.
1155 182&nbsp;for n in `seq 10 7 68`; do
1156 183&nbsp; touch $n
1157 184&nbsp;done
1158 185&nbsp;
1159 186&nbsp;# Set the first type of "horse" the script will draw.
1160 187&nbsp;HORSE_TYPE=2
1161 188&nbsp;
1162 189&nbsp;# Create position-file and odds-file for every "horse".
1163 190&nbsp;#+ In these files, store the current position of the horse,
1164 191&nbsp;#+ the type and the odds.
1165 192&nbsp;for HN in `seq 9`; do
1166 193&nbsp; touch horse_${HN}_position
1167 194&nbsp; touch odds_${HN}
1168 195&nbsp; echo \-1 &#62; horse_${HN}_position
1169 196&nbsp; echo $HORSE_TYPE &#62;&#62; horse_${HN}_position
1170 197&nbsp; # Define a random handicap for horse.
1171 198&nbsp; HANDICAP=`random_1_9`
1172 199&nbsp; # Check if the random_1_9 function returned a good value.
1173 200&nbsp; while ! echo $HANDICAP | grep [1-9] &#38;&#62; /dev/null; do
1174 201&nbsp; HANDICAP=`random_1_9`
1175 202&nbsp; done
1176 203&nbsp; # Define last handicap position for horse.
1177 204&nbsp; LHP=`expr $HANDICAP \* 7 + 3`
1178 205&nbsp; for FILE in `seq 10 7 $LHP`; do
1179 206&nbsp; echo $HN &#62;&#62; $FILE
1180 207&nbsp; done
1181 208&nbsp;
1182 209&nbsp; # Calculate odds.
1183 210&nbsp; case $HANDICAP in
1184 211&nbsp; 1) ODDS=`echo $HANDICAP \* 0.25 + 1.25 | bc`
1185 212&nbsp; echo $ODDS &#62; odds_${HN}
1186 213&nbsp; ;;
1187 214&nbsp; 2 | 3) ODDS=`echo $HANDICAP \* 0.40 + 1.25 | bc`
1188 215&nbsp; echo $ODDS &#62; odds_${HN}
1189 216&nbsp; ;;
1190 217&nbsp; 4 | 5 | 6) ODDS=`echo $HANDICAP \* 0.55 + 1.25 | bc`
1191 218&nbsp; echo $ODDS &#62; odds_${HN}
1192 219&nbsp; ;;
1193 220&nbsp; 7 | 8) ODDS=`echo $HANDICAP \* 0.75 + 1.25 | bc`
1194 221&nbsp; echo $ODDS &#62; odds_${HN}
1195 222&nbsp; ;;
1196 223&nbsp; 9) ODDS=`echo $HANDICAP \* 0.90 + 1.25 | bc`
1197 224&nbsp; echo $ODDS &#62; odds_${HN}
1198 225&nbsp; esac
1199 226&nbsp;
1200 227&nbsp;
1201 228&nbsp;done
1202 229&nbsp;
1203 230&nbsp;
1204 231&nbsp;# Print odds.
1205 232&nbsp;print_odds() {
1206 233&nbsp;tput cup 6 0
1207 234&nbsp;echo -ne '\E[30;42m'
1208 235&nbsp;for HN in `seq 9`; do
1209 236&nbsp; echo "#$HN odds-&#62;" `cat odds_${HN}`
1210 237&nbsp;done
1211 238&nbsp;}
1212 239&nbsp;
1213 240&nbsp;# Draw the horses at starting line.
1214 241&nbsp;draw_horses() {
1215 242&nbsp;tput cup 6 0
1216 243&nbsp;echo -ne '\E[30;42m'
1217 244&nbsp;for HN in `seq 9`; do
1218 245&nbsp; echo /\\$HN/\\" "
1219 246&nbsp;done
1220 247&nbsp;}
1221 248&nbsp;
1222 249&nbsp;print_odds
1223 250&nbsp;
1224 251&nbsp;echo -ne '\E[47m'
1225 252&nbsp;# Wait for a enter key press to start the race.
1226 253&nbsp;# The escape sequence '\E[?25l' disables the cursor.
1227 254&nbsp;tput cup 17 0
1228 255&nbsp;echo -e '\E[?25l'Press [enter] key to start the race...
1229 256&nbsp;read -s
1230 257&nbsp;
1231 258&nbsp;# Disable normal echoing in the terminal.
1232 259&nbsp;# This avoids key presses that might "contaminate" the screen
1233 260&nbsp;#+ during the race.
1234 261&nbsp;stty -echo
1235 262&nbsp;
1236 263&nbsp;# --------------------------------------------------------
1237 264&nbsp;# Start the race.
1238 265&nbsp;
1239 266&nbsp;draw_horses
1240 267&nbsp;echo -ne '\E[37;47m'
1241 268&nbsp;move_and_echo 18 1 $BLANK80
1242 269&nbsp;echo -ne '\E[30m'
1243 270&nbsp;move_and_echo 18 1 Starting...
1244 271&nbsp;sleep 1
1245 272&nbsp;
1246 273&nbsp;# Set the column of the finish line.
1247 274&nbsp;WINNING_POS=74
1248 275&nbsp;
1249 276&nbsp;# Define the time the race started.
1250 277&nbsp;START_TIME=`date +%s`
1251 278&nbsp;
1252 279&nbsp;# COL variable needed by following "while" construct.
1253 280&nbsp;COL=0
1254 281&nbsp;
1255 282&nbsp;while [ $COL -lt $WINNING_POS ]; do
1256 283&nbsp;
1257 284&nbsp; MOVE_HORSE=0
1258 285&nbsp;
1259 286&nbsp; # Check if the random_1_9 function has returned a good value.
1260 287&nbsp; while ! echo $MOVE_HORSE | grep [1-9] &#38;&#62; /dev/null; do
1261 288&nbsp; MOVE_HORSE=`random_1_9`
1262 289&nbsp; done
1263 290&nbsp;
1264 291&nbsp; # Define old type and position of the "randomized horse".
1265 292&nbsp; HORSE_TYPE=`cat horse_${MOVE_HORSE}_position | tail -n 1`
1266 293&nbsp; COL=$(expr `cat horse_${MOVE_HORSE}_position | head -n 1`)
1267 294&nbsp;
1268 295&nbsp; ADD_POS=1
1269 296&nbsp; # Check if the current position is an handicap position.
1270 297&nbsp; if seq 10 7 68 | grep -w $COL &#38;&#62; /dev/null; then
1271 298&nbsp; if grep -w $MOVE_HORSE $COL &#38;&#62; /dev/null; then
1272 299&nbsp; ADD_POS=0
1273 300&nbsp; grep -v -w $MOVE_HORSE $COL &#62; ${COL}_new
1274 301&nbsp; rm -f $COL
1275 302&nbsp; mv -f ${COL}_new $COL
1276 303&nbsp; else ADD_POS=1
1277 304&nbsp; fi
1278 305&nbsp; else ADD_POS=1
1279 306&nbsp; fi
1280 307&nbsp; COL=`expr $COL + $ADD_POS`
1281 308&nbsp; echo $COL &#62; horse_${MOVE_HORSE}_position # Store new position.
1282 309&nbsp;
1283 310&nbsp; # Choose the type of horse to draw.
1284 311&nbsp; case $HORSE_TYPE in
1285 312&nbsp; 1) HORSE_TYPE=2; DRAW_HORSE=draw_horse_two
1286 313&nbsp; ;;
1287 314&nbsp; 2) HORSE_TYPE=1; DRAW_HORSE=draw_horse_one
1288 315&nbsp; esac
1289 316&nbsp; echo $HORSE_TYPE &#62;&#62; horse_${MOVE_HORSE}_position
1290 317&nbsp; # Store current type.
1291 318&nbsp;
1292 319&nbsp; # Set foreground color to black and background to green.
1293 320&nbsp; echo -ne '\E[30;42m'
1294 321&nbsp;
1295 322&nbsp; # Move the cursor to new horse position.
1296 323&nbsp; tput cup `expr $MOVE_HORSE + 5` \
1297 324&nbsp; `cat horse_${MOVE_HORSE}_position | head -n 1`
1298 325&nbsp;
1299 326&nbsp; # Draw the horse.
1300 327&nbsp; $DRAW_HORSE
1301 328&nbsp; usleep $USLEEP_ARG
1302 329&nbsp;
1303 330&nbsp; # When all horses have gone beyond field line 15, reprint odds.
1304 331&nbsp; touch fieldline15
1305 332&nbsp; if [ $COL = 15 ]; then
1306 333&nbsp; echo $MOVE_HORSE &#62;&#62; fieldline15
1307 334&nbsp; fi
1308 335&nbsp; if [ `wc -l fieldline15 | cut -f1 -d " "` = 9 ]; then
1309 336&nbsp; print_odds
1310 337&nbsp; : &#62; fieldline15
1311 338&nbsp; fi
1312 339&nbsp;
1313 340&nbsp; # Define the leading horse.
1314 341&nbsp; HIGHEST_POS=`cat *position | sort -n | tail -1`
1315 342&nbsp;
1316 343&nbsp; # Set background color to white.
1317 344&nbsp; echo -ne '\E[47m'
1318 345&nbsp; tput cup 17 0
1319 346&nbsp; echo -n Current leader: `grep -w $HIGHEST_POS *position | cut -c7`\
1320 347&nbsp; " "
1321 348&nbsp;
1322 349&nbsp;done
1323 350&nbsp;
1324 351&nbsp;# Define the time the race finished.
1325 352&nbsp;FINISH_TIME=`date +%s`
1326 353&nbsp;
1327 354&nbsp;# Set background color to green and enable blinking text.
1328 355&nbsp;echo -ne '\E[30;42m'
1329 356&nbsp;echo -en '\E[5m'
1330 357&nbsp;
1331 358&nbsp;# Make the winning horse blink.
1332 359&nbsp;tput cup `expr $MOVE_HORSE + 5` \
1333 360&nbsp;`cat horse_${MOVE_HORSE}_position | head -n 1`
1334 361&nbsp;$DRAW_HORSE
1335 362&nbsp;
1336 363&nbsp;# Disable blinking text.
1337 364&nbsp;echo -en '\E[25m'
1338 365&nbsp;
1339 366&nbsp;# Set foreground and background color to white.
1340 367&nbsp;echo -ne '\E[37;47m'
1341 368&nbsp;move_and_echo 18 1 $BLANK80
1342 369&nbsp;
1343 370&nbsp;# Set foreground color to black.
1344 371&nbsp;echo -ne '\E[30m'
1345 372&nbsp;
1346 373&nbsp;# Make winner blink.
1347 374&nbsp;tput cup 17 0
1348 375&nbsp;echo -e "\E[5mWINNER: $MOVE_HORSE\E[25m"" Odds: `cat odds_${MOVE_HORSE}`"\
1349 376&nbsp;" Race time: `expr $FINISH_TIME - $START_TIME` secs"
1350 377&nbsp;
1351 378&nbsp;# Restore cursor and old colors.
1352 379&nbsp;echo -en "\E[?25h"
1353 380&nbsp;echo -en "\E[0m"
1354 381&nbsp;
1355 382&nbsp;# Restore echoing.
1356 383&nbsp;stty echo
1357 384&nbsp;
1358 385&nbsp;# Remove race temp directory.
1359 386&nbsp;rm -rf $HORSE_RACE_TMP_DIR
1360 387&nbsp;
1361 388&nbsp;tput cup 19 0
1362 389&nbsp;
1363 390&nbsp;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 >