Advanced-Bash-Scripting-Guide.pdf

(1710 KB) Pobierz
Advanced Bash-Scripting Guide
Advanced Bash−Scripting Guide
An in−depth exploration of the art of shell scripting
Mendel Cooper
3.2
06 February 2005
Revision History
Revision 3.0
03 Oct 2004
Revised by: mc
'LOGANBERRY' release: Major update.
Revision 3.1
14 Nov 2004
Revised by: mc
'BAYBERRY' release: Bugfix update.
Revision 3.2
06 Feb 2005
Revised by: mc
'BLUEBERRY' release: Minor update.
This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an
intermediate/advanced level of instruction . . . all the while sneaking in little snippets of UNIX® wisdom and
lore . It serves as a textbook, a manual for self−study, and a reference and source of knowledge on shell
scripting techniques. The exercises and heavily−commented examples invite active reader participation, under
the premise that the only way to really learn scripting is to write scripts .
This book is suitable for classroom use as a general introduction to programming concepts.
The latest update of this document, as an archived, bzip2−ed "tarball" including both the SGML source and
rendered HTML, may be downloaded from the author's home site. A pdf version is also available. See the
change log for a revision history.
Dedication
For Anita, the source of all the magic
8011929.003.png
 
Advanced Bash−Scripting Guide
Table of Contents
Chapter 1. Why Shell Programming?. ..............................................................................................................1
Chapter 2. Starting Off With a Sha−Bang. ......................................................................................................3
2.1. Invoking the script. ...........................................................................................................................6
2.2. Preliminary Exercises. ......................................................................................................................6
Part 2. Basics. ......................................................................................................................................................7
Chapter 3. Special Characters. ..........................................................................................................................8
Chapter 4. Introduction to Variables and Parameters. .................................................................................25
4.1. Variable Substitution. .....................................................................................................................25
4.2. Variable Assignmen. ......................................................................................................................27
4.3. Bash Variables Are Untyped. .........................................................................................................28
4.4. Special Variable Types. ..................................................................................................................29
Chapter 5. Quoting ...........................................................................................................................................34
Chapter 6. Exit and Exit Status. ......................................................................................................................40
Chapter 7. Tests ................................................................................................................................................42
7.1. Test Constructs ...............................................................................................................................42
7.2. File test operators. ...........................................................................................................................48
7.3. Other Comparison Operators. .........................................................................................................51
7.4. Nested if/then Condition Tests .......................................................................................................56
7.5. Testing Your Knowledge of Tests. .................................................................................................56
Chapter 8. Operations and Related Topics. ...................................................................................................58
8.1. Operators. ........................................................................................................................................58
8.2. Numerical Constants. ......................................................................................................................64
Part 3. Beyond the Basics. ................................................................................................................................66
Chapter 9. Variables Revisited. .......................................................................................................................67
9.1. Internal Variables. ...........................................................................................................................67
9.2. Manipulating Strings.. .....................................................................................................................84
9.2.1. Manipulating strings using awk. ...........................................................................................88
9.2.2. Further Discussion. ................................................................................................................88
9.3. Parameter Substitution. ...................................................................................................................89
9.4. Typing variables: declare or typeset. ..............................................................................................97
9.5. Indirect References to Variables. ....................................................................................................99
9.6. $RANDOM: generate random integer. .........................................................................................101
9.7. The Double Parentheses Construct. ..............................................................................................110
Chapter 10. Loops and Branches ..................................................................................................................112
10.1. Loops ..........................................................................................................................................112
10.2. Nested Loops. .............................................................................................................................122
10.3. Loop Control. ..............................................................................................................................123
i
8011929.004.png
Advanced Bash−Scripting Guide
Table of Contents
10.4. Testing and Branching. ...............................................................................................................126
Chapter 11. Internal Commands and Builtins. ............................................................................................134
11.1. Job Control Commands ..............................................................................................................156
Chapter 12. External Filters, Programs and Commands. ..........................................................................160
12.1. Basic Commands. .......................................................................................................................160
12.2. Complex Commands. ..................................................................................................................164
12.3. Time / Date Commands. .............................................................................................................173
12.4. Text Processing Commands. .......................................................................................................175
12.5. File and Archiving Commands. ..................................................................................................192
12.6. Communications Commands. .....................................................................................................207
12.7. Terminal Control Commands .....................................................................................................216
12.8. Math Commands. ........................................................................................................................217
12.9. Miscellaneous Commands. .........................................................................................................226
Chapter 13. System and Administrative Commands. .................................................................................236
Chapter 14. Command Substitution .............................................................................................................260
Chapter 15. Arithmetic Expansion. ...............................................................................................................265
Chapter 16. I/O Redirection. ..........................................................................................................................266
16.1. Using exec. ..................................................................................................................................268
16.2. Redirecting Code Blocks. ...........................................................................................................272
16.3. Applications. ...............................................................................................................................276
Chapter 17. Here Documents. ........................................................................................................................278
17.1. Here Strings. ...............................................................................................................................286
Chapter 18. Recess Time. ...............................................................................................................................288
Part 4. Advanced Topics.. ...............................................................................................................................289
Chapter 19. Regular Expressions. .................................................................................................................290
19.1. A Brief Introduction to Regular Expressions. .............................................................................290
19.2. Globbing .....................................................................................................................................293
Chapter 20. Subshells .....................................................................................................................................295
Chapter 21. Restricted Shells. ........................................................................................................................298
Chapter 22. Process Substitution. ..................................................................................................................300
Chapter 23. Functions. ...................................................................................................................................303
23.1. Complex Functions and Function Complexities. ........................................................................305
23.2. Local Variables. ..........................................................................................................................315
ii
8011929.005.png
Advanced Bash−Scripting Guide
Table of Contents
23.2.1. Local variables help make recursion possible.. .................................................................316
23.3. Recursion Without Local Variables. ...........................................................................................317
Chapter 24. Aliases .........................................................................................................................................320
Chapter 25. List Constructs. ..........................................................................................................................323
Chapter 26. Arrays .........................................................................................................................................326
Chapter 27. Files .............................................................................................................................................352
Chapter 28. /dev and /proc. ............................................................................................................................353
28.1. /dev. .............................................................................................................................................353
28.2. /proc. ...........................................................................................................................................354
Chapter 29. Of Zeros and Nulls. ....................................................................................................................359
Chapter 30. Debugging. ..................................................................................................................................362
Chapter 31. Options. .......................................................................................................................................370
Chapter 32. Gotchas. ......................................................................................................................................372
Chapter 33. Scripting With Style ..................................................................................................................379
33.1. Unofficial Shell Scripting Styleshee. .........................................................................................379
Chapter 34. Miscellany. ..................................................................................................................................382
34.1. Interactive and non−interactive shells and scripts. .....................................................................382
34.2. Shell Wrappers. ...........................................................................................................................383
34.3. Tests and Comparisons: Alternatives. .........................................................................................386
34.4. Recursion. ...................................................................................................................................387
34.5. "Colorizing" Scripts. ...................................................................................................................389
34.6. Optimizations. .............................................................................................................................395
34.7. Assorted Tips. .............................................................................................................................396
34.8. Security Issues. ...........................................................................................................................405
34.9. Portability Issues. ........................................................................................................................405
34.10. Shell Scripting Under Windows. ..............................................................................................406
Chapter 35. Bash, versions 2 and 3. ..............................................................................................................407
35.1. Bash, version2. ............................................................................................................................407
35.2. Bash, version3. ............................................................................................................................411
Chapter 36. Endnotes .....................................................................................................................................413
36.1. Author's Note. .............................................................................................................................413
36.2. About the Author. .......................................................................................................................413
36.3. Where to Go For Help.. ...............................................................................................................413
36.4. Tools Used to Produce This Book. .............................................................................................414
iii
8011929.001.png
Advanced Bash−Scripting Guide
Table of Contents
36.4.1. Hardware. ..........................................................................................................................414
36.4.2. Software and Printware. ....................................................................................................414
36.5. Credits. ........................................................................................................................................414
Bibliography. ...................................................................................................................................................416
Appendix A. Contributed Scripts. .................................................................................................................422
Appendix B. Reference Cards. .......................................................................................................................538
Appendix C. A Sed and Awk Micro−Primer. ...............................................................................................543
C.1. Sed. ...............................................................................................................................................543
C.2. Awk. .............................................................................................................................................545
Appendix D. Exit Codes With Special Meanings. ........................................................................................548
Appendix E. A Detailed Introduction to I/O and I/O Redirection. ............................................................549
Appendix F. Standard Command−Line Options. ........................................................................................551
Appendix G. Important System Directories. ................................................................................................553
Appendix H. Localization. ..............................................................................................................................555
Appendix I. History Commands. ...................................................................................................................559
Appendix J. A Sample .bashrc File. ..............................................................................................................560
Appendix K. Converting DOS Batch Files to Shell Scripts. .......................................................................571
Appendix L. Exercises. ...................................................................................................................................575
L.1. Analyzing Scripts. ........................................................................................................................575
L.2. Writing Scripts .............................................................................................................................576
Appendix M. Revision History. ......................................................................................................................583
Appendix N. Mirror Sites. ..............................................................................................................................584
Appendix O. To Do Lis. .................................................................................................................................585
Appendix P. Copyright. ..................................................................................................................................587
Notes. ...................................................................................................................................................588
iv
8011929.002.png
Zgłoś jeśli naruszono regulamin