• Embed Doc
  • Readcast
  • Collections
  • 1
    CommentGo Back
Download
 
Advanced Bash-Scripting Guide
An in-depth exploration of the art of shell scripting
Mendel Cooper
<thegrendel@theriver.com>
5.110 November 2007
Revision History
Revision 4.329 Apr 2007Revised by: mc'INKBERRY' release: Minor Update.Revision 5.024 Jun 2007Revised by: mc'SERVICEBERRY' release: Major Update.Revision 5.110 Nov 2007Revised by: mc'LINGONBERRY' release: Minor 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 ( pdf mirrorsite). See the change 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..................................................................................27
4.1. Variable Substitution......................................................................................................................274.2. Variable Assignment.......................................................................................................................304.3. Bash Variables Are Untyped..........................................................................................................314.4. Special Variable Types...................................................................................................................32
Chapter 5. Quoting...........................................................................................................................................37
5.1. Quoting Variables...........................................................................................................................375.2. Escaping..........................................................................................................................................39
Chapter 6. Exit and Exit Status.......................................................................................................................44Chapter 7. Tests................................................................................................................................................46
7.1. Test Constructs...............................................................................................................................467.2. File test operators............................................................................................................................527.3. Other Comparison Operators..........................................................................................................557.4. Nested if/then Condition Tests.......................................................................................................607.5. Testing Your Knowledgeof Tests..................................................................................................60
Chapter 8. Operations and Related Topics....................................................................................................62
8.1. Operators.........................................................................................................................................628.2. Numerical Constants.......................................................................................................................68
Part 3. Beyond the Basics.................................................................................................................................70Chapter 9. Variables Revisited........................................................................................................................71
9.1. Internal Variables............................................................................................................................719.2. Manipulating Strings.......................................................................................................................889.2.1. Manipulating strings using awk............................................................................................959.2.2. Further Discussion.................................................................................................................969.3. Parameter Substitution....................................................................................................................969.4. Typing variables: declare or typeset.............................................................................................1059.5. Indirect References.......................................................................................................................1079.6. $RANDOM: generate random integer..........................................................................................1109.7. The Double Parentheses Construct...............................................................................................121
Advanced Bash-Scripting Guidei
 
Table of Contents
Chapter 10. Loops andBranches..................................................................................................................123
10.1. Loops..........................................................................................................................................12310.2. Nested Loops..............................................................................................................................13510.3. Loop Control...............................................................................................................................13610.4. Testing and Branching................................................................................................................139
Chapter 11. Command Substitution.............................................................................................................147Chapter 12. Arithmetic Expansion................................................................................................................153Chapter 13. Recess Time................................................................................................................................154Part 4. Commands..........................................................................................................................................155Chapter 14. Internal Commands and Builtins.............................................................................................163
14.1. Job Control Commands..............................................................................................................190
Chapter 15. External Filters, Programs and Commands...........................................................................195
15.1. Basic Commands........................................................................................................................19515.2. Complex Commands...................................................................................................................20015.3. Time / Date Commands..............................................................................................................21015.4. Text Processing Commands........................................................................................................21315.5. File and Archiving Commands...................................................................................................23415.6. Communications Commands......................................................................................................25115.7. Terminal Control Commands.....................................................................................................26515.8. Math Commands.........................................................................................................................26615.9. Miscellaneous Commands..........................................................................................................275
Chapter 16. System and AdministrativeCommands..................................................................................289
16.1. Analyzing a System Script..........................................................................................................318
Part 5. Advanced Topics.................................................................................................................................320Chapter 17. Regular Expressions..................................................................................................................322
17.1. ABrief Introduction to Regular Expressions..............................................................................32217.2. Globbing.....................................................................................................................................325
Chapter 18. Here Documents.........................................................................................................................327
18.1. Here Strings................................................................................................................................337
Chapter 19. I/O Redirection...........................................................................................................................340
19.1. Using exec...................................................................................................................................34319.2. Redirecting Code Blocks............................................................................................................34619.3. Applications................................................................................................................................351
Chapter 20. Subshells.....................................................................................................................................353
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: ...