| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Scripting With Style</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="Advanced Topics" |
| 15 | HREF="part5.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Gotchas" |
| 18 | HREF="gotchas.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Miscellany" |
| 21 | HREF="miscellany.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="CHAPTER" |
| 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="gotchas.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | ></TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="miscellany.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="CHAPTER" |
| 92 | ><H1 |
| 93 | ><A |
| 94 | NAME="SCRSTYLE" |
| 95 | ></A |
| 96 | >Chapter 35. Scripting With Style</H1 |
| 97 | ><P |
| 98 | >Get into the habit of writing shell scripts in a structured and |
| 99 | systematic manner. Even on-the-fly and <SPAN |
| 100 | CLASS="QUOTE" |
| 101 | >"written on the |
| 102 | back of an envelope"</SPAN |
| 103 | > scripts will benefit if you take a |
| 104 | few minutes to plan and organize your thoughts before sitting |
| 105 | down and coding.</P |
| 106 | ><P |
| 107 | >Herewith are a few stylistic guidelines. This is not |
| 108 | (necessarily) intended as an <SPAN |
| 109 | CLASS="emphasis" |
| 110 | ><I |
| 111 | CLASS="EMPHASIS" |
| 112 | >Official Shell Scripting |
| 113 | Stylesheet</I |
| 114 | ></SPAN |
| 115 | >.</P |
| 116 | ><DIV |
| 117 | CLASS="SECT1" |
| 118 | ><H1 |
| 119 | CLASS="SECT1" |
| 120 | ><A |
| 121 | NAME="UNOFFICIALST" |
| 122 | ></A |
| 123 | >35.1. Unofficial Shell Scripting Stylesheet</H1 |
| 124 | ><UL |
| 125 | ><LI |
| 126 | ><P |
| 127 | >Comment your code. This makes it easier for others to |
| 128 | understand (and appreciate), and easier for you to maintain. |
| 129 | <TABLE |
| 130 | BORDER="0" |
| 131 | BGCOLOR="#E0E0E0" |
| 132 | WIDTH="90%" |
| 133 | ><TR |
| 134 | ><TD |
| 135 | ><PRE |
| 136 | CLASS="PROGRAMLISTING" |
| 137 | > 1 PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}" |
| 138 | 2 # It made perfect sense when you wrote it last year, |
| 139 | 3 #+ but now it's a complete mystery. |
| 140 | 4 # (From Antek Sawicki's "pw.sh" script.)</PRE |
| 141 | ></TD |
| 142 | ></TR |
| 143 | ></TABLE |
| 144 | > |
| 145 | </P |
| 146 | ><P |
| 147 | >Add descriptive headers to your scripts and functions. |
| 148 | <TABLE |
| 149 | BORDER="0" |
| 150 | BGCOLOR="#E0E0E0" |
| 151 | WIDTH="90%" |
| 152 | ><TR |
| 153 | ><TD |
| 154 | ><PRE |
| 155 | CLASS="PROGRAMLISTING" |
| 156 | > 1 #!/bin/bash |
| 157 | 2 |
| 158 | 3 #************************************************# |
| 159 | 4 # xyz.sh # |
| 160 | 5 # written by Bozo Bozeman # |
| 161 | 6 # July 05, 2001 # |
| 162 | 7 # # |
| 163 | 8 # Clean up project files. # |
| 164 | 9 #************************************************# |
| 165 | 10 |
| 166 | 11 E_BADDIR=85 # No such directory. |
| 167 | 12 projectdir=/home/bozo/projects # Directory to clean up. |
| 168 | 13 |
| 169 | 14 # --------------------------------------------------------- # |
| 170 | 15 # cleanup_pfiles () # |
| 171 | 16 # Removes all files in designated directory. # |
| 172 | 17 # Parameter: $target_directory # |
| 173 | 18 # Returns: 0 on success, $E_BADDIR if something went wrong. # |
| 174 | 19 # --------------------------------------------------------- # |
| 175 | 20 cleanup_pfiles () |
| 176 | 21 { |
| 177 | 22 if [ ! -d "$1" ] # Test if target directory exists. |
| 178 | 23 then |
| 179 | 24 echo "$1 is not a directory." |
| 180 | 25 return $E_BADDIR |
| 181 | 26 fi |
| 182 | 27 |
| 183 | 28 rm -f "$1"/* |
| 184 | 29 return 0 # Success. |
| 185 | 30 } |
| 186 | 31 |
| 187 | 32 cleanup_pfiles $projectdir |
| 188 | 33 |
| 189 | 34 exit $?</PRE |
| 190 | ></TD |
| 191 | ></TR |
| 192 | ></TABLE |
| 193 | ></P |
| 194 | ></LI |
| 195 | ><LI |
| 196 | ><P |
| 197 | >Avoid using <SPAN |
| 198 | CLASS="QUOTE" |
| 199 | >"magic numbers,"</SPAN |
| 200 | > |
| 201 | |
| 202 | <A |
| 203 | NAME="AEN20041" |
| 204 | HREF="#FTN.AEN20041" |
| 205 | >[1]</A |
| 206 | > |
| 207 | |
| 208 | that is, <SPAN |
| 209 | CLASS="QUOTE" |
| 210 | >"hard-wired"</SPAN |
| 211 | > literal constants. Use |
| 212 | meaningful variable names instead. This makes the script |
| 213 | easier to understand and permits making changes and updates |
| 214 | without breaking the application. |
| 215 | |
| 216 | <TABLE |
| 217 | BORDER="0" |
| 218 | BGCOLOR="#E0E0E0" |
| 219 | WIDTH="90%" |
| 220 | ><TR |
| 221 | ><TD |
| 222 | ><PRE |
| 223 | CLASS="PROGRAMLISTING" |
| 224 | > 1 if [ -f /var/log/messages ] |
| 225 | 2 then |
| 226 | 3 ... |
| 227 | 4 fi |
| 228 | 5 # A year later, you decide to change the script to check /var/log/syslog. |
| 229 | 6 # It is now necessary to manually change the script, instance by instance, |
| 230 | 7 #+ and hope nothing breaks. |
| 231 | 8 |
| 232 | 9 # A better way: |
| 233 | 10 LOGFILE=/var/log/messages # Only line that needs to be changed. |
| 234 | 11 if [ -f "$LOGFILE" ] |
| 235 | 12 then |
| 236 | 13 ... |
| 237 | 14 fi</PRE |
| 238 | ></TD |
| 239 | ></TR |
| 240 | ></TABLE |
| 241 | > |
| 242 | </P |
| 243 | ></LI |
| 244 | ><LI |
| 245 | ><P |
| 246 | >Choose descriptive names for variables and functions. |
| 247 | <TABLE |
| 248 | BORDER="0" |
| 249 | BGCOLOR="#E0E0E0" |
| 250 | WIDTH="90%" |
| 251 | ><TR |
| 252 | ><TD |
| 253 | ><PRE |
| 254 | CLASS="PROGRAMLISTING" |
| 255 | > 1 fl=`ls -al $dirname` # Cryptic. |
| 256 | 2 file_listing=`ls -al $dirname` # Better. |
| 257 | 3 |
| 258 | 4 |
| 259 | 5 MAXVAL=10 # All caps used for a script constant. |
| 260 | 6 while [ "$index" -le "$MAXVAL" ] |
| 261 | 7 ... |
| 262 | 8 |
| 263 | 9 |
| 264 | 10 E_NOTFOUND=95 # Uppercase for an errorcode, |
| 265 | 11 #+ and name prefixed with E_. |
| 266 | 12 if [ ! -e "$filename" ] |
| 267 | 13 then |
| 268 | 14 echo "File $filename not found." |
| 269 | 15 exit $E_NOTFOUND |
| 270 | 16 fi |
| 271 | 17 |
| 272 | 18 |
| 273 | 19 MAIL_DIRECTORY=/var/spool/mail/bozo # Uppercase for an environmental |
| 274 | 20 export MAIL_DIRECTORY #+ variable. |
| 275 | 21 |
| 276 | 22 |
| 277 | 23 GetAnswer () # Mixed case works well for a |
| 278 | 24 { #+ function name, especially |
| 279 | 25 prompt=$1 #+ when it improves legibility. |
| 280 | 26 echo -n $prompt |
| 281 | 27 read answer |
| 282 | 28 return $answer |
| 283 | 29 } |
| 284 | 30 |
| 285 | 31 GetAnswer "What is your favorite number? " |
| 286 | 32 favorite_number=$? |
| 287 | 33 echo $favorite_number |
| 288 | 34 |
| 289 | 35 |
| 290 | 36 _uservariable=23 # Permissible, but not recommended. |
| 291 | 37 # It's better for user-defined variables not to start with an underscore. |
| 292 | 38 # Leave that for system variables.</PRE |
| 293 | ></TD |
| 294 | ></TR |
| 295 | ></TABLE |
| 296 | > |
| 297 | </P |
| 298 | ></LI |
| 299 | ><LI |
| 300 | ><P |
| 301 | >Use <A |
| 302 | HREF="exit-status.html#EXITCOMMANDREF" |
| 303 | >exit codes</A |
| 304 | > |
| 305 | in a systematic and meaningful way. |
| 306 | |
| 307 | <TABLE |
| 308 | BORDER="0" |
| 309 | BGCOLOR="#E0E0E0" |
| 310 | WIDTH="90%" |
| 311 | ><TR |
| 312 | ><TD |
| 313 | ><PRE |
| 314 | CLASS="PROGRAMLISTING" |
| 315 | > 1 E_WRONG_ARGS=95 |
| 316 | 2 ... |
| 317 | 3 ... |
| 318 | 4 exit $E_WRONG_ARGS</PRE |
| 319 | ></TD |
| 320 | ></TR |
| 321 | ></TABLE |
| 322 | > |
| 323 | See also <A |
| 324 | HREF="exitcodes.html" |
| 325 | >Appendix E</A |
| 326 | >.</P |
| 327 | ><P |
| 328 | ><SPAN |
| 329 | CLASS="emphasis" |
| 330 | ><I |
| 331 | CLASS="EMPHASIS" |
| 332 | >Ender</I |
| 333 | ></SPAN |
| 334 | > suggests using the <A |
| 335 | HREF="exitcodes.html#SYSEXITSREF" |
| 336 | >exit codes |
| 337 | in <TT |
| 338 | CLASS="FILENAME" |
| 339 | >/usr/include/sysexits.h</TT |
| 340 | ></A |
| 341 | > in shell |
| 342 | scripts, though these are primarily intended for C and C++ |
| 343 | programming.</P |
| 344 | ></LI |
| 345 | ><LI |
| 346 | ><P |
| 347 | >Use standardized parameter flags for script invocation. |
| 348 | <SPAN |
| 349 | CLASS="emphasis" |
| 350 | ><I |
| 351 | CLASS="EMPHASIS" |
| 352 | >Ender</I |
| 353 | ></SPAN |
| 354 | > proposes the following set |
| 355 | of flags.</P |
| 356 | ><P |
| 357 | > <TABLE |
| 358 | BORDER="0" |
| 359 | BGCOLOR="#E0E0E0" |
| 360 | WIDTH="90%" |
| 361 | ><TR |
| 362 | ><TD |
| 363 | ><PRE |
| 364 | CLASS="PROGRAMLISTING" |
| 365 | > 1 -a All: Return all information (including hidden file info). |
| 366 | 2 -b Brief: Short version, usually for other scripts. |
| 367 | 3 -c Copy, concatenate, etc. |
| 368 | 4 -d Daily: Use information from the whole day, and not merely |
| 369 | 5 information for a specific instance/user. |
| 370 | 6 -e Extended/Elaborate: (often does not include hidden file info). |
| 371 | 7 -h Help: Verbose usage w/descs, aux info, discussion, help. |
| 372 | 8 See also -V. |
| 373 | 9 -l Log output of script. |
| 374 | 10 -m Manual: Launch man-page for base command. |
| 375 | 11 -n Numbers: Numerical data only. |
| 376 | 12 -r Recursive: All files in a directory (and/or all sub-dirs). |
| 377 | 13 -s Setup & File Maintenance: Config files for this script. |
| 378 | 14 -u Usage: List of invocation flags for the script. |
| 379 | 15 -v Verbose: Human readable output, more or less formatted. |
| 380 | 16 -V Version / License / Copy(right|left) / Contribs (email too).</PRE |
| 381 | ></TD |
| 382 | ></TR |
| 383 | ></TABLE |
| 384 | > |
| 385 | </P |
| 386 | ><P |
| 387 | >See also <A |
| 388 | HREF="command-line-options.html#STANDARD-OPTIONS" |
| 389 | >Section G.1</A |
| 390 | >.</P |
| 391 | ></LI |
| 392 | ><LI |
| 393 | ><P |
| 394 | >Break complex scripts into simpler modules. Use functions |
| 395 | where appropriate. See <A |
| 396 | HREF="bash2.html#CARDS" |
| 397 | >Example 37-4</A |
| 398 | >.</P |
| 399 | ></LI |
| 400 | ><LI |
| 401 | ><P |
| 402 | >Don't use a complex construct where a simpler one will do. |
| 403 | |
| 404 | <TABLE |
| 405 | BORDER="0" |
| 406 | BGCOLOR="#E0E0E0" |
| 407 | WIDTH="90%" |
| 408 | ><TR |
| 409 | ><TD |
| 410 | ><PRE |
| 411 | CLASS="PROGRAMLISTING" |
| 412 | > 1 COMMAND |
| 413 | 2 if [ $? -eq 0 ] |
| 414 | 3 ... |
| 415 | 4 # Redundant and non-intuitive. |
| 416 | 5 |
| 417 | 6 if COMMAND |
| 418 | 7 ... |
| 419 | 8 # More concise (if perhaps not quite as legible).</PRE |
| 420 | ></TD |
| 421 | ></TR |
| 422 | ></TABLE |
| 423 | > |
| 424 | </P |
| 425 | ></LI |
| 426 | ></UL |
| 427 | ><TABLE |
| 428 | BORDER="0" |
| 429 | WIDTH="100%" |
| 430 | CELLSPACING="0" |
| 431 | CELLPADDING="0" |
| 432 | CLASS="EPIGRAPH" |
| 433 | ><TR |
| 434 | ><TD |
| 435 | WIDTH="45%" |
| 436 | > </TD |
| 437 | ><TD |
| 438 | WIDTH="45%" |
| 439 | ALIGN="LEFT" |
| 440 | VALIGN="TOP" |
| 441 | ><I |
| 442 | ><P |
| 443 | ><I |
| 444 | >... reading the UNIX source code to the Bourne shell (/bin/sh). I |
| 445 | was shocked at how much simple algorithms could be made cryptic, and |
| 446 | therefore useless, by a poor choice of code style. I asked myself, |
| 447 | <SPAN |
| 448 | CLASS="QUOTE" |
| 449 | >"Could someone be proud of this code?"</SPAN |
| 450 | ></I |
| 451 | ></P |
| 452 | ><P |
| 453 | ><I |
| 454 | >--Landon Noll</I |
| 455 | ></P |
| 456 | ></I |
| 457 | ></TD |
| 458 | ></TR |
| 459 | ></TABLE |
| 460 | ></DIV |
| 461 | ></DIV |
| 462 | ><H3 |
| 463 | CLASS="FOOTNOTES" |
| 464 | >Notes</H3 |
| 465 | ><TABLE |
| 466 | BORDER="0" |
| 467 | CLASS="FOOTNOTES" |
| 468 | WIDTH="100%" |
| 469 | ><TR |
| 470 | ><TD |
| 471 | ALIGN="LEFT" |
| 472 | VALIGN="TOP" |
| 473 | WIDTH="5%" |
| 474 | ><A |
| 475 | NAME="FTN.AEN20041" |
| 476 | HREF="scrstyle.html#AEN20041" |
| 477 | >[1]</A |
| 478 | ></TD |
| 479 | ><TD |
| 480 | ALIGN="LEFT" |
| 481 | VALIGN="TOP" |
| 482 | WIDTH="95%" |
| 483 | ><P |
| 484 | >In this context, <SPAN |
| 485 | CLASS="QUOTE" |
| 486 | >"magic |
| 487 | numbers"</SPAN |
| 488 | > have an entirely different meaning than |
| 489 | the <A |
| 490 | HREF="sha-bang.html#MAGNUMREF" |
| 491 | >magic numbers</A |
| 492 | > used |
| 493 | to designate file types.</P |
| 494 | ></TD |
| 495 | ></TR |
| 496 | ></TABLE |
| 497 | ><DIV |
| 498 | CLASS="NAVFOOTER" |
| 499 | ><HR |
| 500 | ALIGN="LEFT" |
| 501 | WIDTH="100%"><TABLE |
| 502 | SUMMARY="Footer navigation table" |
| 503 | WIDTH="100%" |
| 504 | BORDER="0" |
| 505 | CELLPADDING="0" |
| 506 | CELLSPACING="0" |
| 507 | ><TR |
| 508 | ><TD |
| 509 | WIDTH="33%" |
| 510 | ALIGN="left" |
| 511 | VALIGN="top" |
| 512 | ><A |
| 513 | HREF="gotchas.html" |
| 514 | ACCESSKEY="P" |
| 515 | >Prev</A |
| 516 | ></TD |
| 517 | ><TD |
| 518 | WIDTH="34%" |
| 519 | ALIGN="center" |
| 520 | VALIGN="top" |
| 521 | ><A |
| 522 | HREF="index.html" |
| 523 | ACCESSKEY="H" |
| 524 | >Home</A |
| 525 | ></TD |
| 526 | ><TD |
| 527 | WIDTH="33%" |
| 528 | ALIGN="right" |
| 529 | VALIGN="top" |
| 530 | ><A |
| 531 | HREF="miscellany.html" |
| 532 | ACCESSKEY="N" |
| 533 | >Next</A |
| 534 | ></TD |
| 535 | ></TR |
| 536 | ><TR |
| 537 | ><TD |
| 538 | WIDTH="33%" |
| 539 | ALIGN="left" |
| 540 | VALIGN="top" |
| 541 | >Gotchas</TD |
| 542 | ><TD |
| 543 | WIDTH="34%" |
| 544 | ALIGN="center" |
| 545 | VALIGN="top" |
| 546 | ><A |
| 547 | HREF="part5.html" |
| 548 | ACCESSKEY="U" |
| 549 | >Up</A |
| 550 | ></TD |
| 551 | ><TD |
| 552 | WIDTH="33%" |
| 553 | ALIGN="right" |
| 554 | VALIGN="top" |
| 555 | >Miscellany</TD |
| 556 | ></TR |
| 557 | ></TABLE |
| 558 | ></DIV |
| 559 | ></BODY |
| 560 | ></HTML |
| 561 | > |