• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Advanced Bash−Scripting Guide
An in−depth exploration of the art of shell scripting
Mendel Cooper
<thegrendel@theriver.com>
4.2.0115 December 2006
Revision History
Revision 4.018 Jun 2006Revised by: mc'WINTERBERRY' release: Major Update.Revision 4.108 Oct 2006Revised by: mc'WAXBERRY' release: Minor Update.Revision 4.210 Dec 2006Revised by: mc'SPARKLEBERRY' release: Important Update.This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward anintermediate/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 shellscripting techniques. The exercises and heavily−commented examples invite active reader participation, underthe premise that
the only way to really learn scripting is to write scripts
.This book is suitable for classroom use as a general introduction toprogramming concepts. The latest update of this document, as an archived, bzip2−ed "tarball" including both the SGML source andrendered HTML, may be downloaded from the author's home site. A pdf version is also available. See thechange log for a revision history.
Dedication
For Anita, the source of all the magic
 
Table of Contents
Chapter 1. Why Shell Programming?...............................................................................................................1Chapter 2. Starting Off With a Sha−Bang.......................................................................................................3
2.1. Invoking the script............................................................................................................................62.2. Preliminary Exercises.......................................................................................................................6
Part 2. Basics.......................................................................................................................................................7Chapter 3. Special Characters...........................................................................................................................8Chapter 4. Introduction to Variables and Parameters..................................................................................26
4.1. Variable Substitution......................................................................................................................264.2. Variable Assignment.......................................................................................................................284.3. Bash Variables Are Untyped..........................................................................................................304.4. Special Variable Types...................................................................................................................31
Chapter 5. Quoting...........................................................................................................................................36
5.1. Quoting Variables...........................................................................................................................365.2. Escaping..........................................................................................................................................38
Chapter 6. Exit and Exit Status.......................................................................................................................43Chapter 7. Tests................................................................................................................................................45
7.1. Test Constructs...............................................................................................................................457.2. File test operators............................................................................................................................517.3. Other Comparison Operators..........................................................................................................547.4. Nested if/then Condition Tests.......................................................................................................597.5. Testing Your Knowledgeof Tests..................................................................................................59
Chapter 8. Operations and Related Topics....................................................................................................61
8.1. Operators.........................................................................................................................................618.2. Numerical Constants.......................................................................................................................67
Part 3. Beyond the Basics.................................................................................................................................69Chapter 9. Variables Revisited........................................................................................................................70
9.1. Internal Variables............................................................................................................................709.2. Manipulating Strings.......................................................................................................................879.2.1. Manipulating strings using awk............................................................................................939.2.2. Further Discussion.................................................................................................................949.3. Parameter Substitution....................................................................................................................949.4. Typing variables: declare or typeset.............................................................................................1039.5. Indirect References to Variables...................................................................................................1059.6. $RANDOM: generate random integer..........................................................................................1089.7. The Double Parentheses Construct...............................................................................................117
Advanced Bash−Scripting Guidei
 
Table of Contents
Chapter 10. Loops andBranches..................................................................................................................119
10.1. Loops..........................................................................................................................................11910.2. Nested Loops..............................................................................................................................13010.3. Loop Control...............................................................................................................................13110.4. Testing and Branching................................................................................................................134
Chapter 11. Command Substitution.............................................................................................................142Chapter 12. Arithmetic Expansion................................................................................................................148Chapter 13. Recess Time................................................................................................................................149Part 4. Commands..........................................................................................................................................150Chapter 14. Internal Commands and Builtins.............................................................................................158
14.1. Job Control Commands..............................................................................................................184
Chapter 15. External Filters, Programs and Commands...........................................................................189
15.1. Basic Commands........................................................................................................................18915.2. Complex Commands...................................................................................................................19415.3. Time / Date Commands..............................................................................................................20415.4. Text Processing Commands........................................................................................................20715.5. File and Archiving Commands...................................................................................................22715.6. Communications Commands......................................................................................................24315.7. Terminal Control Commands.....................................................................................................25715.8. Math Commands.........................................................................................................................25815.9. Miscellaneous Commands..........................................................................................................267
Chapter 16. System and AdministrativeCommands..................................................................................280
16.1. Analyzing a System Script..........................................................................................................308
Part 5. Advanced Topics.................................................................................................................................310Chapter 17. Regular Expressions..................................................................................................................312
17.1. ABrief Introduction to Regular Expressions..............................................................................31217.2. Globbing.....................................................................................................................................315
Chapter 18. Here Documents.........................................................................................................................317
18.1. Here Strings................................................................................................................................326
Chapter 19. I/O Redirection...........................................................................................................................329
19.1. Using exec...................................................................................................................................33219.2. Redirecting Code Blocks............................................................................................................33519.3. Applications................................................................................................................................340
Chapter 20. Subshells.....................................................................................................................................342
Advanced Bash−Scripting Guideii
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...