Debian Codegraph @

HTML/scrstyle.html

raw symbols SCIP
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&nbsp;PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
138 2&nbsp;# It made perfect sense when you wrote it last year,
139 3&nbsp;#+ but now it's a complete mystery.
140 4&nbsp;# (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&nbsp;#!/bin/bash
157 2&nbsp;
158 3&nbsp;#************************************************#
159 4&nbsp;# xyz.sh #
160 5&nbsp;# written by Bozo Bozeman #
161 6&nbsp;# July 05, 2001 #
162 7&nbsp;# #
163 8&nbsp;# Clean up project files. #
164 9&nbsp;#************************************************#
165 10&nbsp;
166 11&nbsp;E_BADDIR=85 # No such directory.
167 12&nbsp;projectdir=/home/bozo/projects # Directory to clean up.
168 13&nbsp;
169 14&nbsp;# --------------------------------------------------------- #
170 15&nbsp;# cleanup_pfiles () #
171 16&nbsp;# Removes all files in designated directory. #
172 17&nbsp;# Parameter: $target_directory #
173 18&nbsp;# Returns: 0 on success, $E_BADDIR if something went wrong. #
174 19&nbsp;# --------------------------------------------------------- #
175 20&nbsp;cleanup_pfiles ()
176 21&nbsp;{
177 22&nbsp; if [ ! -d "$1" ] # Test if target directory exists.
178 23&nbsp; then
179 24&nbsp; echo "$1 is not a directory."
180 25&nbsp; return $E_BADDIR
181 26&nbsp; fi
182 27&nbsp;
183 28&nbsp; rm -f "$1"/*
184 29&nbsp; return 0 # Success.
185 30&nbsp;}
186 31&nbsp;
187 32&nbsp;cleanup_pfiles $projectdir
188 33&nbsp;
189 34&nbsp;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&nbsp;if [ -f /var/log/messages ]
225 2&nbsp;then
226 3&nbsp; ...
227 4&nbsp;fi
228 5&nbsp;# A year later, you decide to change the script to check /var/log/syslog.
229 6&nbsp;# It is now necessary to manually change the script, instance by instance,
230 7&nbsp;#+ and hope nothing breaks.
231 8&nbsp;
232 9&nbsp;# A better way:
233 10&nbsp;LOGFILE=/var/log/messages # Only line that needs to be changed.
234 11&nbsp;if [ -f "$LOGFILE" ]
235 12&nbsp;then
236 13&nbsp; ...
237 14&nbsp;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&nbsp;fl=`ls -al $dirname` # Cryptic.
256 2&nbsp;file_listing=`ls -al $dirname` # Better.
257 3&nbsp;
258 4&nbsp;
259 5&nbsp;MAXVAL=10 # All caps used for a script constant.
260 6&nbsp;while [ "$index" -le "$MAXVAL" ]
261 7&nbsp;...
262 8&nbsp;
263 9&nbsp;
264 10&nbsp;E_NOTFOUND=95 # Uppercase for an errorcode,
265 11&nbsp; #+ and name prefixed with E_.
266 12&nbsp;if [ ! -e "$filename" ]
267 13&nbsp;then
268 14&nbsp; echo "File $filename not found."
269 15&nbsp; exit $E_NOTFOUND
270 16&nbsp;fi
271 17&nbsp;
272 18&nbsp;
273 19&nbsp;MAIL_DIRECTORY=/var/spool/mail/bozo # Uppercase for an environmental
274 20&nbsp;export MAIL_DIRECTORY #+ variable.
275 21&nbsp;
276 22&nbsp;
277 23&nbsp;GetAnswer () # Mixed case works well for a
278 24&nbsp;{ #+ function name, especially
279 25&nbsp; prompt=$1 #+ when it improves legibility.
280 26&nbsp; echo -n $prompt
281 27&nbsp; read answer
282 28&nbsp; return $answer
283 29&nbsp;}
284 30&nbsp;
285 31&nbsp;GetAnswer "What is your favorite number? "
286 32&nbsp;favorite_number=$?
287 33&nbsp;echo $favorite_number
288 34&nbsp;
289 35&nbsp;
290 36&nbsp;_uservariable=23 # Permissible, but not recommended.
291 37&nbsp;# It's better for user-defined variables not to start with an underscore.
292 38&nbsp;# 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&nbsp;E_WRONG_ARGS=95
316 2&nbsp;...
317 3&nbsp;...
318 4&nbsp;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&nbsp;-a All: Return all information (including hidden file info).
366 2&nbsp;-b Brief: Short version, usually for other scripts.
367 3&nbsp;-c Copy, concatenate, etc.
368 4&nbsp;-d Daily: Use information from the whole day, and not merely
369 5&nbsp; information for a specific instance/user.
370 6&nbsp;-e Extended/Elaborate: (often does not include hidden file info).
371 7&nbsp;-h Help: Verbose usage w/descs, aux info, discussion, help.
372 8&nbsp; See also -V.
373 9&nbsp;-l Log output of script.
374 10&nbsp;-m Manual: Launch man-page for base command.
375 11&nbsp;-n Numbers: Numerical data only.
376 12&nbsp;-r Recursive: All files in a directory (and/or all sub-dirs).
377 13&nbsp;-s Setup &#38; File Maintenance: Config files for this script.
378 14&nbsp;-u Usage: List of invocation flags for the script.
379 15&nbsp;-v Verbose: Human readable output, more or less formatted.
380 16&nbsp;-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&nbsp;COMMAND
413 2&nbsp;if [ $? -eq 0 ]
414 3&nbsp;...
415 4&nbsp;# Redundant and non-intuitive.
416 5&nbsp;
417 6&nbsp;if COMMAND
418 7&nbsp;...
419 8&nbsp;# 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 >&nbsp;</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 >