Debian Codegraph @

HTML/writingscripts.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Writing 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="Exercises"
15 HREF="exercises.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Exercises"
18 HREF="exercises.html"><LINK
19 REL="NEXT"
20 TITLE="Revision History"
21 HREF="revisionhistory.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="exercises.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Appendix O. Exercises</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="revisionhistory.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="WRITINGSCRIPTS"
96 ></A
97 >O.2. Writing Scripts</H1
98 ><P
99 ><A
100 NAME="WRITINGSCRIPTS1"
101 ></A
102 ></P
103 ><P
104 >Write a script to carry out each of the following tasks.</P
105 ><DIV
106 CLASS="VARIABLELIST"
107 ><P
108 ><B
109 ><A
110 NAME="EXEASY1"
111 ></A
112 >EASY</B
113 ></P
114 ><DL
115 ><DT
116 ><B
117 CLASS="COMMAND"
118 >Self-reproducing Script</B
119 ></DT
120 ><DD
121 ><P
122 >Write a script that backs itself up, that is, copies
123 itself to a file named <TT
124 CLASS="FILENAME"
125 >backup.sh</TT
126 >.</P
127 ><P
128 >Hint: Use the <A
129 HREF="external.html#CATREF"
130 >cat</A
131 > command
132 and the appropriate <A
133 HREF="othertypesv.html#SCRNAMEPARAM"
134 >positional
135 parameter</A
136 >.</P
137 ></DD
138 ><DT
139 ><B
140 CLASS="COMMAND"
141 >Home Directory Listing</B
142 ></DT
143 ><DD
144 ><P
145 >Perform a recursive directory listing on the user's home
146 directory and save the information to a file. Compress
147 the file, have the script prompt the user to insert
148 a USB flash drive, then press <B
149 CLASS="KEYCAP"
150 >ENTER</B
151 >.
152 Finally, save the file to the flash drive after making
153 certain the flash drive has properly mounted by parsing
154 the output of <A
155 HREF="system.html#DFREF"
156 >df</A
157 >. Note that
158 the flash drive must be <I
159 CLASS="FIRSTTERM"
160 >unmounted</I
161 >
162 before it is removed.</P
163 ></DD
164 ><DT
165 ><B
166 CLASS="COMMAND"
167 >Converting <A
168 HREF="loops.html#FORLOOPREF1"
169 >for</A
170 >
171 loops to <A
172 HREF="loops.html#WHILELOOPREF"
173 >while</A
174 > and <A
175 HREF="loops.html#UNTILLOOPREF"
176 >until</A
177 > loops</B
178 ></DT
179 ><DD
180 ><P
181 >Convert the <I
182 CLASS="FIRSTTERM"
183 >for loops</I
184 > in <A
185 HREF="loops.html#EX22"
186 >Example 11-1</A
187 > to <I
188 CLASS="FIRSTTERM"
189 >while
190 loops</I
191 >. Hint: store the data in an <A
192 HREF="arrays.html#ARRAYREF"
193 >array</A
194 > and step through the array
195 elements.</P
196 ><P
197 >Having already done the <SPAN
198 CLASS="QUOTE"
199 >"heavy lifting,"</SPAN
200 >
201 now convert the loops in the example to <I
202 CLASS="FIRSTTERM"
203 > until
204 loops</I
205 >.</P
206 ></DD
207 ><DT
208 ><B
209 CLASS="COMMAND"
210 >Changing the line spacing of a text file</B
211 ></DT
212 ><DD
213 ><P
214 >Write a script that reads each line of a target file, then
215 writes the line back to <TT
216 CLASS="FILENAME"
217 >stdout</TT
218 >, but with
219 an extra blank line following. This has the effect of
220 <SPAN
221 CLASS="emphasis"
222 ><I
223 CLASS="EMPHASIS"
224 >double-spacing</I
225 ></SPAN
226 > the file.</P
227 ><P
228 >Include all necessary code to check whether the script
229 gets the necessary command-line argument (a filename),
230 and whether the specified file exists.</P
231 ><P
232 >When the script runs correctly, modify it to
233 <SPAN
234 CLASS="emphasis"
235 ><I
236 CLASS="EMPHASIS"
237 >triple-space</I
238 ></SPAN
239 > the target file.</P
240 ><P
241 >Finally, write a script to remove all blank lines from
242 the target file, <SPAN
243 CLASS="emphasis"
244 ><I
245 CLASS="EMPHASIS"
246 >single-spacing</I
247 ></SPAN
248 > it.</P
249 ></DD
250 ><DT
251 ><B
252 CLASS="COMMAND"
253 >Backwards Listing</B
254 ></DT
255 ><DD
256 ><P
257 >Write a script that echoes itself to
258 <TT
259 CLASS="FILENAME"
260 >stdout</TT
261 >, but
262 <SPAN
263 CLASS="emphasis"
264 ><I
265 CLASS="EMPHASIS"
266 >backwards</I
267 ></SPAN
268 >.</P
269 ></DD
270 ><DT
271 ><B
272 CLASS="COMMAND"
273 >Automatically Decompressing Files</B
274 ></DT
275 ><DD
276 ><P
277 >Given a list of filenames as input, this script
278 queries each target file (parsing the output of the
279 <A
280 HREF="filearchiv.html#FILEREF"
281 >file</A
282 > command) for
283 the type of compression used on it. Then the script
284 automatically invokes the appropriate decompression command
285 (<B
286 CLASS="COMMAND"
287 >gunzip</B
288 >, <B
289 CLASS="COMMAND"
290 >bunzip2</B
291 >,
292 <B
293 CLASS="COMMAND"
294 >unzip</B
295 >, <B
296 CLASS="COMMAND"
297 >uncompress</B
298 >,
299 or whatever). If a target file is not compressed, the
300 script emits a warning message, but takes no other action
301 on that particular file.</P
302 ></DD
303 ><DT
304 ><B
305 CLASS="COMMAND"
306 >Unique System ID</B
307 ></DT
308 ><DD
309 ><P
310 >Generate a <SPAN
311 CLASS="QUOTE"
312 >"unique"</SPAN
313 > 6-digit hexadecimal
314 identifier for your computer. Do <SPAN
315 CLASS="emphasis"
316 ><I
317 CLASS="EMPHASIS"
318 >not</I
319 ></SPAN
320 >
321 use the flawed <A
322 HREF="system.html#HOSTIDREF"
323 >hostid</A
324 >
325 command. Hint: <B
326 CLASS="COMMAND"
327 ><A
328 HREF="filearchiv.html#MD5SUMREF"
329 >md5sum</A
330 >
331 <A
332 HREF="files.html#DATAFILESREF1"
333 ><TT
334 CLASS="FILENAME"
335 >/etc/passwd</TT
336 ></A
337 ></B
338 >,
339 then select the first 6 digits of output.</P
340 ></DD
341 ><DT
342 ><B
343 CLASS="COMMAND"
344 >Backup</B
345 ></DT
346 ><DD
347 ><P
348 >Archive as a <SPAN
349 CLASS="QUOTE"
350 >"tarball"</SPAN
351 >
352 (<TT
353 CLASS="FILENAME"
354 >*.tar.gz</TT
355 > file) all the files
356 in your home directory tree
357 (<TT
358 CLASS="FILENAME"
359 >/home/your-name</TT
360 >) that have
361 been modified in the last 24 hours. Hint: use <A
362 HREF="moreadv.html#FINDREF"
363 >find</A
364 >.</P
365 ><P
366 >Optional: you may use this as the basis of a
367 <I
368 CLASS="FIRSTTERM"
369 >backup</I
370 > script.</P
371 ></DD
372 ><DT
373 ><B
374 CLASS="COMMAND"
375 >Checking whether a process is still running</B
376 ></DT
377 ><DD
378 ><P
379 >Given a <A
380 HREF="special-chars.html#PROCESSIDREF"
381 >process ID</A
382 >
383 (<I
384 CLASS="FIRSTTERM"
385 >PID</I
386 >) as an argument, this script
387 will check, at user-specified intervals, whether
388 the given process is still running. You may use
389 the <A
390 HREF="system.html#PPSSREF"
391 >ps</A
392 > and <A
393 HREF="timedate.html#SLEEPREF"
394 >sleep</A
395 > commands.</P
396 ></DD
397 ><DT
398 ><B
399 CLASS="COMMAND"
400 >Primes</B
401 ></DT
402 ><DD
403 ><P
404 >Print (to <TT
405 CLASS="FILENAME"
406 >stdout</TT
407 >) all
408 prime numbers between 60000 and 63000. The output
409 should be nicely formatted in columns (hint:
410 use <A
411 HREF="internal.html#PRINTFREF"
412 >printf</A
413 >).</P
414 ></DD
415 ><DT
416 ><B
417 CLASS="COMMAND"
418 >Lottery Numbers</B
419 ></DT
420 ><DD
421 ><P
422 >One type of lottery involves picking five
423 different numbers, in the range of 1 - 50. Write a
424 script that generates five pseudorandom numbers in this
425 range, <SPAN
426 CLASS="emphasis"
427 ><I
428 CLASS="EMPHASIS"
429 >with no duplicates</I
430 ></SPAN
431 >. The
432 script will give the option of echoing the numbers to
433 <TT
434 CLASS="FILENAME"
435 >stdout</TT
436 > or saving them to a file,
437 along with the date and time the particular number set
438 was generated. (If your script consistently generates
439 <SPAN
440 CLASS="emphasis"
441 ><I
442 CLASS="EMPHASIS"
443 >winning</I
444 ></SPAN
445 > lottery numbers, then you
446 can retire on the proceeds and leave shell scripting to
447 those of us who have to work for a living.)</P
448 ></DD
449 ></DL
450 ></DIV
451 ><DIV
452 CLASS="VARIABLELIST"
453 ><P
454 ><B
455 ><A
456 NAME="EXMEDIUM1"
457 ></A
458 >INTERMEDIATE</B
459 ></P
460 ><DL
461 ><DT
462 ><B
463 CLASS="COMMAND"
464 >Integer or String</B
465 ></DT
466 ><DD
467 ><P
468 >Write a script <A
469 HREF="functions.html#FUNCTIONREF"
470 >function</A
471 >
472 that determines if an argument passed to it is an integer
473 or a string. The function will return TRUE (0) if
474 passed an integer, and FALSE (1) if passed a string.</P
475 ><P
476 >Hint: What does the following expression return
477 when <TT
478 CLASS="VARNAME"
479 >$1</TT
480 > is <SPAN
481 CLASS="emphasis"
482 ><I
483 CLASS="EMPHASIS"
484 >not</I
485 ></SPAN
486 >
487 an integer?</P
488 ><P
489 ><TT
490 CLASS="VARNAME"
491 >expr $1 + 0</TT
492 ></P
493 ></DD
494 ><DT
495 ><B
496 CLASS="COMMAND"
497 ><A
498 HREF="special-chars.html#ASCIIDEF"
499 >ASCII</A
500 >
501 to Integer</B
502 ></DT
503 ><DD
504 ><P
505 >The <I
506 CLASS="FIRSTTERM"
507 >atoi</I
508 > function in
509 <B
510 CLASS="COMMAND"
511 >C</B
512 > converts a string character to
513 an integer. Write a shell script function that performs
514 the same operation. Likewise, write a shell script function
515 that does the inverse, mirroring the <B
516 CLASS="COMMAND"
517 >C</B
518 >
519 <I
520 CLASS="FIRSTTERM"
521 >itoa</I
522 > function which converts an
523 integer into an ASCII character.</P
524 ></DD
525 ><DT
526 ><B
527 CLASS="COMMAND"
528 >Managing Disk Space</B
529 ></DT
530 ><DD
531 ><P
532 >List, one at a time, all files larger than 100K in
533 the <TT
534 CLASS="FILENAME"
535 >/home/username</TT
536 >
537 directory tree. Give the user the option to delete or
538 compress the file, then proceed to show the next one. Write
539 to a logfile the names of all deleted files and the
540 deletion times.</P
541 ></DD
542 ><DT
543 ><B
544 CLASS="COMMAND"
545 >Banner</B
546 ></DT
547 ><DD
548 ><P
549 >Simulate the functionality of the deprecated <A
550 HREF="extmisc.html#BANNERREF"
551 >banner</A
552 > command in a script.</P
553 ></DD
554 ><DT
555 ><B
556 CLASS="COMMAND"
557 >Removing Inactive Accounts</B
558 ></DT
559 ><DD
560 ><P
561 >Inactive accounts on a network server waste disk space and may
562 become a security risk. Write an administrative script
563 (to be invoked by <I
564 CLASS="FIRSTTERM"
565 >root</I
566 > or the <A
567 HREF="system.html#CRONREF"
568 >cron daemon</A
569 >) that checks
570 for and deletes user accounts that have not been accessed
571 within the last 90 days.</P
572 ></DD
573 ><DT
574 ><B
575 CLASS="COMMAND"
576 >Enforcing Disk Quotas</B
577 ></DT
578 ><DD
579 ><P
580 >Write a script for a multi-user system that checks users'
581 disk usage. If a user surpasses a preset limit
582 (500 MB, for example) in her <TT
583 CLASS="FILENAME"
584 >/home/username</TT
585 >
586 directory, then the script automatically sends her a
587 <SPAN
588 CLASS="QUOTE"
589 >"pigout"</SPAN
590 > warning e-mail.</P
591 ><P
592 >The
593 script will use the <A
594 HREF="system.html#DUREF"
595 >du</A
596 >
597 and <A
598 HREF="communications.html#COMMMAIL1"
599 >mail</A
600 > commands. As
601 an option, it will allow setting and enforcing quotas
602 using the <A
603 HREF="system.html#QUOTAREF"
604 >quota</A
605 > and <A
606 HREF="system.html#SETQUOTAREF"
607 >setquota</A
608 > commands.</P
609 ></DD
610 ><DT
611 ><B
612 CLASS="COMMAND"
613 >Logged in User Information</B
614 ></DT
615 ><DD
616 ><P
617 >For all logged in users, show their real names and the time
618 and date of their last login.</P
619 ><P
620 >Hint: use <A
621 HREF="system.html#WHOREF"
622 >who</A
623 >,
624 <A
625 HREF="system.html#LASTLOGREF"
626 >lastlog</A
627 >,
628 and parse <A
629 HREF="files.html#DATAFILESREF1"
630 ><TT
631 CLASS="FILENAME"
632 >/etc/passwd</TT
633 ></A
634 >.</P
635 ></DD
636 ><DT
637 ><B
638 CLASS="COMMAND"
639 >Safe Delete</B
640 ></DT
641 ><DD
642 ><P
643 >Implement, as a script, a <SPAN
644 CLASS="QUOTE"
645 >"safe"</SPAN
646 > delete
647 command, <TT
648 CLASS="FILENAME"
649 >sdel.sh</TT
650 >. Filenames passed as
651 command-line arguments to this script are not deleted,
652 but instead <A
653 HREF="filearchiv.html#GZIPREF"
654 >gzipped</A
655 >
656 if not already compressed (use <A
657 HREF="filearchiv.html#FILEREF"
658 >file</A
659 > to check), then moved
660 to a <TT
661 CLASS="FILENAME"
662 >~/TRASH</TT
663 >
664 directory. Upon invocation, the script checks the <TT
665 CLASS="FILENAME"
666 >~/TRASH</TT
667 > directory for files
668 older than 48 hours and <A
669 HREF="external.html#RMREF"
670 >permanently
671 deletes</A
672 > them. (An better alternative might be to
673 have a second script handle this, periodically invoked
674 by the <A
675 HREF="system.html#CRONREF"
676 >cron daemon</A
677 >.)</P
678 ><P
679 ><SPAN
680 CLASS="emphasis"
681 ><I
682 CLASS="EMPHASIS"
683 >Extra credit:</I
684 ></SPAN
685 > Write the script
686 so it can handle files and directories <A
687 HREF="external.html#RMRECURS"
688 >recursively</A
689 >. This would give it
690 the capability of <SPAN
691 CLASS="QUOTE"
692 >"safely deleting"</SPAN
693 > entire
694 directory structures.</P
695 ></DD
696 ><DT
697 ><B
698 CLASS="COMMAND"
699 >Making Change</B
700 ></DT
701 ><DD
702 ><P
703 >What is the most efficient way to make change for $1.68,
704 using only coins in common circulations (up to 25c)? It's
705 6 quarters, 1 dime, a nickel, and three cents.</P
706 ><P
707 >Given any arbitrary command-line input in dollars and
708 cents ($*.??), calculate the change, using the minimum
709 number of coins. If your home country is not the United
710 States, you may use your local currency units instead. The
711 script will need to parse the command-line input, then
712 change it to multiples of the smallest monetary unit (cents
713 or whatever). Hint: look at <A
714 HREF="functions.html#EX61"
715 >Example 24-8</A
716 >.</P
717 ></DD
718 ><DT
719 ><B
720 CLASS="COMMAND"
721 >Quadratic Equations</B
722 ></DT
723 ><DD
724 ><P
725 >Solve a <I
726 CLASS="FIRSTTERM"
727 >quadratic</I
728 > equation of the form
729 <TT
730 CLASS="PARAMETER"
731 ><I
732 >Ax^2 + Bx + C = 0</I
733 ></TT
734 >. Have a script take
735 as arguments the coefficients, <TT
736 CLASS="USERINPUT"
737 ><B
738 >A</B
739 ></TT
740 >,
741 <TT
742 CLASS="USERINPUT"
743 ><B
744 >B</B
745 ></TT
746 >, and <TT
747 CLASS="USERINPUT"
748 ><B
749 >C</B
750 ></TT
751 >,
752 and return the solutions to five decimal places.</P
753 ><P
754 >Hint: pipe the coefficients to <A
755 HREF="mathc.html#BCREF"
756 >bc</A
757 >, using the well-known formula,
758 <TT
759 CLASS="PARAMETER"
760 ><I
761 >x = ( -B +/- sqrt( B^2 - 4AC ) ) / 2A</I
762 ></TT
763 >.</P
764 ></DD
765 ><DT
766 ><B
767 CLASS="COMMAND"
768 >Table of Logarithms</B
769 ></DT
770 ><DD
771 ><P
772 >Using the <A
773 HREF="mathc.html#BCREF"
774 >bc</A
775 > and <A
776 HREF="internal.html#PRINTFREF"
777 >printf</A
778 > commands, print out a
779 nicely-formatted table of eight-place natural logarithms
780 in the interval between 0.00 and 100.00, in steps of
781 .01.</P
782 ><P
783 >Hint: <I
784 CLASS="FIRSTTERM"
785 >bc</I
786 > requires the
787 <TT
788 CLASS="OPTION"
789 >-l</TT
790 > option to load the math library.</P
791 ></DD
792 ><DT
793 ><B
794 CLASS="COMMAND"
795 >Unicode Table</B
796 ></DT
797 ><DD
798 ><P
799 >Using <A
800 HREF="asciitable.html#ASCIISH"
801 >Example T-1</A
802 > as a template,
803 write a script that prints to a file a complete
804 <A
805 HREF="bashver4.html#UNICODEREF"
806 >Unicode</A
807 > table.</P
808 ><P
809 >Hint: Use the <TT
810 CLASS="OPTION"
811 >-e</TT
812 > option to
813 <A
814 HREF="internal.html#ECHOREF"
815 >echo</A
816 >:
817 <B
818 CLASS="COMMAND"
819 >echo -e '\uXXXX'</B
820 >, where
821 <TT
822 CLASS="REPLACEABLE"
823 ><I
824 >XXXX</I
825 ></TT
826 >
827 is the Unicode numerical character designation.
828 This requires <A
829 HREF="bashver4.html#BASH42"
830 >version 4.2</A
831 >
832 or later of Bash.</P
833 ></DD
834 ><DT
835 ><B
836 CLASS="COMMAND"
837 >Sum of Matching Numbers</B
838 ></DT
839 ><DD
840 ><P
841 >Find the sum of all five-digit numbers (in the range
842 10000 - 99999) containing <SPAN
843 CLASS="emphasis"
844 ><I
845 CLASS="EMPHASIS"
846 >exactly two</I
847 ></SPAN
848 >
849 out of the following set of digits: { 4, 5, 6 }. These may
850 repeat within the same number, and if so, they count once
851 for each occurrence.</P
852 ><P
853 >Some examples of <I
854 CLASS="FIRSTTERM"
855 >matching numbers</I
856 > are
857 42057, 74638, and 89515.</P
858 ></DD
859 ><DT
860 ><B
861 CLASS="COMMAND"
862 >Lucky Numbers</B
863 ></DT
864 ><DD
865 ><P
866 >A <I
867 CLASS="FIRSTTERM"
868 >lucky number</I
869 > is one whose
870 individual digits add up to 7, in successive additions. For
871 example, 62431 is a <I
872 CLASS="FIRSTTERM"
873 >lucky number</I
874 >
875 (6 + 2 + 4 + 3 + 1 = 16, 1 + 6 = 7). Find all the
876 <I
877 CLASS="FIRSTTERM"
878 >lucky numbers</I
879 > between 1000 and
880 10000.</P
881 ></DD
882 ><DT
883 ><B
884 CLASS="COMMAND"
885 >Craps</B
886 ></DT
887 ><DD
888 ><P
889 >Borrowing the ASCII graphics from <A
890 HREF="contributed-scripts.html#PETALS"
891 >Example A-40</A
892 >,
893 write a script that plays the well-known gambling game of
894 <I
895 CLASS="FIRSTTERM"
896 >craps</I
897 >. The script will accept bets
898 from one or more players, roll the dice, and keep track of
899 wins and losses, as well as of each player's bankroll.</P
900 ></DD
901 ><DT
902 ><B
903 CLASS="COMMAND"
904 >Tic-tac-toe</B
905 ></DT
906 ><DD
907 ><P
908 >Write a script that plays the child's game of
909 <I
910 CLASS="FIRSTTERM"
911 >tic-tac-toe</I
912 > against a human
913 player. The script will let the human choose whether
914 to take the first move. The script will follow
915 an optimal strategy, and therefore never lose. To simplify
916 matters, you may use ASCII graphics:</P
917 ><P
918 ><TABLE
919 BORDER="0"
920 BGCOLOR="#E0E0E0"
921 WIDTH="90%"
922 ><TR
923 ><TD
924 ><PRE
925 CLASS="PROGRAMLISTING"
926 > 1&nbsp; o | x |
927 2&nbsp; ----------
928 3&nbsp; | x |
929 4&nbsp; ----------
930 5&nbsp; | o |
931 6&nbsp;
932 7&nbsp; Your move, human (row, column)?</PRE
933 ></TD
934 ></TR
935 ></TABLE
936 ></P
937 ></DD
938 ><DT
939 ><B
940 CLASS="COMMAND"
941 >Alphabetizing a String</B
942 ></DT
943 ><DD
944 ><P
945 >Alphabetize (in ASCII order) an arbitrary string
946 read from the command-line.</P
947 ></DD
948 ><DT
949 ><B
950 CLASS="COMMAND"
951 >Parsing</B
952 ></DT
953 ><DD
954 ><P
955 >Parse <A
956 HREF="files.html#DATAFILESREF1"
957 ><TT
958 CLASS="FILENAME"
959 >/etc/passwd</TT
960 ></A
961 >,
962 and output its contents in nice, easy-to-read tabular
963 form.</P
964 ></DD
965 ><DT
966 ><B
967 CLASS="COMMAND"
968 >Logging Logins</B
969 ></DT
970 ><DD
971 ><P
972 >Parse <TT
973 CLASS="FILENAME"
974 >/var/log/messages</TT
975 > to
976 produce a nicely formatted file of user logins and login
977 times. The script may need to run as
978 <I
979 CLASS="FIRSTTERM"
980 >root</I
981 >. (Hint: Search for the string
982 <SPAN
983 CLASS="QUOTE"
984 >"LOGIN."</SPAN
985 >)</P
986 ></DD
987 ><DT
988 ><B
989 CLASS="COMMAND"
990 >Pretty-Printing a Data File</B
991 ></DT
992 ><DD
993 ><P
994 >Certain database and spreadsheet packages use
995 save-files with the fields separated by commas, commonly
996 referred to as <I
997 CLASS="FIRSTTERM"
998 >comma-separated values</I
999 >
1000 or CSVs. Other applications often need to parse these
1001 files.</P
1002 ><P
1003 >Given a data file with comma-separated
1004 <A
1005 HREF="special-chars.html#FIELDREF"
1006 >fields</A
1007 >, of the form:
1008 <TABLE
1009 BORDER="0"
1010 BGCOLOR="#E0E0E0"
1011 WIDTH="90%"
1012 ><TR
1013 ><TD
1014 ><PRE
1015 CLASS="PROGRAMLISTING"
1016 > 1&nbsp;Jones,Bill,235 S. Williams St.,Denver,CO,80221,(303) 244-7989
1017 2&nbsp;Smith,Tom,404 Polk Ave.,Los Angeles,CA,90003,(213) 879-5612
1018 3&nbsp;...</PRE
1019 ></TD
1020 ></TR
1021 ></TABLE
1022 >
1023 Reformat the data and print it out to
1024 <TT
1025 CLASS="FILENAME"
1026 >stdout</TT
1027 > in labeled, evenly-spaced columns.</P
1028 ></DD
1029 ><DT
1030 ><B
1031 CLASS="COMMAND"
1032 >Justification</B
1033 ></DT
1034 ><DD
1035 ><P
1036 >Given ASCII text input either from
1037 <TT
1038 CLASS="FILENAME"
1039 >stdin</TT
1040 > or a file, adjust
1041 the word spacing to right-justify each line to a
1042 user-specified line-width, then send the output to
1043 <TT
1044 CLASS="FILENAME"
1045 >stdout</TT
1046 >.</P
1047 ></DD
1048 ><DT
1049 ><B
1050 CLASS="COMMAND"
1051 >Mailing List</B
1052 ></DT
1053 ><DD
1054 ><P
1055 >Using the <A
1056 HREF="communications.html#COMMMAIL1"
1057 >mail</A
1058 > command,
1059 write a script that manages a simple mailing list. The
1060 script automatically e-mails the monthly company newsletter,
1061 read from a specified text file, and sends it to all the
1062 addresses on the mailing list, which the script reads from
1063 another specified file.</P
1064 ></DD
1065 ><DT
1066 ><B
1067 CLASS="COMMAND"
1068 >Generating Passwords</B
1069 ></DT
1070 ><DD
1071 ><P
1072 >Generate pseudorandom 8-character passwords, using
1073 characters in the ranges [0-9], [A-Z], [a-z]. Each password
1074 must contain at least two digits.</P
1075 ></DD
1076 ><DT
1077 ><B
1078 CLASS="COMMAND"
1079 >Monitoring a User</B
1080 ></DT
1081 ><DD
1082 ><P
1083 >You suspect that one particular user on the network
1084 has been abusing her privileges and possibly attempting to
1085 hack the system. Write a script to automatically monitor
1086 and log her activities when she's signed on. The log file
1087 will save entries for the previous week, and delete those
1088 entries more than seven days old.</P
1089 ><P
1090 >You may use <A
1091 HREF="system.html#LASTREF"
1092 >last</A
1093 >,
1094 <A
1095 HREF="system.html#LASTLOGREF"
1096 >lastlog</A
1097 >, and <A
1098 HREF="system.html#LASTCOMMREF"
1099 >lastcomm</A
1100 > to aid your
1101 surveillance of the suspected fiend.</P
1102 ></DD
1103 ><DT
1104 ><B
1105 CLASS="COMMAND"
1106 >Checking for Broken Links</B
1107 ></DT
1108 ><DD
1109 ><P
1110 >Using <A
1111 HREF="communications.html#LYNXREF"
1112 >lynx</A
1113 > with the
1114 <TT
1115 CLASS="OPTION"
1116 >-traversal</TT
1117 > option, write a script that
1118 checks a Web site for broken links.</P
1119 ></DD
1120 ></DL
1121 ></DIV
1122 ><DIV
1123 CLASS="VARIABLELIST"
1124 ><P
1125 ><B
1126 ><A
1127 NAME="EXDIFFICULT1"
1128 ></A
1129 >DIFFICULT</B
1130 ></P
1131 ><DL
1132 ><DT
1133 ><B
1134 CLASS="COMMAND"
1135 >Testing Passwords</B
1136 ></DT
1137 ><DD
1138 ><P
1139 >Write a script to check and validate passwords. The object
1140 is to flag <SPAN
1141 CLASS="QUOTE"
1142 >"weak"</SPAN
1143 > or easily guessed password
1144 candidates.</P
1145 ><P
1146 >A trial password will be input to the script as a
1147 command-line parameter. To be considered acceptable,
1148 a password must meet the following minimum qualifications:
1149
1150 <UL
1151 ><LI
1152 ><P
1153 >Minimum length of 8 characters</P
1154 ></LI
1155 ><LI
1156 ><P
1157 >Must contain at least one numeric character</P
1158 ></LI
1159 ><LI
1160 ><P
1161 >Must contain at least one of the following
1162 non-alphabetic characters: <SPAN
1163 CLASS="TOKEN"
1164 >@</SPAN
1165 >,
1166 <SPAN
1167 CLASS="TOKEN"
1168 >#</SPAN
1169 >, <SPAN
1170 CLASS="TOKEN"
1171 >$</SPAN
1172 >, <SPAN
1173 CLASS="TOKEN"
1174 >%</SPAN
1175 >,
1176 <SPAN
1177 CLASS="TOKEN"
1178 >&#38;</SPAN
1179 >, <SPAN
1180 CLASS="TOKEN"
1181 >*</SPAN
1182 >, <SPAN
1183 CLASS="TOKEN"
1184 >+</SPAN
1185 >,
1186 <SPAN
1187 CLASS="TOKEN"
1188 >-</SPAN
1189 >, <SPAN
1190 CLASS="TOKEN"
1191 >=</SPAN
1192 ></P
1193 ></LI
1194 ></UL
1195 ></P
1196 ><P
1197 >Optional:
1198
1199 <UL
1200 ><LI
1201 ><P
1202 >Do a dictionary check on every sequence of at least
1203 four consecutive alphabetic characters in the password under
1204 test. This will eliminate passwords containing embedded
1205 <SPAN
1206 CLASS="QUOTE"
1207 >"words"</SPAN
1208 > found in a standard dictionary.</P
1209 ></LI
1210 ><LI
1211 ><P
1212 >Enable the script to check all the passwords on your
1213 system. These do not reside in
1214 <A
1215 HREF="files.html#DATAFILESREF1"
1216 ><TT
1217 CLASS="FILENAME"
1218 >/etc/passwd</TT
1219 ></A
1220 >.</P
1221 ></LI
1222 ></UL
1223 ></P
1224 ><P
1225 >This exercise tests mastery of <A
1226 HREF="regexp.html#REGEXREF"
1227 >Regular Expressions</A
1228 >.</P
1229 ></DD
1230 ><DT
1231 ><B
1232 CLASS="COMMAND"
1233 >Cross Reference</B
1234 ></DT
1235 ><DD
1236 ><P
1237 >Write a script that generates a
1238 <I
1239 CLASS="FIRSTTERM"
1240 >cross-reference</I
1241 >
1242 (<I
1243 CLASS="FIRSTTERM"
1244 >concordance</I
1245 >) on a target file.
1246 The output will be a listing of all word occurrences in
1247 the target file, along with the line numbers in which
1248 each word occurs. Traditionally, <I
1249 CLASS="FIRSTTERM"
1250 >linked
1251 list</I
1252 > constructs would be used in such
1253 applications. Therefore, you should investigate <A
1254 HREF="arrays.html#ARRAYREF"
1255 >arrays</A
1256 > in the course of
1257 this exercise. <A
1258 HREF="textproc.html#WF"
1259 >Example 16-12</A
1260 > is probably
1261 <SPAN
1262 CLASS="emphasis"
1263 ><I
1264 CLASS="EMPHASIS"
1265 >not</I
1266 ></SPAN
1267 > a good place to start.</P
1268 ></DD
1269 ><DT
1270 ><A
1271 NAME="NEWTONSQRT"
1272 ></A
1273 ><B
1274 CLASS="COMMAND"
1275 >Square Root</B
1276 ></DT
1277 ><DD
1278 ><P
1279 >Write a script to calculate square roots of numbers
1280 using <I
1281 CLASS="FIRSTTERM"
1282 >Newton's Method</I
1283 >.</P
1284 ><P
1285 >The algorithm for this, expressed as a snippet of Bash
1286 <A
1287 HREF="assortedtips.html#PSEUDOCODEREF"
1288 >pseudo-code</A
1289 > is:</P
1290 ><P
1291 ><TABLE
1292 BORDER="0"
1293 BGCOLOR="#E0E0E0"
1294 WIDTH="90%"
1295 ><TR
1296 ><TD
1297 ><PRE
1298 CLASS="PROGRAMLISTING"
1299 > 1&nbsp;# (Isaac) Newton's Method for speedy extraction
1300 2&nbsp;#+ of square roots.
1301 3&nbsp;
1302 4&nbsp;guess = $argument
1303 5&nbsp;# $argument is the number to find the square root of.
1304 6&nbsp;# $guess is each successive calculated "guess" -- or trial solution --
1305 7&nbsp;#+ of the square root.
1306 8&nbsp;# Our first "guess" at a square root is the argument itself.
1307 9&nbsp;
1308 10&nbsp;oldguess = 0
1309 11&nbsp;# $oldguess is the previous $guess.
1310 12&nbsp;
1311 13&nbsp;tolerance = .000001
1312 14&nbsp;# To how close a tolerance we wish to calculate.
1313 15&nbsp;
1314 16&nbsp;loopcnt = 0
1315 17&nbsp;# Let's keep track of how many times through the loop.
1316 18&nbsp;# Some arguments will require more loop iterations than others.
1317 19&nbsp;
1318 20&nbsp;
1319 21&nbsp;while [ ABS( $guess $oldguess ) -gt $tolerance ]
1320 22&nbsp;# ^^^^^^^^^^^^^^^^^^^^^^^ Fix up syntax, of course.
1321 23&nbsp;
1322 24&nbsp;# "ABS" is a (floating point) function to find the absolute value
1323 25&nbsp;#+ of the difference between the two terms.
1324 26&nbsp;# So, as long as difference between current and previous
1325 27&nbsp;#+ trial solution (guess) exceeds the tolerance, keep looping.
1326 28&nbsp;
1327 29&nbsp;do
1328 30&nbsp; oldguess = $guess # Update $oldguess to previous $guess.
1329 31&nbsp;
1330 32&nbsp;# =======================================================
1331 33&nbsp; guess = ( $oldguess + ( $argument / $oldguess ) ) / 2.0
1332 34&nbsp;# = 1/2 ( ($oldguess **2 + $argument) / $oldguess )
1333 35&nbsp;# equivalent to:
1334 36&nbsp;# = 1/2 ( $oldguess + $argument / $oldguess )
1335 37&nbsp;# that is, "averaging out" the trial solution and
1336 38&nbsp;#+ the proportion of argument deviation
1337 39&nbsp;#+ (in effect, splitting the error in half).
1338 40&nbsp;# This converges on an accurate solution
1339 41&nbsp;#+ with surprisingly few loop iterations . . .
1340 42&nbsp;#+ for arguments &#62; $tolerance, of course.
1341 43&nbsp;# =======================================================
1342 44&nbsp;
1343 45&nbsp; (( loopcnt++ )) # Update loop counter.
1344 46&nbsp;done</PRE
1345 ></TD
1346 ></TR
1347 ></TABLE
1348 ></P
1349 ><P
1350 >It's a simple enough recipe, and
1351 <SPAN
1352 CLASS="emphasis"
1353 ><I
1354 CLASS="EMPHASIS"
1355 >seems</I
1356 ></SPAN
1357 > at first glance easy enough to
1358 convert into a working Bash script. The problem, though,
1359 is that Bash has <A
1360 HREF="operations.html#NOFLOATINGPOINT"
1361 >no native
1362 support for floating point numbers</A
1363 >. So, the script
1364 writer needs to use <A
1365 HREF="mathc.html#BCREF"
1366 >bc</A
1367 > or
1368 possibly <A
1369 HREF="awk.html#AWKREF"
1370 >awk</A
1371 > to convert the
1372 numbers and do the calculations. It could get rather messy
1373 . . .</P
1374 ></DD
1375 ><DT
1376 ><B
1377 CLASS="COMMAND"
1378 >Logging File Accesses</B
1379 ></DT
1380 ><DD
1381 ><P
1382 >Log all accesses to the files in <TT
1383 CLASS="FILENAME"
1384 >/etc</TT
1385 > during the course of
1386 a single day. This information should include the filename,
1387 user name, and access time. If any alterations to the
1388 files take place, that will be flagged. Write this data
1389 as tabular (tab-separated) formatted records in a logfile.</P
1390 ></DD
1391 ><DT
1392 ><B
1393 CLASS="COMMAND"
1394 >Monitoring Processes</B
1395 ></DT
1396 ><DD
1397 ><P
1398 >Write a script to continually monitor all running
1399 processes and to keep track of how many child processes each
1400 parent spawns. If a process spawns more than five children,
1401 then the script sends an e-mail to the system administrator
1402 (or <I
1403 CLASS="FIRSTTERM"
1404 >root</I
1405 >) with all relevant
1406 information, including the time, PID of the parent, PIDs
1407 of the children, etc. The script appends a report to a log
1408 file every ten minutes. </P
1409 ></DD
1410 ><DT
1411 ><B
1412 CLASS="COMMAND"
1413 >Strip Comments</B
1414 ></DT
1415 ><DD
1416 ><P
1417 >Strip all comments from a shell script whose name
1418 is specified on the command-line. Note that the initial
1419 <A
1420 HREF="sha-bang.html#SHABANGREF"
1421 >#! line</A
1422 > must not be
1423 stripped out.</P
1424 ></DD
1425 ><DT
1426 ><B
1427 CLASS="COMMAND"
1428 >Strip HTML Tags</B
1429 ></DT
1430 ><DD
1431 ><P
1432 >Strip all the HTML tags from a specified HTML file, then
1433 reformat it into lines between 60 and 75 characters
1434 in length. Reset paragraph and block spacing, as
1435 appropriate, and convert HTML tables to their approximate
1436 text equivalent.</P
1437 ></DD
1438 ><DT
1439 ><B
1440 CLASS="COMMAND"
1441 >XML Conversion</B
1442 ></DT
1443 ><DD
1444 ><P
1445 >Convert an XML file to both HTML and text format.</P
1446 ><P
1447 >Optional: A script that converts Docbook/SGML to XML.</P
1448 ></DD
1449 ><DT
1450 ><A
1451 NAME="CSPAMMERS"
1452 ></A
1453 ><B
1454 CLASS="COMMAND"
1455 >Chasing Spammers</B
1456 ></DT
1457 ><DD
1458 ><P
1459 > Write a script that analyzes a spam e-mail by doing
1460 DNS lookups on the IP addresses in the headers to identify
1461 the relay hosts as well as the originating ISP. The
1462 script will forward the unaltered spam message to the
1463 responsible ISPs. Of course, it will be necessary to
1464 filter out <SPAN
1465 CLASS="emphasis"
1466 ><I
1467 CLASS="EMPHASIS"
1468 >your own ISP's IP address</I
1469 ></SPAN
1470 >,
1471 so you don't end up complaining about yourself.</P
1472 ><P
1473 >As necessary, use the appropriate <A
1474 HREF="communications.html#COMMUNINFO1"
1475 >network analysis commands</A
1476 >.</P
1477 ><P
1478 >For some ideas, see <A
1479 HREF="communications.html#ISSPAMMER"
1480 >Example 16-41</A
1481 > and <A
1482 HREF="contributed-scripts.html#ISSPAMMER2"
1483 >Example A-28</A
1484 >.</P
1485 ><P
1486 >Optional: Write a script that searches through a list of
1487 e-mail messages and deletes the spam according to specified
1488 filters.</P
1489 ></DD
1490 ><DT
1491 ><B
1492 CLASS="COMMAND"
1493 >Creating man pages</B
1494 ></DT
1495 ><DD
1496 ><P
1497 >Write a script that automates the process of creating
1498 <A
1499 HREF="external.html#MANREF"
1500 >man pages</A
1501 >.</P
1502 ><P
1503 >Given a text file which contains information to be
1504 formatted into a <I
1505 CLASS="FIRSTTERM"
1506 >man page</I
1507 >, the
1508 script will read the file, then invoke the appropriate
1509 <A
1510 HREF="textproc.html#GROFFREF"
1511 >groff</A
1512 > commands to
1513 output the corresponding <I
1514 CLASS="FIRSTTERM"
1515 >man page</I
1516 >
1517 to <TT
1518 CLASS="FILENAME"
1519 >stdout</TT
1520 >. The text file contains
1521 blocks of information under the standard <I
1522 CLASS="FIRSTTERM"
1523 >man
1524 page</I
1525 > headings, i.e., NAME, SYNOPSIS,
1526 DESCRIPTION, etc.</P
1527 ><P
1528 ><A
1529 HREF="contributed-scripts.html#MANED"
1530 >Example A-39</A
1531 > is an instructive first step.</P
1532 ></DD
1533 ><DT
1534 ><B
1535 CLASS="COMMAND"
1536 >Hex Dump</B
1537 ></DT
1538 ><DD
1539 ><P
1540 >Do a hex(adecimal) dump on a binary file
1541 specified as an argument to the script. The output should
1542 be in neat tabular <A
1543 HREF="special-chars.html#FIELDREF"
1544 >fields</A
1545 >,
1546 with the first field showing the address, each of the
1547 next 8 fields a 4-byte hex number, and the final field
1548 the ASCII equivalent of the previous 8 fields.</P
1549 ><P
1550 >The obvious followup to this is to extend the hex
1551 dump script into a disassembler. Using a lookup table,
1552 or some other clever gimmick, convert the hex values into
1553 80x86 op codes.</P
1554 ></DD
1555 ><DT
1556 ><B
1557 CLASS="COMMAND"
1558 >Emulating a Shift Register</B
1559 ></DT
1560 ><DD
1561 ><P
1562 >Using <A
1563 HREF="arrays.html#STACKEX"
1564 >Example 27-15</A
1565 > as an inspiration,
1566 write a script that emulates a 64-bit shift register as
1567 an <A
1568 HREF="arrays.html#ARRAYREF"
1569 >array</A
1570 >. Implement
1571 functions to <I
1572 CLASS="FIRSTTERM"
1573 >load</I
1574 > the register,
1575 <I
1576 CLASS="FIRSTTERM"
1577 >shift left</I
1578 >, <I
1579 CLASS="FIRSTTERM"
1580 >shift
1581 right</I
1582 >, and <I
1583 CLASS="FIRSTTERM"
1584 >rotate</I
1585 >
1586 it. Finally, write a function that interprets the register
1587 contents as eight 8-bit ASCII characters.</P
1588 ></DD
1589 ><DT
1590 ><B
1591 CLASS="COMMAND"
1592 >Calculating Determinants</B
1593 ></DT
1594 ><DD
1595 ><P
1596 >Write a script that calculates
1597 determinants
1598 <A
1599 NAME="AEN25254"
1600 HREF="#FTN.AEN25254"
1601 >[1]</A
1602 >
1603
1604 by <A
1605 HREF="localvar.html#RECURSIONREF0"
1606 >recursively</A
1607 > expanding the
1608 <I
1609 CLASS="FIRSTTERM"
1610 >minors</I
1611 >. Use a 4 x 4 determinant as
1612 a test case.</P
1613 ></DD
1614 ><DT
1615 ><B
1616 CLASS="COMMAND"
1617 >Hidden Words</B
1618 ></DT
1619 ><DD
1620 ><P
1621 >Write a <SPAN
1622 CLASS="QUOTE"
1623 >"word-find"</SPAN
1624 > puzzle generator,
1625 a script that hides 10 input words in a 10 x 10 array
1626 of random letters. The words may be hidden across, down,
1627 or diagonally.</P
1628 ><P
1629 >Optional: Write a script that <SPAN
1630 CLASS="emphasis"
1631 ><I
1632 CLASS="EMPHASIS"
1633 >solves</I
1634 ></SPAN
1635 >
1636 word-find puzzles. To keep this from becoming too difficult,
1637 the solution script will find only horizontal and vertical
1638 words. (Hint: Treat each row and column as a string, and
1639 search for substrings.)</P
1640 ></DD
1641 ><DT
1642 ><B
1643 CLASS="COMMAND"
1644 >Anagramming</B
1645 ></DT
1646 ><DD
1647 ><P
1648 > Anagram 4-letter input. For example, the
1649 anagrams of <SPAN
1650 CLASS="emphasis"
1651 ><I
1652 CLASS="EMPHASIS"
1653 >word</I
1654 ></SPAN
1655 > are:
1656 <SPAN
1657 CLASS="emphasis"
1658 ><I
1659 CLASS="EMPHASIS"
1660 >do or rod row word</I
1661 ></SPAN
1662 >. You may use
1663 <TT
1664 CLASS="FILENAME"
1665 >/usr/share/dict/linux.words</TT
1666 > as the
1667 reference list.</P
1668 ></DD
1669 ><DT
1670 ><B
1671 CLASS="COMMAND"
1672 >Word Ladders</B
1673 ></DT
1674 ><DD
1675 ><P
1676 >A <SPAN
1677 CLASS="QUOTE"
1678 >"word ladder"</SPAN
1679 > is a sequence of words,
1680 with each successive word in the sequence differing from
1681 the previous one by a single letter.</P
1682 ><P
1683 >For example, to <SPAN
1684 CLASS="QUOTE"
1685 >"ladder"</SPAN
1686 > from
1687 <SPAN
1688 CLASS="emphasis"
1689 ><I
1690 CLASS="EMPHASIS"
1691 >mark</I
1692 ></SPAN
1693 > to
1694 <SPAN
1695 CLASS="emphasis"
1696 ><I
1697 CLASS="EMPHASIS"
1698 >vase</I
1699 ></SPAN
1700 >:</P
1701 ><P
1702 > <TABLE
1703 BORDER="0"
1704 BGCOLOR="#E0E0E0"
1705 WIDTH="90%"
1706 ><TR
1707 ><TD
1708 ><PRE
1709 CLASS="PROGRAMLISTING"
1710 > 1&nbsp;mark --&#62; park --&#62; part --&#62; past --&#62; vast --&#62; vase
1711 2&nbsp; ^ ^ ^ ^ ^</PRE
1712 ></TD
1713 ></TR
1714 ></TABLE
1715 >
1716 </P
1717 ><P
1718 >Write a script that solves word ladder puzzles. Given
1719 a starting and an ending word, the script will list all
1720 intermediate steps in the <SPAN
1721 CLASS="QUOTE"
1722 >"ladder."</SPAN
1723 > Note
1724 that <SPAN
1725 CLASS="emphasis"
1726 ><I
1727 CLASS="EMPHASIS"
1728 >all</I
1729 ></SPAN
1730 > words in the sequence must
1731 be legitimate dictionary words.</P
1732 ></DD
1733 ><DT
1734 ><B
1735 CLASS="COMMAND"
1736 >Fog Index</B
1737 ></DT
1738 ><DD
1739 ><P
1740 >The <SPAN
1741 CLASS="QUOTE"
1742 >"fog index"</SPAN
1743 > of a passage of text
1744 estimates its reading difficulty, as a number corresponding
1745 roughly to a school grade level. For example, a passage
1746 with a fog index of 12 should be comprehensible to anyone
1747 with 12 years of schooling.</P
1748 ><P
1749 >The Gunning version of the fog index uses the following
1750 algorithm.</P
1751 ><OL
1752 TYPE="1"
1753 ><LI
1754 ><P
1755 >Choose a section of the text at least
1756 100 words in length.</P
1757 ></LI
1758 ><LI
1759 ><P
1760 >Count the number of sentences (a portion of
1761 a sentence truncated by the boundary of the text section
1762 counts as one).</P
1763 ></LI
1764 ><LI
1765 ><P
1766 >Find the average number of words per
1767 sentence.</P
1768 ><P
1769 >AVE_WDS_SEN = TOTAL_WORDS / SENTENCES</P
1770 ></LI
1771 ><LI
1772 ><P
1773 >Count the number of <SPAN
1774 CLASS="QUOTE"
1775 >"difficult"</SPAN
1776 >
1777 words in the segment -- those containing at least
1778 3 syllables. Divide this quantity by total words to
1779 get the proportion of difficult words.</P
1780 ><P
1781 >PRO_DIFF_WORDS = LONG_WORDS / TOTAL_WORDS</P
1782 ></LI
1783 ><LI
1784 ><P
1785 >The Gunning fog index is the sum of the above two
1786 quantities, multiplied by 0.4, then rounded to the
1787 nearest integer.</P
1788 ><P
1789 >G_FOG_INDEX = int ( 0.4 * ( AVE_WDS_SEN + PRO_DIFF_WORDS ) )</P
1790 ></LI
1791 ></OL
1792 ><P
1793 >Step 4 is by far the most difficult portion of the
1794 exercise. There exist various algorithms for estimating
1795 the syllable count of a word. A rule-of-thumb formula
1796 might consider the number of letters in a word and the
1797 vowel-consonant mix.</P
1798 ><P
1799 >A strict interpretation of the Gunning fog index does
1800 not count compound words and proper nouns as
1801 <SPAN
1802 CLASS="QUOTE"
1803 >"difficult"</SPAN
1804 > words, but this would enormously
1805 complicate the script.</P
1806 ></DD
1807 ><DT
1808 ><B
1809 CLASS="COMMAND"
1810 >Calculating PI using Buffon's Needle</B
1811 ></DT
1812 ><DD
1813 ><P
1814 >The Eighteenth Century French mathematician de Buffon
1815 came up with a novel experiment. Repeatedly drop a needle
1816 of length <TT
1817 CLASS="REPLACEABLE"
1818 ><I
1819 >n</I
1820 ></TT
1821 > onto a wooden floor
1822 composed of long and narrow parallel boards. The cracks
1823 separating the equal-width floorboards are a fixed distance
1824 <TT
1825 CLASS="REPLACEABLE"
1826 ><I
1827 >d</I
1828 ></TT
1829 > apart. Keep track of the
1830 total drops and the number of times the needle intersects
1831 a crack on the floor. The ratio of these two quantities
1832 turns out to be a fractional multiple of PI.</P
1833 ><P
1834 >In the spirit of <A
1835 HREF="mathc.html#CANNON"
1836 >Example 16-50</A
1837 >, write a
1838 script that runs a Monte Carlo simulation of
1839 <I
1840 CLASS="FIRSTTERM"
1841 >Buffon's Needle</I
1842 >. To simplify matters,
1843 set the needle length equal to the distance between the
1844 cracks, <TT
1845 CLASS="PARAMETER"
1846 ><I
1847 >n = d</I
1848 ></TT
1849 >.</P
1850 ><P
1851 >Hint: there are actually two critical variables:
1852 the distance from the center of the needle to the nearest
1853 crack, and the inclination angle of the needle to that crack.
1854 You may use <A
1855 HREF="mathc.html#BCREF"
1856 >bc</A
1857 > to handle
1858 the calculations.</P
1859 ></DD
1860 ><DT
1861 ><B
1862 CLASS="COMMAND"
1863 >Playfair Cipher</B
1864 ></DT
1865 ><DD
1866 ><P
1867 >Implement the Playfair (Wheatstone) Cipher in a
1868 script.</P
1869 ><P
1870 >The Playfair Cipher encrypts text by substitution
1871 of <I
1872 CLASS="FIRSTTERM"
1873 >digrams</I
1874 > (2-letter groupings).
1875 It is traditional to use a 5 x 5 letter scrambled-alphabet
1876 <I
1877 CLASS="FIRSTTERM"
1878 >key square</I
1879 > for the encryption and
1880 decryption.</P
1881 ><P
1882 > <TABLE
1883 BORDER="0"
1884 BGCOLOR="#E0E0E0"
1885 WIDTH="90%"
1886 ><TR
1887 ><TD
1888 ><PRE
1889 CLASS="PROGRAMLISTING"
1890 > 1&nbsp; C O D E S
1891 2&nbsp; A B F G H
1892 3&nbsp; I K L M N
1893 4&nbsp; P Q R T U
1894 5&nbsp; V W X Y Z
1895 6&nbsp;
1896 7&nbsp;Each letter of the alphabet appears once, except "I" also represents
1897 8&nbsp;"J". The arbitrarily chosen key word, "CODES" comes first, then all
1898 9&nbsp;the rest of the alphabet, in order from left to right, skipping letters
1899 10&nbsp;already used.
1900 11&nbsp;
1901 12&nbsp;To encrypt, separate the plaintext message into digrams (2-letter
1902 13&nbsp;groups). If a group has two identical letters, delete the second, and
1903 14&nbsp;form a new group. If there is a single letter left over at the end,
1904 15&nbsp;insert a "null" character, typically an "X."
1905 16&nbsp;
1906 17&nbsp;THIS IS A TOP SECRET MESSAGE
1907 18&nbsp;
1908 19&nbsp;TH IS IS AT OP SE CR ET ME SA GE
1909 20&nbsp;
1910 21&nbsp;
1911 22&nbsp;
1912 23&nbsp;For each digram, there are three possibilities.
1913 24&nbsp;-----------------------------------------------
1914 25&nbsp;
1915 26&nbsp;1) Both letters will be on the same row of the key square:
1916 27&nbsp; For each letter, substitute the one immediately to the right, in that
1917 28&nbsp; row. If necessary, wrap around left to the beginning of the row.
1918 29&nbsp;
1919 30&nbsp;or
1920 31&nbsp;
1921 32&nbsp;2) Both letters will be in the same column of the key square:
1922 33&nbsp; For each letter, substitute the one immediately below it, in that
1923 34&nbsp; row. If necessary, wrap around to the top of the column.
1924 35&nbsp;
1925 36&nbsp;or
1926 37&nbsp;
1927 38&nbsp;3) Both letters will form the corners of a rectangle within the key square:
1928 39&nbsp; For each letter, substitute the one on the other corner the rectangle
1929 40&nbsp; which lies on the same row.
1930 41&nbsp;
1931 42&nbsp;
1932 43&nbsp;The "TH" digram falls under case #3.
1933 44&nbsp;G H
1934 45&nbsp;M N
1935 46&nbsp;T U (Rectangle with "T" and "H" at corners)
1936 47&nbsp;
1937 48&nbsp;T --&#62; U
1938 49&nbsp;H --&#62; G
1939 50&nbsp;
1940 51&nbsp;
1941 52&nbsp;The "SE" digram falls under case #1.
1942 53&nbsp;C O D E S (Row containing "S" and "E")
1943 54&nbsp;
1944 55&nbsp;S --&#62; C (wraps around left to beginning of row)
1945 56&nbsp;E --&#62; S
1946 57&nbsp;
1947 58&nbsp;=========================================================================
1948 59&nbsp;
1949 60&nbsp;To decrypt encrypted text, reverse the above procedure under cases #1
1950 61&nbsp;and #2 (move in opposite direction for substitution). Under case #3,
1951 62&nbsp;just take the remaining two corners of the rectangle.
1952 63&nbsp;
1953 64&nbsp;
1954 65&nbsp;Helen Fouche Gaines' classic work, ELEMENTARY CRYPTANALYSIS (1939), gives a
1955 66&nbsp;fairly detailed description of the Playfair Cipher and its solution methods.</PRE
1956 ></TD
1957 ></TR
1958 ></TABLE
1959 >
1960 </P
1961 ><P
1962 >This script will have three main sections</P
1963 ><OL
1964 TYPE="I"
1965 ><LI
1966 ><P
1967 >Generating the <I
1968 CLASS="FIRSTTERM"
1969 >key square</I
1970 >,
1971 based on a user-input keyword.</P
1972 ></LI
1973 ><LI
1974 ><P
1975 >Encrypting a <I
1976 CLASS="FIRSTTERM"
1977 >plaintext</I
1978 >
1979 message.</P
1980 ></LI
1981 ><LI
1982 ><P
1983 >Decrypting encrypted
1984 text.</P
1985 ></LI
1986 ></OL
1987 ><P
1988 >The script will make extensive use of <A
1989 HREF="arrays.html#ARRAYREF"
1990 >arrays</A
1991 > and <A
1992 HREF="functions.html#FUNCTIONREF"
1993 >functions</A
1994 >.
1995 You may use <A
1996 HREF="contributed-scripts.html#GRONSFELD"
1997 >Example A-56</A
1998 > as an
1999 inspiration.</P
2000 ></DD
2001 ></DL
2002 ></DIV
2003 ><P
2004 >--</P
2005 ><P
2006 >Please do not send the author your solutions to these
2007 exercises. There are more appropriate ways to impress him with
2008 your cleverness, such as submitting bugfixes and suggestions
2009 for improving the book.</P
2010 ></DIV
2011 ><H3
2012 CLASS="FOOTNOTES"
2013 >Notes</H3
2014 ><TABLE
2015 BORDER="0"
2016 CLASS="FOOTNOTES"
2017 WIDTH="100%"
2018 ><TR
2019 ><TD
2020 ALIGN="LEFT"
2021 VALIGN="TOP"
2022 WIDTH="5%"
2023 ><A
2024 NAME="FTN.AEN25254"
2025 HREF="writingscripts.html#AEN25254"
2026 >[1]</A
2027 ></TD
2028 ><TD
2029 ALIGN="LEFT"
2030 VALIGN="TOP"
2031 WIDTH="95%"
2032 ><P
2033 >For all you clever types who failed intermediate algebra,
2034 a <I
2035 CLASS="FIRSTTERM"
2036 >determinant</I
2037 > is a numerical value
2038 associated with a multidimensional
2039 <I
2040 CLASS="FIRSTTERM"
2041 >matrix</I
2042 > (<A
2043 HREF="arrays.html#ARRAYREF"
2044 >array</A
2045 > of numbers).
2046 <TABLE
2047 BORDER="0"
2048 BGCOLOR="#E0E0E0"
2049 WIDTH="90%"
2050 ><TR
2051 ><TD
2052 ><PRE
2053 CLASS="PROGRAMLISTING"
2054 > 1&nbsp;For the simple case of a 2 x 2 determinant:
2055 2&nbsp;
2056 3&nbsp; |a b|
2057 4&nbsp; |b a|
2058 5&nbsp;
2059 6&nbsp;The solution is a*a - b*b, where "a" and "b" represent numbers.</PRE
2060 ></TD
2061 ></TR
2062 ></TABLE
2063 >
2064 </P
2065 ></TD
2066 ></TR
2067 ></TABLE
2068 ><DIV
2069 CLASS="NAVFOOTER"
2070 ><HR
2071 ALIGN="LEFT"
2072 WIDTH="100%"><TABLE
2073 SUMMARY="Footer navigation table"
2074 WIDTH="100%"
2075 BORDER="0"
2076 CELLPADDING="0"
2077 CELLSPACING="0"
2078 ><TR
2079 ><TD
2080 WIDTH="33%"
2081 ALIGN="left"
2082 VALIGN="top"
2083 ><A
2084 HREF="exercises.html"
2085 ACCESSKEY="P"
2086 >Prev</A
2087 ></TD
2088 ><TD
2089 WIDTH="34%"
2090 ALIGN="center"
2091 VALIGN="top"
2092 ><A
2093 HREF="index.html"
2094 ACCESSKEY="H"
2095 >Home</A
2096 ></TD
2097 ><TD
2098 WIDTH="33%"
2099 ALIGN="right"
2100 VALIGN="top"
2101 ><A
2102 HREF="revisionhistory.html"
2103 ACCESSKEY="N"
2104 >Next</A
2105 ></TD
2106 ></TR
2107 ><TR
2108 ><TD
2109 WIDTH="33%"
2110 ALIGN="left"
2111 VALIGN="top"
2112 >Exercises</TD
2113 ><TD
2114 WIDTH="34%"
2115 ALIGN="center"
2116 VALIGN="top"
2117 ><A
2118 HREF="exercises.html"
2119 ACCESSKEY="U"
2120 >Up</A
2121 ></TD
2122 ><TD
2123 WIDTH="33%"
2124 ALIGN="right"
2125 VALIGN="top"
2126 >Revision History</TD
2127 ></TR
2128 ></TABLE
2129 ></DIV
2130 ></BODY
2131 ></HTML
2132 >