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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Scripting With Style</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="Advanced Topics"
HREF="part5.html"><LINK
REL="PREVIOUS"
TITLE="Gotchas"
HREF="gotchas.html"><LINK
REL="NEXT"
TITLE="Miscellany"
HREF="miscellany.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="CHAPTER"
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="gotchas.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="miscellany.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="SCRSTYLE"
></A
>Chapter 35. Scripting With Style</H1
><P
>Get into the habit of writing shell scripts in a structured and
systematic manner. Even on-the-fly and <SPAN
CLASS="QUOTE"
>"written on the
back of an envelope"</SPAN
> scripts will benefit if you take a
few minutes to plan and organize your thoughts before sitting
down and coding.</P
><P
>Herewith are a few stylistic guidelines. This is not
(necessarily) intended as an <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Official Shell Scripting
Stylesheet</I
></SPAN
>.</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="UNOFFICIALST"
></A
>35.1. Unofficial Shell Scripting Stylesheet</H1
><UL
><LI
><P
>Comment your code. This makes it easier for others to
understand (and appreciate), and easier for you to maintain.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
2 # It made perfect sense when you wrote it last year,
3 #+ but now it's a complete mystery.
4 # (From Antek Sawicki's "pw.sh" script.)</PRE
></TD
></TR
></TABLE
>
</P
><P
>Add descriptive headers to your scripts and functions.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 #************************************************#
4 # xyz.sh #
5 # written by Bozo Bozeman #
6 # July 05, 2001 #
7 # #
8 # Clean up project files. #
9 #************************************************#
10
11 E_BADDIR=85 # No such directory.
12 projectdir=/home/bozo/projects # Directory to clean up.
13
14 # --------------------------------------------------------- #
15 # cleanup_pfiles () #
16 # Removes all files in designated directory. #
17 # Parameter: $target_directory #
18 # Returns: 0 on success, $E_BADDIR if something went wrong. #
19 # --------------------------------------------------------- #
20 cleanup_pfiles ()
21 {
22 if [ ! -d "$1" ] # Test if target directory exists.
23 then
24 echo "$1 is not a directory."
25 return $E_BADDIR
26 fi
27
28 rm -f "$1"/*
29 return 0 # Success.
30 }
31
32 cleanup_pfiles $projectdir
33
34 exit $?</PRE
></TD
></TR
></TABLE
></P
></LI
><LI
><P
>Avoid using <SPAN
CLASS="QUOTE"
>"magic numbers,"</SPAN
>
<A
NAME="AEN20041"
HREF="#FTN.AEN20041"
>[1]</A
>
that is, <SPAN
CLASS="QUOTE"
>"hard-wired"</SPAN
> literal constants. Use
meaningful variable names instead. This makes the script
easier to understand and permits making changes and updates
without breaking the application.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 if [ -f /var/log/messages ]
2 then
3 ...
4 fi
5 # A year later, you decide to change the script to check /var/log/syslog.
6 # It is now necessary to manually change the script, instance by instance,
7 #+ and hope nothing breaks.
8
9 # A better way:
10 LOGFILE=/var/log/messages # Only line that needs to be changed.
11 if [ -f "$LOGFILE" ]
12 then
13 ...
14 fi</PRE
></TD
></TR
></TABLE
>
</P
></LI
><LI
><P
>Choose descriptive names for variables and functions.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 fl=`ls -al $dirname` # Cryptic.
2 file_listing=`ls -al $dirname` # Better.
3
4
5 MAXVAL=10 # All caps used for a script constant.
6 while [ "$index" -le "$MAXVAL" ]
7 ...
8
9
10 E_NOTFOUND=95 # Uppercase for an errorcode,
11 #+ and name prefixed with E_.
12 if [ ! -e "$filename" ]
13 then
14 echo "File $filename not found."
15 exit $E_NOTFOUND
16 fi
17
18
19 MAIL_DIRECTORY=/var/spool/mail/bozo # Uppercase for an environmental
20 export MAIL_DIRECTORY #+ variable.
21
22
23 GetAnswer () # Mixed case works well for a
24 { #+ function name, especially
25 prompt=$1 #+ when it improves legibility.
26 echo -n $prompt
27 read answer
28 return $answer
29 }
30
31 GetAnswer "What is your favorite number? "
32 favorite_number=$?
33 echo $favorite_number
34
35
36 _uservariable=23 # Permissible, but not recommended.
37 # It's better for user-defined variables not to start with an underscore.
38 # Leave that for system variables.</PRE
></TD
></TR
></TABLE
>
</P
></LI
><LI
><P
>Use <A
HREF="exit-status.html#EXITCOMMANDREF"
>exit codes</A
>
in a systematic and meaningful way.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 E_WRONG_ARGS=95
2 ...
3 ...
4 exit $E_WRONG_ARGS</PRE
></TD
></TR
></TABLE
>
See also <A
HREF="exitcodes.html"
>Appendix E</A
>.</P
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Ender</I
></SPAN
> suggests using the <A
HREF="exitcodes.html#SYSEXITSREF"
>exit codes
in <TT
CLASS="FILENAME"
>/usr/include/sysexits.h</TT
></A
> in shell
scripts, though these are primarily intended for C and C++
programming.</P
></LI
><LI
><P
>Use standardized parameter flags for script invocation.
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Ender</I
></SPAN
> proposes the following set
of flags.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 -a All: Return all information (including hidden file info).
2 -b Brief: Short version, usually for other scripts.
3 -c Copy, concatenate, etc.
4 -d Daily: Use information from the whole day, and not merely
5 information for a specific instance/user.
6 -e Extended/Elaborate: (often does not include hidden file info).
7 -h Help: Verbose usage w/descs, aux info, discussion, help.
8 See also -V.
9 -l Log output of script.
10 -m Manual: Launch man-page for base command.
11 -n Numbers: Numerical data only.
12 -r Recursive: All files in a directory (and/or all sub-dirs).
13 -s Setup & File Maintenance: Config files for this script.
14 -u Usage: List of invocation flags for the script.
15 -v Verbose: Human readable output, more or less formatted.
16 -V Version / License / Copy(right|left) / Contribs (email too).</PRE
></TD
></TR
></TABLE
>
</P
><P
>See also <A
HREF="command-line-options.html#STANDARD-OPTIONS"
>Section G.1</A
>.</P
></LI
><LI
><P
>Break complex scripts into simpler modules. Use functions
where appropriate. See <A
HREF="bash2.html#CARDS"
>Example 37-4</A
>.</P
></LI
><LI
><P
>Don't use a complex construct where a simpler one will do.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 COMMAND
2 if [ $? -eq 0 ]
3 ...
4 # Redundant and non-intuitive.
5
6 if COMMAND
7 ...
8 # More concise (if perhaps not quite as legible).</PRE
></TD
></TR
></TABLE
>
</P
></LI
></UL
><TABLE
BORDER="0"
WIDTH="100%"
CELLSPACING="0"
CELLPADDING="0"
CLASS="EPIGRAPH"
><TR
><TD
WIDTH="45%"
> </TD
><TD
WIDTH="45%"
ALIGN="LEFT"
VALIGN="TOP"
><I
><P
><I
>... reading the UNIX source code to the Bourne shell (/bin/sh). I
was shocked at how much simple algorithms could be made cryptic, and
therefore useless, by a poor choice of code style. I asked myself,
<SPAN
CLASS="QUOTE"
>"Could someone be proud of this code?"</SPAN
></I
></P
><P
><I
>--Landon Noll</I
></P
></I
></TD
></TR
></TABLE
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN20041"
HREF="scrstyle.html#AEN20041"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>In this context, <SPAN
CLASS="QUOTE"
>"magic
numbers"</SPAN
> have an entirely different meaning than
the <A
HREF="sha-bang.html#MAGNUMREF"
>magic numbers</A
> used
to designate file types.</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="gotchas.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="miscellany.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Gotchas</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Miscellany</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|