abs-guide.pdf

(2354 KB) Pobierz
Advanced Bash-Scripting Guide
An in-depth exploration of the art of shell scripting
Mendel Cooper
<thegrendel@theriver.com>
5.3
11 May 2008
Revision History
Revision 5.1
10 Nov 2007
'LINGONBERRY' release: Minor Update.
Revision 5.2
16 Mar 2008
'SILVERBERRY' release: Important Update.
Revised by: mc
Revised by: mc
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 ( pdf mirror
site). See the change log for a revision history.
Dedication
For Anita, the source of all the magic
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
..................................................................................28
4.1. Variable Substitution......................................................................................................................28
4.2. Variable Assignment......................................................................................................................31
.
4.3. Bash Variables Are Untyped..........................................................................................................32
4.4. Special Variable Types...................................................................................................................33
Chapter 5. Quoting...........................................................................................................................................38
5.1. Quoting Variables...........................................................................................................................38
5.2. Escaping..........................................................................................................................................40
Chapter 6. Exit and Exit Status.......................................................................................................................45
Chapter 7. Tests................................................................................................................................................47
7.1. Test Constructs...............................................................................................................................47
7.2. File test operators............................................................................................................................54
7.3. Other Comparison Operators..........................................................................................................57
7.4. Nested if/then Condition Tests.......................................................................................................62
7.5. Testing Your Knowledge of Tests..................................................................................................62
Chapter 8. Operations and Related Topics....................................................................................................63
8.1. Operators.........................................................................................................................................63
8.2. Numerical Constants.......................................................................................................................69
Part 3. Beyond the Basics.................................................................................................................................71
Chapter 9. Variables Revisited........................................................................................................................72
9.1. Internal Variables............................................................................................................................72
9.2. Manipulating Strings
.......................................................................................................................89
9.2.1. Manipulating strings using awk............................................................................................96
9.2.2. Further Discussion
.................................................................................................................97
9.3. Parameter Substitution....................................................................................................................97
9.4. Typing variables: declare or typeset.............................................................................................106
9.5. Indirect References.......................................................................................................................108
9.6. $RANDOM: generate random integer..........................................................................................111
9.7. The Double Parentheses Construct...............................................................................................122
i
Advanced Bash-Scripting Guide
Table of Contents
Chapter 10. Loops and Branches..................................................................................................................124
10.1. Loops..........................................................................................................................................124
10.2. Nested Loops..............................................................................................................................136
10.3. Loop Control...............................................................................................................................137
10.4. Testing and Branching................................................................................................................140
Chapter 11. Command Substitution.............................................................................................................148
Chapter 12. Arithmetic Expansion................................................................................................................154
Chapter 13. Recess Time................................................................................................................................155
Part 4. Commands..........................................................................................................................................156
Chapter 14. Internal Commands and Builtins.............................................................................................164
14.1. Job Control Commands..............................................................................................................191
Chapter 15. External Filters, Programs and Commands...........................................................................196
15.1. Basic Commands........................................................................................................................196
15.2. Complex Commands
...................................................................................................................201
15.3. Time / Date Commands..............................................................................................................211
15.4. Text Processing Commands
........................................................................................................215
15.5. File and Archiving Commands...................................................................................................237
15.6. Communications Commands......................................................................................................254
15.7. Terminal Control Commands.....................................................................................................268
15.8. Math Commands.........................................................................................................................269
15.9. Miscellaneous Commands..........................................................................................................278
Chapter 16. System and Administrative Commands..................................................................................292
16.1. Analyzing a System Script..........................................................................................................321
Part 5. Advanced Topics
.................................................................................................................................323
Chapter 17. Regular Expressions..................................................................................................................325
17.1. A Brief Introduction to Regular Expressions
..............................................................................325
17.2. Globbing.....................................................................................................................................328
Chapter 18. Here Documents.........................................................................................................................330
18.1. Here Strings................................................................................................................................340
Chapter 19. I/O Redirection
...........................................................................................................................343
19.1. Using exec
...................................................................................................................................346
19.2. Redirecting Code Blocks............................................................................................................349
19.3. Applications................................................................................................................................354
Chapter 20. Subshells.....................................................................................................................................356
ii
Advanced Bash-Scripting Guide
Table of Contents
Chapter 21. Restricted Shells.........................................................................................................................361
Chapter 22. Process Substitution
...................................................................................................................363
Chapter 23. Functions....................................................................................................................................366
23.1. Complex Functions and Function Complexities.........................................................................369
23.2. Local Variables...........................................................................................................................379
23.2.1. Local variables and recursion............................................................................................380
23.3. Recursion Without Local Variables............................................................................................382
Chapter 24. Aliases.........................................................................................................................................385
Chapter 25. List Constructs...........................................................................................................................388
Chapter 26. Arrays.........................................................................................................................................391
Chapter 27. /dev and /proc.............................................................................................................................419
27.1. /dev
..............................................................................................................................................419
27.2. /proc............................................................................................................................................421
Chapter 28. Of Zeros and Nulls.....................................................................................................................427
Chapter 29. Debugging...................................................................................................................................431
Chapter 30. Options........................................................................................................................................441
Chapter 31. Gotchas.......................................................................................................................................443
Chapter 32. Scripting With Style..................................................................................................................451
32.1. Unofficial Shell Scripting Stylesheet..........................................................................................451
Chapter 33. Miscellany...................................................................................................................................454
33.1. Interactive and non-interactive shells and scripts.......................................................................454
33.2. Operator Precedence...................................................................................................................455
33.3. Shell Wrappers............................................................................................................................457
33.4. Tests and Comparisons: Alternatives
..........................................................................................462
33.5. A script calling itself (recursion)................................................................................................462
33.6. "Colorizing" Scripts....................................................................................................................465
33.7. Optimizations..............................................................................................................................477
33.8. Assorted Tips..............................................................................................................................478
33.8.1. Ideas for more powerful scripts.........................................................................................478
33.8.2. Widgets
..............................................................................................................................488
33.9. Security Issues............................................................................................................................489
33.9.1. Infected Shell Scripts
.........................................................................................................489
33.9.2. Hiding Shell Script Source................................................................................................490
33.9.3. Writing Secure Shell Scripts.............................................................................................490
33.10. Portability Issues.......................................................................................................................490
33.11. Shell Scripting Under Windows...............................................................................................491
iii
Advanced Bash-Scripting Guide
Table of Contents
Chapter 34. Bash, versions 2 and 3...............................................................................................................492
34.1. Bash, version 2............................................................................................................................492
34.2. Bash, version 3............................................................................................................................496
34.2.1. Bash, version 3.1...............................................................................................................498
34.2.2. Bash, version 3.2...............................................................................................................499
Chapter 35. Endnotes.....................................................................................................................................500
35.1. Author's Note..............................................................................................................................500
35.2. About the Author........................................................................................................................500
35.3. Where to Go For Help
.................................................................................................................500
35.4. Tools Used to Produce This Book..............................................................................................501
35.4.1. Hardware...........................................................................................................................501
35.4.2. Software and Printware.....................................................................................................501
35.5. Credits.........................................................................................................................................501
35.6. Disclaimer...................................................................................................................................503
Bibliography....................................................................................................................................................504
Appendix A. Contributed Scripts..................................................................................................................511
Appendix B. Reference Cards........................................................................................................................673
Appendix C. A Sed and Awk Micro-Primer................................................................................................678
C.1. Sed................................................................................................................................................678
C.2. Awk..............................................................................................................................................681
Appendix D. Exit Codes With Special Meanings.........................................................................................684
Appendix E. A Detailed Introduction to I/O and I/O Redirection.............................................................685
Appendix F. Command-Line Options...........................................................................................................687
F.1. Standard Command-Line Options................................................................................................687
F.2. Bash Command-Line Options......................................................................................................688
Appendix G. Important Files.........................................................................................................................690
Appendix H. Important System Directories.................................................................................................691
Appendix I. Localization................................................................................................................................693
Appendix J. History Commands...................................................................................................................697
Appendix K. A Sample .bashrc File..............................................................................................................698
Appendix L. Converting DOS Batch Files to Shell Scripts.........................................................................710
iv
Zgłoś jeśli naruszono regulamin