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.2 31 October 2003 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. Revision 1.7 05 January 2003 Revised by: mc 'COCONUT' release: a couple of bugfixes, more material, one more script. Revision 1.8 10 May 2003 Revised by: mc 'BREADFRUIT' release: a number of bugfixes, more scripts and material. Revision 1.9 21 June 2003 Revised by: mc 'PERSIMMON' release: bugfixes and more material. Revision 2.0 24 August 2003 Revised by: mc 'GOOSEBERRY' release: Major update. Revision 2.1 14 September 2003 Revised by: mc 'HUCKLEBERRY' release: bugfixes and more material. Revision 2.2 31 October 2003 Revised by: mc 'CRANBERRY' 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. 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...............................................................................................105 Chapter 10. Loops and Branches..................................................................................................................107 10.1. Loops..........................................................................................................................................107 10.2. Nested Loops..............................................................................................................................117 10.3. Loop Control...............................................................................................................................118 i Advanced Bash−Scripting Guide Table of Contents Chapter 10. Loops and Branches 10.4. Testing and Branching................................................................................................................121 Chapter 11. Internal Commands and Builtins.............................................................................................129 11.1. Job Control Commands..............................................................................................................150 Chapter 12. External Filters, Programs and Commands...........................................................................154 12.1. Basic Commands........................................................................................................................154 12.2. Complex Commands...................................................................................................................157 12.3. Time / Date Commands..............................................................................................................164 12.4. Text Processing Commands........................................................................................................166 12.5. File and Archiving Commands...................................................................................................183