Debian Codegraph @

HTML/exercises.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Exercises</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="PREVIOUS"
14 TITLE="Converting DOS Batch Files to Shell Scripts"
15 HREF="dosbatch.html"><LINK
16 REL="NEXT"
17 TITLE="Writing Scripts"
18 HREF="writingscripts.html"><META
19 HTTP-EQUIV="Content-Style-Type"
20 CONTENT="text/css"><LINK
21 REL="stylesheet"
22 HREF="common/kde-common.css"
23 TYPE="text/css"><META
24 HTTP-EQUIV="Content-Type"
25 CONTENT="text/html; charset=iso-8859-1"><META
26 HTTP-EQUIV="Content-Language"
27 CONTENT="en"><LINK
28 REL="stylesheet"
29 HREF="common/kde-localised.css"
30 TYPE="text/css"
31 TITLE="KDE-English"><LINK
32 REL="stylesheet"
33 HREF="common/kde-default.css"
34 TYPE="text/css"
35 TITLE="KDE-Default"></HEAD
36 ><BODY
37 CLASS="APPENDIX"
38 BGCOLOR="#FFFFFF"
39 TEXT="#000000"
40 LINK="#AA0000"
41 VLINK="#AA0055"
42 ALINK="#AA0000"
43 STYLE="font-family: sans-serif;"
44 ><DIV
45 CLASS="NAVHEADER"
46 ><TABLE
47 SUMMARY="Header navigation table"
48 WIDTH="100%"
49 BORDER="0"
50 CELLPADDING="0"
51 CELLSPACING="0"
52 ><TR
53 ><TH
54 COLSPAN="3"
55 ALIGN="center"
56 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
57 ></TR
58 ><TR
59 ><TD
60 WIDTH="10%"
61 ALIGN="left"
62 VALIGN="bottom"
63 ><A
64 HREF="dosbatch.html"
65 ACCESSKEY="P"
66 >Prev</A
67 ></TD
68 ><TD
69 WIDTH="80%"
70 ALIGN="center"
71 VALIGN="bottom"
72 ></TD
73 ><TD
74 WIDTH="10%"
75 ALIGN="right"
76 VALIGN="bottom"
77 ><A
78 HREF="writingscripts.html"
79 ACCESSKEY="N"
80 >Next</A
81 ></TD
82 ></TR
83 ></TABLE
84 ><HR
85 ALIGN="LEFT"
86 WIDTH="100%"></DIV
87 ><DIV
88 CLASS="APPENDIX"
89 ><H1
90 ><A
91 NAME="EXERCISES"
92 ></A
93 >Appendix O. Exercises</H1
94 ><P
95 >The exercises that follow test and extend your knowledge
96 of scripting. Think of them as a challenge, as an entertaining way
97 to take you further along the stony path toward UNIX wizardry.</P
98 ><P
99 > <P
100 CLASS="LITERALLAYOUT"
101 >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;On&nbsp;a&nbsp;dingy&nbsp;side&nbsp;street&nbsp;in&nbsp;a&nbsp;run-down&nbsp;section&nbsp;of&nbsp;Hoboken,&nbsp;New&nbsp;Jersey,<br>
102 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;there&nbsp;sits&nbsp;a&nbsp;nondescript&nbsp;squat&nbsp;two-story&nbsp;brick&nbsp;building&nbsp;with&nbsp;an&nbsp;inscription<br>
103 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;incised&nbsp;on&nbsp;a&nbsp;marble&nbsp;plate&nbsp;in&nbsp;its&nbsp;wall:<br>
104 <br>
105 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TT
106 CLASS="COMPUTEROUTPUT"
107 >Bash Scripting Hall of Fame</TT
108 >.<br>
109 <br>
110 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inside,&nbsp;among&nbsp;various&nbsp;dusty&nbsp;uninteresting&nbsp;exhibits&nbsp;is&nbsp;a&nbsp;corroding,<br>
111 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cobweb-festooned&nbsp;brass&nbsp;plaque&nbsp;inscribed&nbsp;with&nbsp;a&nbsp;short,&nbsp;very&nbsp;short<br>
112 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list&nbsp;of&nbsp;those&nbsp;few&nbsp;persons&nbsp;who&nbsp;have&nbsp;successfully&nbsp;mastered&nbsp;the&nbsp;material<br>
113 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in&nbsp;the&nbsp;<I
114 CLASS="FIRSTTERM"
115 >Advanced Bash Scripting Guide</I
116 >,&nbsp;as&nbsp;evidenced&nbsp;by&nbsp;their&nbsp;performance<br>
117 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;on&nbsp;the&nbsp;following&nbsp;Exercise&nbsp;sections.<br>
118 <br>
119 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Alas,&nbsp;the&nbsp;author&nbsp;of&nbsp;the&nbsp;<I
120 CLASS="FIRSTTERM"
121 >ABS Guide</I
122 >&nbsp;is&nbsp;not&nbsp;represented&nbsp;among&nbsp;the&nbsp;exhibits.<br>
123 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;is&nbsp;possibly&nbsp;due&nbsp;to&nbsp;malicious&nbsp;rumors&nbsp;about&nbsp;<A
124 HREF="aboutauthor.html#NOCREDS"
125 >lack of credentials</A
126 >&nbsp;and<br>
127 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A
128 HREF="contributed-scripts.html#KTOUR0"
129 >deficient scripting skills</A
130 >.)<br>
131 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P
132 >
133 </P
134 ><DIV
135 CLASS="SECT1"
136 ><H1
137 CLASS="SECT1"
138 ><A
139 NAME="SCRIPTANALYSIS"
140 ></A
141 >O.1. Analyzing Scripts</H1
142 ><P
143 >Examine the following script. Run it, then explain what it
144 does. Annotate the script and rewrite it in a more compact and
145 elegant manner.</P
146 ><P
147 > <TABLE
148 BORDER="0"
149 BGCOLOR="#E0E0E0"
150 WIDTH="100%"
151 ><TR
152 ><TD
153 ><PRE
154 CLASS="PROGRAMLISTING"
155 > 1&nbsp;#!/bin/bash
156 2&nbsp;
157 3&nbsp;MAX=10000
158 4&nbsp;
159 5&nbsp;
160 6&nbsp; for((nr=1; nr&#60;$MAX; nr++))
161 7&nbsp; do
162 8&nbsp;
163 9&nbsp; let "t1 = nr % 5"
164 10&nbsp; if [ "$t1" -ne 3 ]
165 11&nbsp; then
166 12&nbsp; continue
167 13&nbsp; fi
168 14&nbsp;
169 15&nbsp; let "t2 = nr % 7"
170 16&nbsp; if [ "$t2" -ne 4 ]
171 17&nbsp; then
172 18&nbsp; continue
173 19&nbsp; fi
174 20&nbsp;
175 21&nbsp; let "t3 = nr % 9"
176 22&nbsp; if [ "$t3" -ne 5 ]
177 23&nbsp; then
178 24&nbsp; continue
179 25&nbsp; fi
180 26&nbsp;
181 27&nbsp; break # What happens when you comment out this line? Why?
182 28&nbsp;
183 29&nbsp; done
184 30&nbsp;
185 31&nbsp; echo "Number = $nr"
186 32&nbsp;
187 33&nbsp;
188 34&nbsp;exit 0</PRE
189 ></TD
190 ></TR
191 ></TABLE
192 >
193 </P
194 ><P
195 >---</P
196 ><P
197 >Explain what the following script does. It is really just
198 a parameterized command-line pipe.</P
199 ><P
200 > <TABLE
201 BORDER="0"
202 BGCOLOR="#E0E0E0"
203 WIDTH="100%"
204 ><TR
205 ><TD
206 ><PRE
207 CLASS="PROGRAMLISTING"
208 > 1&nbsp;#!/bin/bash
209 2&nbsp;
210 3&nbsp;DIRNAME=/usr/bin
211 4&nbsp;FILETYPE="shell script"
212 5&nbsp;LOGFILE=logfile
213 6&nbsp;
214 7&nbsp;file "$DIRNAME"/* | fgrep "$FILETYPE" | tee $LOGFILE | wc -l
215 8&nbsp;
216 9&nbsp;exit 0</PRE
217 ></TD
218 ></TR
219 ></TABLE
220 >
221 </P
222 ><P
223 >---</P
224 ><P
225 >Examine and explain the following script. For hints, you
226 might refer to the listings for <A
227 HREF="moreadv.html#FINDREF"
228 >find</A
229 > and <A
230 HREF="system.html#STATREF"
231 >stat</A
232 >.</P
233 ><P
234 > <TABLE
235 BORDER="0"
236 BGCOLOR="#E0E0E0"
237 WIDTH="100%"
238 ><TR
239 ><TD
240 ><PRE
241 CLASS="PROGRAMLISTING"
242 > 1&nbsp;#!/bin/bash
243 2&nbsp;
244 3&nbsp;# Author: Nathan Coulter
245 4&nbsp;# This code is released to the public domain.
246 5&nbsp;# The author gave permission to use this code snippet in the ABS Guide.
247 6&nbsp;
248 7&nbsp;find -maxdepth 1 -type f -printf '%f\000' | {
249 8&nbsp; while read -d $'\000'; do
250 9&nbsp; mv "$REPLY" "$(date -d "$(stat -c '%y' "$REPLY") " '+%Y%m%d%H%M%S'
251 10&nbsp; )-$REPLY"
252 11&nbsp; done
253 12&nbsp;}
254 13&nbsp;
255 14&nbsp;# Warning: Test-drive this script in a "scratch" directory.
256 15&nbsp;# It will somehow affect all the files there.</PRE
257 ></TD
258 ></TR
259 ></TABLE
260 >
261 </P
262 ><P
263 >---</P
264 ><P
265 >A reader sent in the following code snippet.</P
266 ><P
267 > <TABLE
268 BORDER="0"
269 BGCOLOR="#E0E0E0"
270 WIDTH="100%"
271 ><TR
272 ><TD
273 ><PRE
274 CLASS="PROGRAMLISTING"
275 > 1&nbsp;while read LINE
276 2&nbsp;do
277 3&nbsp; echo $LINE
278 4&nbsp;done &#60; `tail -f /var/log/messages`</PRE
279 ></TD
280 ></TR
281 ></TABLE
282 >
283 </P
284 ><P
285 >He wished to write a script tracking changes to the system log
286 file, <TT
287 CLASS="FILENAME"
288 >/var/log/messages</TT
289 >. Unfortunately,
290 the above code block hangs and does nothing
291 useful. Why? Fix this so it does work. (Hint:
292 rather than <A
293 HREF="redircb.html#REDIRREF"
294 >redirecting the
295 <TT
296 CLASS="FILENAME"
297 >stdin</TT
298 > of the loop</A
299 >, try a <A
300 HREF="special-chars.html#PIPEREF"
301 >pipe</A
302 >.)</P
303 ><P
304 >---</P
305 ><P
306 >Analyze the following <SPAN
307 CLASS="QUOTE"
308 >"one-liner"</SPAN
309 > (here
310 split into two lines for clarity) contributed by Rory
311 Winston:</P
312 ><P
313 > <TABLE
314 BORDER="0"
315 BGCOLOR="#E0E0E0"
316 WIDTH="100%"
317 ><TR
318 ><TD
319 ><PRE
320 CLASS="PROGRAMLISTING"
321 > 1&nbsp;export SUM=0; for f in $(find src -name "*.java");
322 2&nbsp;do export SUM=$(($SUM + $(wc -l $f | awk '{ print $1 }'))); done; echo $SUM</PRE
323 ></TD
324 ></TR
325 ></TABLE
326 >
327 </P
328 ><P
329 >Hint: First, break the script up into bite-sized
330 sections. Then, carefully examine its use of <A
331 HREF="dblparens.html"
332 >double-parentheses</A
333 > arithmetic,
334 the <A
335 HREF="internal.html#EXPORTREF"
336 >export</A
337 > command,
338 the <A
339 HREF="moreadv.html#FINDREF"
340 >find</A
341 > command, the
342 <A
343 HREF="textproc.html#WCREF"
344 >wc</A
345 > command, and <A
346 HREF="awk.html#AWKREF"
347 >awk</A
348 >.</P
349 ><P
350 >---</P
351 ><P
352 >Analyze <A
353 HREF="contributed-scripts.html#LIFESLOW"
354 >Example A-10</A
355 >, and reorganize it in a
356 simplified and more logical style. See how many of the variables
357 can be eliminated, and try to optimize the script to speed up
358 its execution time.</P
359 ><P
360 >Alter the script so that it accepts any ordinary ASCII
361 text file as input for its initial <SPAN
362 CLASS="QUOTE"
363 >"generation"</SPAN
364 >. The
365 script will read the first <TT
366 CLASS="PARAMETER"
367 ><I
368 >$ROW*$COL</I
369 ></TT
370 >
371 characters, and set the occurrences of vowels as
372 <SPAN
373 CLASS="QUOTE"
374 >"living"</SPAN
375 > cells. Hint: be sure to translate the
376 spaces in the input file to underscore characters.</P
377 ></DIV
378 ></DIV
379 ><DIV
380 CLASS="NAVFOOTER"
381 ><HR
382 ALIGN="LEFT"
383 WIDTH="100%"><TABLE
384 SUMMARY="Footer navigation table"
385 WIDTH="100%"
386 BORDER="0"
387 CELLPADDING="0"
388 CELLSPACING="0"
389 ><TR
390 ><TD
391 WIDTH="33%"
392 ALIGN="left"
393 VALIGN="top"
394 ><A
395 HREF="dosbatch.html"
396 ACCESSKEY="P"
397 >Prev</A
398 ></TD
399 ><TD
400 WIDTH="34%"
401 ALIGN="center"
402 VALIGN="top"
403 ><A
404 HREF="index.html"
405 ACCESSKEY="H"
406 >Home</A
407 ></TD
408 ><TD
409 WIDTH="33%"
410 ALIGN="right"
411 VALIGN="top"
412 ><A
413 HREF="writingscripts.html"
414 ACCESSKEY="N"
415 >Next</A
416 ></TD
417 ></TR
418 ><TR
419 ><TD
420 WIDTH="33%"
421 ALIGN="left"
422 VALIGN="top"
423 >Converting DOS Batch Files to Shell Scripts</TD
424 ><TD
425 WIDTH="34%"
426 ALIGN="center"
427 VALIGN="top"
428 >&nbsp;</TD
429 ><TD
430 WIDTH="33%"
431 ALIGN="right"
432 VALIGN="top"
433 >Writing Scripts</TD
434 ></TR
435 ></TABLE
436 ></DIV
437 ></BODY
438 ></HTML
439 >