Advanced Bash-Scripting Guide
Total Page:16
File Type:pdf, Size:1020Kb
Advanced Bash−Scripting Guide An in−depth exploration of the gentle art of shell scripting Mendel Cooper Brindle−Phlogiston Associates [email protected] 13 July 2002 Revision History Revision 0.1 14 June 2000 Revised by: mc Initial release. Revision 0.2 30 October 2000 Revised by: mc Bugs fixed, plus much additional material and more example scripts. Revision 0.3 12 February 2001 Revised by: mc Another major update. Revision 0.4 08 July 2001 Revised by: mc More bugfixes, much more material, more scripts − a complete revision and expansion of the book. Revision 0.5 03 September 2001 Revised by: mc Major update. Bugfixes, material added, chapters and sections reorganized. Revision 1.0 14 October 2001 Revised by: mc Bugfixes, reorganization, material added. Stable release. Revision 1.1 06 January 2002 Revised by: mc Bugfixes, material and scripts added. Revision 1.2 31 March 2002 Revised by: mc Bugfixes, material and scripts added. Revision 1.3 02 June 2002 Revised by: mc 'TANGERINE' release: A few bugfixes, much more material and scripts added. Revision 1.4 16 June 2002 Revised by: mc 'MANGO' release: Quite a number of typos fixed, more material and scripts added. Revision 1.5 13 July 2002 Revised by: mc 'PAPAYA' release: A few bugfixes, much more material and scripts added. 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. 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.......................................................................................................................6 Part 2. Basics............................................................................................................................................6 Chapter 3. Exit and Exit Status.........................................................................................................................7 Chapter 4. Special Characters...........................................................................................................................9 Chapter 5. Introduction to Variables and Parameters..................................................................................27 5.1. Variable Substitution......................................................................................................................27 5.2. Variable Assignment.......................................................................................................................29 5.3. Bash Variables Are Untyped..........................................................................................................31 5.4. Special Variable Types...................................................................................................................32 Chapter 6. Quoting...........................................................................................................................................36 Chapter 7. Tests................................................................................................................................................42 7.1. Test Constructs...............................................................................................................................42 7.2. File test operators............................................................................................................................48 7.3. Comparison operators (binary).......................................................................................................51 7.4. Nested if/then Condition Tests.......................................................................................................56 7.5. Testing Your Knowledge of Tests..................................................................................................57 Chapter 8. Operations and Related Topics....................................................................................................58 8.1. Operators.........................................................................................................................................58 8.2. Numerical Constants.......................................................................................................................65 Part 3. Beyond the Basics......................................................................................................................66 Chapter 9. Variables Revisited........................................................................................................................67 9.1. Internal Variables............................................................................................................................67 9.2. Manipulating Strings.......................................................................................................................83 9.2.1. Manipulating strings using awk......................................................................................87 9.2.2. Further Discussion..........................................................................................................88 9.3. Parameter Substitution....................................................................................................................88 9.4. Typing variables: declare or typeset..............................................................................................97 9.5. Indirect References to Variables.....................................................................................................98 9.6. $RANDOM: generate random integer..........................................................................................100 9.7. The Double Parentheses Construct...............................................................................................105 Chapter 10. Loops and Branches..................................................................................................................107 10.1. Loops..........................................................................................................................................107 10.2. Nested Loops..............................................................................................................................117 10.3. Loop Control...............................................................................................................................118 10.4. Testing and Branching................................................................................................................120 i Advanced Bash−Scripting Guide Table of Contents Chapter 11. Internal Commands and Builtins.............................................................................................128 11.1. Job Control Commands..............................................................................................................146 Chapter 12. External Filters, Programs and Commands...........................................................................151 12.1. Basic Commands........................................................................................................................151 12.2. Complex Commands...................................................................................................................154 12.3. Time / Date Commands..............................................................................................................161 12.4. Text Processing Commands........................................................................................................164 12.5. File and Archiving Commands...................................................................................................181 12.6. Communications Commands......................................................................................................197 12.7. Terminal Control Commands.....................................................................................................202 12.8. Math Commands.........................................................................................................................203 12.9. Miscellaneous Commands..........................................................................................................212 Chapter 13. System and Administrative Commands..................................................................................222 Chapter 14. Command Substitution.............................................................................................................246