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]> 5.0 24 June 2007 Revision History Revision 4.2 10 Dec 2006 Revised by: mc 'SPARKLEBERRY' release: Important Update. Revision 4.3 29 Apr 2007 Revised by: mc 'INKBERRY' release: Minor Update. Revision 5.0 24 Jun 2007 Revised by: mc 'SERVICEBERRY' release: Major 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 ( 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..................................................................................26 4.1. Variable Substitution......................................................................................................................26 4.2. Variable Assignment.......................................................................................................................29 4.3. Bash Variables Are Untyped..........................................................................................................30 4.4. Special Variable Types...................................................................................................................31 Chapter 5. Quoting...........................................................................................................................................36 5.1. Quoting Variables...........................................................................................................................36 5.2. Escaping..........................................................................................................................................38 Chapter 6. Exit and Exit Status.......................................................................................................................43 Chapter 7. Tests................................................................................................................................................45 7.1. Test Constructs...............................................................................................................................45 7.2. File test operators............................................................................................................................51 7.3. Other Comparison Operators..........................................................................................................54 7.4. Nested if/then Condition Tests.......................................................................................................59 7.5. Testing Your Knowledge of Tests..................................................................................................60 Chapter 8. Operations and Related Topics....................................................................................................61 8.1. Operators.........................................................................................................................................61 8.2. Numerical Constants.......................................................................................................................67 Part 3. Beyond the Basics.................................................................................................................................69 Chapter 9. Variables Revisited........................................................................................................................70 9.1. Internal Variables............................................................................................................................70 9.2. Manipulating Strings.......................................................................................................................87 9.2.1. Manipulating strings using awk............................................................................................93 9.2.2. Further Discussion.................................................................................................................94 9.3. Parameter Substitution....................................................................................................................94 9.4. Typing variables: declare or typeset.............................................................................................103 9.5. Indirect References.......................................................................................................................105 9.6. $RANDOM: generate random integer..........................................................................................108 9.7. The Double Parentheses Construct...............................................................................................117 i Advanced Bash−Scripting Guide Table of Contents Chapter 10. Loops and Branches..................................................................................................................120 10.1. Loops..........................................................................................................................................120 10.2. Nested Loops..............................................................................................................................132 10.3. Loop Control...............................................................................................................................133 10.4. Testing and Branching................................................................................................................136 Chapter 11. Command Substitution.............................................................................................................144 Chapter 12. Arithmetic Expansion................................................................................................................150 Chapter 13. Recess Time................................................................................................................................151 Part 4. Commands..........................................................................................................................................152 Chapter 14. Internal Commands and Builtins.............................................................................................160 14.1. Job Control Commands..............................................................................................................187 Chapter 15. External Filters, Programs and Commands...........................................................................191 15.1. Basic Commands........................................................................................................................191 15.2. Complex Commands...................................................................................................................196 15.3. Time / Date Commands..............................................................................................................206 15.4. Text Processing Commands........................................................................................................209 15.5. File and Archiving Commands...................................................................................................229 15.6. Communications Commands......................................................................................................246 15.7. Terminal Control Commands.....................................................................................................260 15.8. Math Commands.........................................................................................................................261 15.9. Miscellaneous Commands..........................................................................................................270 Chapter 16. System and Administrative Commands..................................................................................283