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
<
thegrendel.abs@gmail.com
>
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
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
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
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
Advanced Bash-Scripting Guide
Table of Contents
Chapter 36. Miscellany
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
Plik z chomika:
newk
Inne pliki z tego folderu:
advanced_bash_scripting.pdf
(2632 KB)
bash.Cookbook.pdf
(2099 KB)
ShellScripting.pdf
(17672 KB)
Inne foldery tego chomika:
bezpieczenstwo
programowanie
Zgłoś jeśli
naruszono regulamin