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]> 2.5 15 February 2004 Revision History Revision 2.3 03 January 2004 Revised by: mc 'STRAWBERRY' release: Major update. Revision 2.4 25 January 2004 Revised by: mc 'MUSKMELON' release: Bugfixes. Revision 2.5 15 February 2004 Revised by: mc 'STARFRUIT' release: Bugfixes and more material. 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. 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............................................................................................................................5 2.2. Preliminary Exercises.......................................................................................................................5 Part 2. Basics.......................................................................................................................................................6 Chapter 3. Special Characters...........................................................................................................................7 Chapter 4. Introduction to Variables and Parameters..................................................................................23 4.1. Variable Substitution......................................................................................................................23 4.2. Variable Assignment.......................................................................................................................25 4.3. Bash Variables Are Untyped..........................................................................................................26 4.4. Special Variable Types...................................................................................................................28 Chapter 5. Quoting...........................................................................................................................................32 Chapter 6. Exit and Exit Status.......................................................................................................................38 Chapter 7. Tests................................................................................................................................................40 7.1. Test Constructs...............................................................................................................................40 7.2. File test operators............................................................................................................................46 7.3. Comparison operators (binary).......................................................................................................49 7.4. Nested if/then Condition Tests.......................................................................................................54 7.5. Testing Your Knowledge of Tests..................................................................................................54 Chapter 8. Operations and Related Topics....................................................................................................55 8.1. Operators.........................................................................................................................................55 8.2. Numerical Constants.......................................................................................................................61 Part 3. Beyond the Basics.................................................................................................................................63 Chapter 9. Variables Revisited........................................................................................................................64 9.1. Internal Variables............................................................................................................................64 9.2. Manipulating Strings.......................................................................................................................80 9.2.1. Manipulating strings using awk............................................................................................84 9.2.2. Further Discussion.................................................................................................................85 9.3. Parameter Substitution....................................................................................................................85 9.4. Typing variables: declare or typeset...............................................................................................93 9.5. Indirect References to Variables.....................................................................................................95 9.6. $RANDOM: generate random integer............................................................................................97 9.7. The Double Parentheses Construct...............................................................................................106 Chapter 10. Loops and Branches..................................................................................................................108 10.1. Loops..........................................................................................................................................108 10.2. Nested Loops..............................................................................................................................118 10.3. Loop Control...............................................................................................................................119 i Advanced Bash−Scripting Guide Table of Contents Chapter 10. Loops and Branches 10.4. Testing and Branching................................................................................................................122 Chapter 11. Internal Commands and Builtins.............................................................................................130 11.1. Job Control Commands..............................................................................................................151 Chapter 12. External Filters, Programs and Commands...........................................................................155 12.1. Basic Commands........................................................................................................................155 12.2. Complex Commands...................................................................................................................159 12.3. Time / Date Commands..............................................................................................................167 12.4. Text Processing Commands........................................................................................................170 12.5. File and Archiving Commands...................................................................................................186 12.6. Communications Commands......................................................................................................201 12.7. Terminal Control Commands.....................................................................................................205 12.8. Math Commands.........................................................................................................................206 12.9. Miscellaneous Commands..........................................................................................................215 Chapter 13. System and Administrative Commands..................................................................................225 Chapter 14. Command Substitution.............................................................................................................248 Chapter 15. Arithmetic Expansion................................................................................................................253 Chapter 16. I/O Redirection...........................................................................................................................254 16.1. Using exec...................................................................................................................................256 16.2. Redirecting Code Blocks............................................................................................................259 16.3. Applications................................................................................................................................263