advanced_bash_scripting.pdf

(2632 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Advanced Bash-Scripting Guide
An in-depth exploration of the art of shell scripting
Mendel Cooper
6.5
05 Apr 2012
Revision History
Revision 6.3
27 Apr 2011
Revised by: mc
'SWOZZLEBERRY' release
Revision 6.4
30 Aug 2011
Revised by: mc
'VORTEXBERRY' release
Revision 6.5
05 Apr 2012
Revised by: mc
'TUNGSTENBERRY' release
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 nuggets 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.
Dedication
For Anita, the source of all the magic
836945194.096.png
 
Advanced Bash-Scripting Guide
Table of Contents
Chapter 1. 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. .................................................................................30
4.1. Variable Substitution. .....................................................................................................................30
4.2. Variable Assignmen. ......................................................................................................................33
4.3. Bash Variables Are Untyped. .........................................................................................................34
4.4. Special Variable Types. ..................................................................................................................35
Chapter 5. Quoting ...........................................................................................................................................41
5.1. Quoting Variables. ..........................................................................................................................41
5.2. Escaping. .........................................................................................................................................43
Chapter 6. Exit and Exit Status. ......................................................................................................................51
Chapter 7. Tests ................................................................................................................................................54
7.1. Test Constructs ...............................................................................................................................54
7.2. File test operators. ...........................................................................................................................62
7.3. Other Comparison Operators. .........................................................................................................65
7.4. Nested if/then Condition Tests .......................................................................................................70
7.5. Testing Your Knowledge of Tests. .................................................................................................71
Chapter 8. Operations and Related Topics. ...................................................................................................72
8.1. Operators. ........................................................................................................................................72
8.2. Numerical Constants. ......................................................................................................................78
8.3. The Double-Parentheses Construc . ................................................................................................80
8.4. Operator Precedence. ......................................................................................................................81
Part 3. Beyond the Basics. ................................................................................................................................84
Chapter 9. Another Look at Variables ...........................................................................................................85
9.1. Internal Variables. ...........................................................................................................................85
9.2. Typing variables: declare or typeset. ............................................................................................104
9.2.1. Another use for declare. ......................................................................................................106
9.3. $RANDOM: generate random integer. .........................................................................................106
Chapter 10. Manipulating Variables.. ...........................................................................................................119
10.1. Manipulating Strings. ..................................................................................................................119
10.1.1. Manipulating strings using awk. .......................................................................................126
10.1.2. Further Reference. .............................................................................................................127
10.2. Parameter Substitution. ...............................................................................................................127
i
836945194.116.png 836945194.127.png 836945194.001.png 836945194.012.png 836945194.022.png 836945194.033.png 836945194.044.png 836945194.052.png 836945194.053.png 836945194.054.png 836945194.055.png 836945194.056.png 836945194.057.png 836945194.058.png 836945194.059.png 836945194.060.png 836945194.061.png 836945194.062.png 836945194.063.png 836945194.064.png 836945194.065.png 836945194.066.png 836945194.067.png 836945194.068.png 836945194.069.png 836945194.070.png 836945194.071.png 836945194.072.png 836945194.073.png 836945194.074.png 836945194.075.png 836945194.076.png 836945194.077.png 836945194.078.png 836945194.079.png 836945194.080.png
 
Advanced Bash-Scripting Guide
Table of Contents
Chapter 11. Loops and Branches ..................................................................................................................138
11.1. Loops ..........................................................................................................................................138
11.2. Nested Loops. .............................................................................................................................151
11.3. Loop Control. ..............................................................................................................................152
11.4. Testing and Branching. ...............................................................................................................156
Chapter 12. Command Substitution .............................................................................................................164
Chapter 13. Arithmetic Expansion. ...............................................................................................................170
Chapter 14. Recess Time. ...............................................................................................................................171
Part 4. Commands ..........................................................................................................................................172
Chapter 15. Internal Commands and Builtins. ............................................................................................180
15.1. Job Control Commands ..............................................................................................................209
Chapter 16. External Filters, Programs and Commands. ..........................................................................214
16.1. Basic Commands. .......................................................................................................................214
16.2. Complex Commands. ..................................................................................................................219
16.3. Time / Date Commands. .............................................................................................................230
16.4. Text Processing Commands. .......................................................................................................234
16.5. File and Archiving Commands. ..................................................................................................256
16.6. Communications Commands. .....................................................................................................275
16.7. Terminal Control Commands .....................................................................................................289
16.8. Math Commands. ........................................................................................................................290
16.9. Miscellaneous Commands. .........................................................................................................301
Chapter 17. System and Administrative Commands. .................................................................................315
17.1. Analyzing a System Scrip . .........................................................................................................345
Part 5. Advanced Topics.. ...............................................................................................................................347
Chapter 18. Regular Expressions. .................................................................................................................349
18.1. A Brief Introduction to Regular Expressions. .............................................................................349
18.2. Globbing .....................................................................................................................................353
Chapter 19. Here Documents. ........................................................................................................................355
19.1. Here Strings. ...............................................................................................................................365
Chapter 20. I/O Redirection. ..........................................................................................................................369
20.1. Using exec. ..................................................................................................................................372
20.2. Redirecting Code Blocks. ...........................................................................................................375
20.3. Applications. ...............................................................................................................................380
Chapter 21. Subshells .....................................................................................................................................382
ii
836945194.081.png 836945194.082.png 836945194.083.png 836945194.084.png 836945194.085.png 836945194.086.png 836945194.087.png 836945194.088.png 836945194.089.png 836945194.090.png 836945194.091.png 836945194.092.png 836945194.093.png 836945194.094.png 836945194.095.png 836945194.097.png 836945194.098.png 836945194.099.png 836945194.100.png 836945194.101.png 836945194.102.png 836945194.103.png 836945194.104.png 836945194.105.png 836945194.106.png 836945194.107.png 836945194.108.png 836945194.109.png 836945194.110.png 836945194.111.png 836945194.112.png 836945194.113.png 836945194.114.png
 
Advanced Bash-Scripting Guide
Table of Contents
Chapter 22. Restricted Shells. ........................................................................................................................387
Chapter 23. Process Substitution. ..................................................................................................................389
Chapter 24. Functions. ...................................................................................................................................394
24.1. Complex Functions and Function Complexities. ........................................................................398
24.2. Local Variables. ..........................................................................................................................408
24.2.1. Local variables and recursion. ..........................................................................................410
24.3. Recursion Without Local Variables. ...........................................................................................412
Chapter 25. Aliases .........................................................................................................................................416
Chapter 26. List Constructs. ..........................................................................................................................419
Chapter 27. Arrays .........................................................................................................................................423
Chapter 28. Indirect References. ...................................................................................................................452
Chapter 29. /dev and /proc. ............................................................................................................................456
29.1. /dev. .............................................................................................................................................456
29.2. /proc. ...........................................................................................................................................459
Chapter 30. Network Programming .............................................................................................................465
Chapter 31. Of Zeros and Nulls. ....................................................................................................................468
Chapter 32. Debugging. ..................................................................................................................................472
Chapter 33. Options. .......................................................................................................................................483
Chapter 34. Gotchas. ......................................................................................................................................486
Chapter 35. Scripting With Style ..................................................................................................................495
35.1. Unofficial Shell Scripting Styleshee. .........................................................................................495
Chapter 36. Miscellany. ..................................................................................................................................498
36.1. Interactive and non-interactive shells and scripts. ......................................................................498
36.2. Shell Wrappers. ...........................................................................................................................499
36.3. Tests and Comparisons: Alternatives. .........................................................................................504
36.4. Recursion: a script calling itself. .................................................................................................505
36.5. "Colorizing" Scripts. ...................................................................................................................507
36.6. Optimizations. .............................................................................................................................520
36.7. Assorted Tips. .............................................................................................................................521
36.7.1. Ideas for more powerful scripts. ........................................................................................521
36.7.2. Widgets. .............................................................................................................................531
36.8. Security Issues. ...........................................................................................................................533
36.8.1. Infected Shell Scripts.. .......................................................................................................533
36.8.2. Hiding Shell Script Source. ...............................................................................................533
iii
836945194.115.png 836945194.117.png 836945194.118.png 836945194.119.png 836945194.120.png 836945194.121.png 836945194.122.png 836945194.123.png 836945194.124.png 836945194.125.png 836945194.126.png 836945194.128.png 836945194.129.png 836945194.130.png 836945194.131.png 836945194.132.png 836945194.133.png 836945194.134.png 836945194.135.png 836945194.136.png 836945194.137.png 836945194.002.png 836945194.003.png 836945194.004.png 836945194.005.png 836945194.006.png 836945194.007.png 836945194.008.png 836945194.009.png 836945194.010.png 836945194.011.png 836945194.013.png 836945194.014.png
 
Advanced Bash-Scripting Guide
Table of Contents
36.8.3. Writing Secure Shell Scripts. ............................................................................................534
36.9. Portability Issues. ........................................................................................................................534
36.9.1. A Test Suite. ......................................................................................................................535
36.10. Shell Scripting Under Windows. ..............................................................................................536
Chapter 37. Bash, versions 2, 3, and 4 ..........................................................................................................537
37.1. Bash, version 2. ...........................................................................................................................537
37.2. Bash, version 3. ...........................................................................................................................541
37.2.1. Bash, version 3.1. ..............................................................................................................544
37.2.2. Bash, version 3.2. ..............................................................................................................545
37.3. Bash, version 4. ...........................................................................................................................545
37.3.1. Bash, version 4.1. ..............................................................................................................552
37.3.2. Bash, version 4.2. ..............................................................................................................553
Chapter 38. Endnotes. ....................................................................................................................................557
38.1. Author's Note. .............................................................................................................................557
38.2. About the Author. .......................................................................................................................557
38.3. Where to Go For Help.. ...............................................................................................................557
38.4. Tools Used to Produce This Book. .............................................................................................558
38.4.1. Hardware. ..........................................................................................................................558
38.4.2. Software and Printware. ....................................................................................................558
38.5. Credits. ........................................................................................................................................558
38.6. Disclaimer. ..................................................................................................................................560
Bibliography. ...................................................................................................................................................561
Appendix A. Contributed Scripts. .................................................................................................................568
Appendix B. Reference Cards. .......................................................................................................................772
Appendix C. A Sed and Awk Micro-Primer. ...............................................................................................777
C.1. Sed. ...............................................................................................................................................777
C.2. Awk. .............................................................................................................................................780
Appendix D. Exit Codes With Special Meanings. ........................................................................................783
Appendix E. A Detailed Introduction to I/O and I/O Redirection. ............................................................784
Appendix F. Command-Line Options. ..........................................................................................................786
F.1. Standard Command-Line Options. ...............................................................................................786
F.2. Bash Command-Line Options. .....................................................................................................787
Appendix G. Important Files. ........................................................................................................................789
Appendix H. Important System Directories. ................................................................................................790
iv
836945194.015.png 836945194.016.png 836945194.017.png 836945194.018.png 836945194.019.png 836945194.020.png 836945194.021.png 836945194.023.png 836945194.024.png 836945194.025.png 836945194.026.png 836945194.027.png 836945194.028.png 836945194.029.png 836945194.030.png 836945194.031.png 836945194.032.png 836945194.034.png 836945194.035.png 836945194.036.png 836945194.037.png 836945194.038.png 836945194.039.png 836945194.040.png 836945194.041.png 836945194.042.png 836945194.043.png 836945194.045.png 836945194.046.png 836945194.047.png 836945194.048.png 836945194.049.png 836945194.050.png 836945194.051.png
 
Zgłoś jeśli naruszono regulamin