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]> 3.8 26 February 2006 Revision History Revision 3.6 28 Aug 2005 Revised by: mc 'POKEBERRY' release: Bugfix Update. Revision 3.7 23 Oct 2005 Revised by: mc 'WHORTLEBERRY' release: Bugfix Update. Revision 3.8 26 Feb 2006 Revised by: mc 'BLAEBERRY' release: Minor 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. 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..................................................................................25 4.1. Variable Substitution......................................................................................................................25 4.2. Variable Assignment.......................................................................................................................27 4.3. Bash Variables Are Untyped..........................................................................................................29 4.4. Special Variable Types...................................................................................................................30 Chapter 5. Quoting...........................................................................................................................................34 5.1. Quoting Variables...........................................................................................................................34 5.2. Escaping..........................................................................................................................................35 Chapter 6. Exit and Exit Status.......................................................................................................................41 Chapter 7. Tests................................................................................................................................................43 7.1. Test Constructs...............................................................................................................................43 7.2. File test operators............................................................................................................................49 7.3. Other Comparison Operators..........................................................................................................52 7.4. Nested if/then Condition Tests.......................................................................................................57 7.5. Testing Your Knowledge of Tests..................................................................................................57 Chapter 8. Operations and Related Topics....................................................................................................59 8.1. Operators.........................................................................................................................................59 8.2. Numerical Constants.......................................................................................................................65 Part 3. Beyond the Basics.................................................................................................................................67 Chapter 9. Variables Revisited........................................................................................................................68 9.1. Internal Variables............................................................................................................................68 9.2. Manipulating Strings.......................................................................................................................84 9.2.1. Manipulating strings using awk............................................................................................90 9.2.2. Further Discussion.................................................................................................................90 9.3. Parameter Substitution....................................................................................................................90 9.4. Typing variables: declare or typeset...............................................................................................99 9.5. Indirect References to Variables...................................................................................................101 9.6. $RANDOM: generate random integer..........................................................................................104 9.7. The Double Parentheses Construct...............................................................................................113 i Advanced Bash−Scripting Guide Table of Contents Chapter 10. Loops and Branches..................................................................................................................115 10.1. Loops..........................................................................................................................................115 10.2. Nested Loops..............................................................................................................................126 10.3. Loop Control...............................................................................................................................126 10.4. Testing and Branching................................................................................................................130 Chapter 11. Internal Commands and Builtins.............................................................................................137 11.1. Job Control Commands..............................................................................................................161 Chapter 12. External Filters, Programs and Commands...........................................................................166 12.1. Basic Commands........................................................................................................................166 12.2. Complex Commands...................................................................................................................171 12.3. Time / Date Commands..............................................................................................................180 12.4. Text Processing Commands........................................................................................................183 12.5. File and Archiving Commands...................................................................................................202 12.6. Communications Commands......................................................................................................218 12.7. Terminal Control Commands.....................................................................................................232 12.8. Math Commands.........................................................................................................................233 12.9. Miscellaneous Commands..........................................................................................................242 Chapter 13. System and Administrative Commands..................................................................................254 13.1. Analyzing a System Script..........................................................................................................279 Chapter 14. Command Substitution.............................................................................................................281 Chapter 15. Arithmetic Expansion................................................................................................................287 Chapter 16. I/O Redirection...........................................................................................................................288