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] 29 September 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. Revision 1.6 29 September 2002 Revised by: mc 'POMEGRANATE' release: some bugfixes, more material, one more script 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.......................................................................................................................5 Part 2. Basics.......................................................................................................................................................7 Chapter 3. Special Characters...........................................................................................................................8 Chapter 4. Introduction to Variables and Parameters..................................................................................24 4.1. Variable Substitution......................................................................................................................24 4.2. Variable Assignment.......................................................................................................................26 4.3. Bash Variables Are Untyped..........................................................................................................27 4.4. Special Variable Types...................................................................................................................28 Chapter 5. Quoting...........................................................................................................................................33 Chapter 6. Exit and Exit Status.......................................................................................................................39 Chapter 7. Tests................................................................................................................................................41 7.1. Test Constructs...............................................................................................................................41 7.2. File test operators............................................................................................................................47 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.......................................................................................................................78 9.2.1. Manipulating strings using awk............................................................................................82 9.2.2. Further Discussion.................................................................................................................83 9.3. Parameter Substitution....................................................................................................................83 9.4. Typing variables: declare or typeset...............................................................................................91 9.5. Indirect References to Variables.....................................................................................................93 9.6. $RANDOM: generate random integer............................................................................................94 9.7. The Double Parentheses Construct.................................................................................................99 Chapter 10. Loops and Branches..................................................................................................................101 10.1. Loops..........................................................................................................................................101 10.2. Nested Loops..............................................................................................................................111 10.3. Loop Control...............................................................................................................................112 i Advanced Bash−Scripting Guide Table of Contents Chapter 10. Loops and Branches 10.4. Testing and Branching................................................................................................................114 Chapter 11. Internal Commands and Builtins.............................................................................................122 11.1. Job Control Commands..............................................................................................................141 Chapter 12. External Filters, Programs and Commands...........................................................................145 12.1. Basic Commands........................................................................................................................145 12.2. Complex Commands...................................................................................................................148 12.3. Time / Date Commands..............................................................................................................155 12.4. Text Processing Commands........................................................................................................157 12.5. File and Archiving Commands...................................................................................................173 12.6. Communications Commands......................................................................................................187 12.7. Terminal Control Commands.....................................................................................................191 12.8. Math Commands.........................................................................................................................192 12.9. Miscellaneous Commands..........................................................................................................201 Chapter 13. System and Administrative Commands..................................................................................211