| 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 | > On a dingy side street in a run-down section of Hoboken, New Jersey,<br> |
| 102 | there sits a nondescript squat two-story brick building with an inscription<br> |
| 103 | incised on a marble plate in its wall:<br> |
| 104 | <br> |
| 105 | <TT |
| 106 | CLASS="COMPUTEROUTPUT" |
| 107 | >Bash Scripting Hall of Fame</TT |
| 108 | >.<br> |
| 109 | <br> |
| 110 | Inside, among various dusty uninteresting exhibits is a corroding,<br> |
| 111 | cobweb-festooned brass plaque inscribed with a short, very short<br> |
| 112 | list of those few persons who have successfully mastered the material<br> |
| 113 | in the <I |
| 114 | CLASS="FIRSTTERM" |
| 115 | >Advanced Bash Scripting Guide</I |
| 116 | >, as evidenced by their performance<br> |
| 117 | on the following Exercise sections.<br> |
| 118 | <br> |
| 119 | (Alas, the author of the <I |
| 120 | CLASS="FIRSTTERM" |
| 121 | >ABS Guide</I |
| 122 | > is not represented among the exhibits.<br> |
| 123 | This is possibly due to malicious rumors about <A |
| 124 | HREF="aboutauthor.html#NOCREDS" |
| 125 | >lack of credentials</A |
| 126 | > and<br> |
| 127 | <A |
| 128 | HREF="contributed-scripts.html#KTOUR0" |
| 129 | >deficient scripting skills</A |
| 130 | >.)<br> |
| 131 | </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 #!/bin/bash |
| 156 | 2 |
| 157 | 3 MAX=10000 |
| 158 | 4 |
| 159 | 5 |
| 160 | 6 for((nr=1; nr<$MAX; nr++)) |
| 161 | 7 do |
| 162 | 8 |
| 163 | 9 let "t1 = nr % 5" |
| 164 | 10 if [ "$t1" -ne 3 ] |
| 165 | 11 then |
| 166 | 12 continue |
| 167 | 13 fi |
| 168 | 14 |
| 169 | 15 let "t2 = nr % 7" |
| 170 | 16 if [ "$t2" -ne 4 ] |
| 171 | 17 then |
| 172 | 18 continue |
| 173 | 19 fi |
| 174 | 20 |
| 175 | 21 let "t3 = nr % 9" |
| 176 | 22 if [ "$t3" -ne 5 ] |
| 177 | 23 then |
| 178 | 24 continue |
| 179 | 25 fi |
| 180 | 26 |
| 181 | 27 break # What happens when you comment out this line? Why? |
| 182 | 28 |
| 183 | 29 done |
| 184 | 30 |
| 185 | 31 echo "Number = $nr" |
| 186 | 32 |
| 187 | 33 |
| 188 | 34 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 #!/bin/bash |
| 209 | 2 |
| 210 | 3 DIRNAME=/usr/bin |
| 211 | 4 FILETYPE="shell script" |
| 212 | 5 LOGFILE=logfile |
| 213 | 6 |
| 214 | 7 file "$DIRNAME"/* | fgrep "$FILETYPE" | tee $LOGFILE | wc -l |
| 215 | 8 |
| 216 | 9 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 #!/bin/bash |
| 243 | 2 |
| 244 | 3 # Author: Nathan Coulter |
| 245 | 4 # This code is released to the public domain. |
| 246 | 5 # The author gave permission to use this code snippet in the ABS Guide. |
| 247 | 6 |
| 248 | 7 find -maxdepth 1 -type f -printf '%f\000' | { |
| 249 | 8 while read -d $'\000'; do |
| 250 | 9 mv "$REPLY" "$(date -d "$(stat -c '%y' "$REPLY") " '+%Y%m%d%H%M%S' |
| 251 | 10 )-$REPLY" |
| 252 | 11 done |
| 253 | 12 } |
| 254 | 13 |
| 255 | 14 # Warning: Test-drive this script in a "scratch" directory. |
| 256 | 15 # 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 while read LINE |
| 276 | 2 do |
| 277 | 3 echo $LINE |
| 278 | 4 done < `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 export SUM=0; for f in $(find src -name "*.java"); |
| 322 | 2 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 | > </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 | > |