Advanced Bash-Scripting Guide
Total Page:16
File Type:pdf, Size:1020Kb
Advanced Bash-Scripting Guide An in-depth exploration of the art of shell scripting Mendel Cooper <[email protected]> 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 Assignment.......................................................................................................................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 Construct.................................................................................................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