File: colorizing.html

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