- •Table of Contents
- •Chapter 1. Why Shell Programming?
- •2.1. Invoking the script
- •2.2. Preliminary Exercises
- •Part 2. Basics
- •Chapter 3. Exit and Exit Status
- •Chapter 4. Special Characters
- •Chapter 5. Introduction to Variables and Parameters
- •5.1. Variable Substitution
- •5.2. Variable Assignment
- •5.3. Bash Variables Are Untyped
- •5.4. Special Variable Types
- •Chapter 6. Quoting
- •Chapter 7. Tests
- •7.1. Test Constructs
- •7.2. File test operators
- •7.3. Comparison operators (binary)
- •7.4. Nested if/then Condition Tests
- •7.5. Testing Your Knowledge of Tests
- •Chapter 8. Operations and Related Topics
- •8.1. Operators
- •8.2. Numerical Constants
- •Part 3. Beyond the Basics
- •Chapter 9. Variables Revisited
- •9.1. Internal Variables
- •9.2. Manipulating Strings
- •9.2.1. Manipulating strings using awk
- •9.2.2. Further Discussion
- •9.3. Parameter Substitution
- •9.4. Typing variables: declare or typeset
- •9.5. Indirect References to Variables
- •9.6. $RANDOM: generate random integer
- •9.7. The Double Parentheses Construct
- •Chapter 10. Loops and Branches
- •10.1. Loops
- •10.2. Nested Loops
- •10.3. Loop Control
- •10.4. Testing and Branching
- •Chapter 11. Internal Commands and Builtins
- •11.1. Job Control Commands
- •Chapter 12. External Filters, Programs and Commands
- •12.1. Basic Commands
- •12.2. Complex Commands
- •12.3. Time / Date Commands
- •12.4. Text Processing Commands
- •12.5. File and Archiving Commands
- •12.6. Communications Commands
- •12.7. Terminal Control Commands
- •12.8. Math Commands
- •12.9. Miscellaneous Commands
- •Chapter 13. System and Administrative Commands
- •Chapter 14. Command Substitution
- •Chapter 15. Arithmetic Expansion
- •Chapter 16. I/O Redirection
- •16.1. Using exec
- •16.2. Redirecting Code Blocks
- •16.3. Applications
- •Chapter 17. Here Documents
- •Chapter 18. Recess Time
- •Part 4. Advanced Topics
- •Chapter 19. Regular Expressions
- •19.1. A Brief Introduction to Regular Expressions
- •19.2. Globbing
- •Chapter 20. Subshells
- •Chapter 21. Restricted Shells
- •Chapter 22. Process Substitution
- •Chapter 23. Functions
- •23.1. Complex Functions and Function Complexities
- •23.2. Local Variables
- •23.2.1. Local variables make recursion possible.
- •Chapter 24. Aliases
- •Chapter 25. List Constructs
- •Chapter 26. Arrays
- •Chapter 27. Files
- •Chapter 28. /dev and /proc
- •28.2. /proc
- •Chapter 29. Of Zeros and Nulls
- •Chapter 30. Debugging
- •Chapter 31. Options
- •Chapter 32. Gotchas
- •Chapter 33. Scripting With Style
- •33.1. Unofficial Shell Scripting Stylesheet
- •Chapter 34. Miscellany
- •34.2. Shell Wrappers
- •34.3. Tests and Comparisons: Alternatives
- •34.4. Optimizations
- •34.5. Assorted Tips
- •34.6. Oddities
- •34.7. Portability Issues
- •34.8. Shell Scripting Under Windows
- •Chapter 35. Bash, version 2
- •Chapter 36. Endnotes
- •36.1. Author's Note
- •36.2. About the Author
- •36.3. Tools Used to Produce This Book
- •36.3.1. Hardware
- •36.3.2. Software and Printware
- •36.4. Credits
- •Bibliography
- •Appendix A. Contributed Scripts
- •Appendix C. Exit Codes With Special Meanings
- •Appendix D. A Detailed Introduction to I/O and I/O Redirection
- •Appendix E. Localization
- •Appendix F. History Commands
- •Appendix G. A Sample .bashrc File
- •Appendix H. Converting DOS Batch Files to Shell Scripts
- •Appendix I. Exercises
- •Appendix J. Copyright
Advanced Bash−Scripting Guide
iii.Norman Walsh's DSSSL stylesheets.
iv.DocBook, The Definitive Guide, by Norman Walsh and Leonard Muellner (O'Reilly, ISBN 1−56592−580−7). This is the standard reference for anyone attempting to write a document in Docbook SGML format.
36.4.Credits
Community participation made this project possible. The author gratefully acknowledges that writing this book would have been an impossible task without help and feedback from all you people out there.
Philippe Martin translated this document into DocBook/SGML. While not on the job at a small French company as a software developer, he enjoys working on GNU/Linux documentation and software, reading literature, playing music, and for his peace of mind making merry with friends. You may run across him somewhere in France or in the Basque Country, or email him at feloy@free.fr.
Philippe Martin also pointed out that positional parameters past $9 are possible using {bracket} notation, see Example 5−5.
Stephane Chazelas sent a long list of corrections, additions, and example scripts. More than a contributor, he has, in effect, taken on the role of editor for this document. Merci beaucoup !
I would like to especially thank Patrick Callahan, Mike Novak, and Pal Domokos for catching bugs, pointing out ambiguities, and for suggesting clarifications and changes. Their lively discussion of shell scripting and general documentation issues inspired me to try to make this document more readable.
I'm grateful to Jim Van Zandt for pointing out errors and omissions in version 0.2 of this document. He also contributed an instructive example script.
Many thanks to Jordi Sanfeliu for giving permission to use his fine tree script (Example A−12).
Kudos to Noah Friedman for permission to use his string function script (Example A−13).
Emmanuel Rouat suggested corrections and additions on command substitution and aliases. He also contributed a very nice sample .bashrc file (Appendix G).
Heiner Steven kindly gave permission to use his base conversion script, Example 12−29. He also made a number of corrections and many helpful suggestions. Special thanks.
Florian Wisser enlightened me on some of the fine points of testing strings (see Example 7−5), and on other matters.
Oleg Philon sent suggestions concerning cut and pidof.
Marc−Jano Knopp sent corrections on DOS batch files.
Hyun Jin Cha found several typos in the document in the process of doing a Korean translation. Thanks for pointing these out.
36.4. Credits |
314 |
Advanced Bash−Scripting Guide
Others making helpful suggestions and pointing out errors were Gabor Kiss, Leopold Toetsch, Peter Tillier, Nick Drage (script ideas!), and David Lawyer (himself an author of 4 HOWTOs).
My gratitude to Chet Ramey and Brian Fox for writing Bash, an elegant and powerful scripting tool.
Thanks most of all to my wife, Anita, for her encouragement and emotional support.
Bibliography
Dale Dougherty and Arnold Robbins, Sed and Awk, 2nd edition, O'Reilly and Associates, 1997, 1−156592−225−5.
To unfold the full power of shell scripting, you need at least a passing familiarity with sed and awk. This is the standard tutorial. It includes an excellent introduction to "regular expressions". Read this book.
*
Aeleen Frisch, Essential System Administration, 2nd edition, O'Reilly and Associates, 1995, 1−56592−127−5.
This excellent sys admin manual has a decent introduction to shell scripting for sys administrators and does a nice job of explaining the startup and initialization scripts. The book is long overdue for a third edition (are you listening, Tim O'Reilly?).
*
Stephen Kochan and Patrick Woods, Unix Shell Programming, Hayden, 1990, 067248448X.
The standard reference, though a bit dated by now.
*
Neil Matthew and Richard Stones, Beginning Linux Programming, Wrox Press, 1996, 1874416680.
Good in−depth coverage of various programming languages available for Linux, including a fairly strong chapter on shell scripting.
*
Herbert Mayer, Advanced C Programming on the IBM PC, Windcrest Books, 1989, 0830693637.
Excellent coverage of algorithms and general programming practices.
*
Bibliography |
315 |
Advanced Bash−Scripting Guide
David Medinets, Unix Shell Programming Tools, McGraw−Hill, 1999, 0070397333.
Good info on shell scripting, with examples, and a short intro to Tcl and Perl.
*
Cameron Newham and Bill Rosenblatt, Learning the Bash Shell, 2nd edition, O'Reilly and Associates, 1998, 1−56592−347−2.
This is a valiant effort at a decent shell primer, but somewhat deficient in coverage on programming topics and lacking sufficient examples.
*
Anatole Olczak, Bourne Shell Quick Reference Guide, ASP, Inc., 1991, 093573922X.
A very handy pocket reference, despite lacking coverage of Bash−specific features.
*
Jerry Peek, Tim O'Reilly, and Mike Loukides, Unix Power Tools, 2nd edition, O'Reilly and Associates, Random House, 1997, 1−56592−260−3.
Contains a couple of sections of very informative in−depth articles on shell programming, but falls short of being a tutorial. It reproduces much of the regular expressions tutorial from the Dougherty and Robbins book, above.
*
Arnold Robbins, Bash Reference Card, SSC, 1998, 1−58731−010−5.
Excellent Bash pocket reference (don't leave home without it). A bargain at $4.95, but also available for free download on−line in pdf format.
*
Arnold Robbins, Effective Awk Programming, Free Software Foundation / O'Reilly and Associates, 2000, 1−882114−26−4.
The absolute best awk tutorial and reference. The free electronic version of this book is part of the
awk documentation, and printed copies of the latest version are available from O'Reilly and Associates.
This book has served as an inspiration for the author of this document.
*
Bibliography |
316 |
Advanced Bash−Scripting Guide
Bill Rosenblatt, Learning the Korn Shell, O'Reilly and Associates, 1993, 1−56592−054−6.
This well−written book contains some excellent pointers on shell scripting.
*
Paul Sheer, LINUX: Rute User's Tutorial and Exposition, 1st edition, , 2002, 0−13−033351−4.
Very detailed and readable introduction to Linux system administration.
The book is available in print, or on−line.
*
Ellen Siever and and the Staff of O'Reilly and Associates, Linux in a Nutshell, 2nd edition, O'Reilly and Associates, 1999, 1−56592−585−8.
The all−around best Linux command reference, even has a Bash section.
*
The UNIX CD Bookshelf, 2nd edition, O'Reilly and Associates, 2000, 1−56592−815−6.
An array of six UNIX books on CD ROM, including UNIX Power Tools, Sed and Awk, and Learning the Korn Shell. A complete set of all the UNIX references and tutorials you would ever need at about $70. Buy this one, even if it means going into debt and not paying the rent.
Unfortunately, out of print at present.
*
The O'Reilly books on Perl. (Actually, any O'Reilly books.)
−−−
Ben Okopnik's well−written introductory Bash scripting articles in issues 53, 54, 55, 57, and 59 of the Linux Gazette , and his explanation of "The Deep, Dark Secrets of Bash" in issue 56.
Chet Ramey's bash − The GNU Shell , a two−part series published in issues 3 and 4 of the Linux Journal, July−August 1994.
Mike G's Bash−Programming−Intro HOWTO.
Bibliography |
317 |
Advanced Bash−Scripting Guide
Richard's UNIX Scripting Universe.
Chet Ramey's Bash F.A.Q.
Example shell scripts at Lucc's Shell Scripts .
Example shell scripts at SHELLdorado .
Example shell scripts at Noah Friedman's script site.
Example shell scripts at SourceForge Snippet Library − shell scrips.
Giles Orr's Bash−Prompt HOWTO.
The sed F.A.Q.
Carlos Duarte's instructive "Do It With Sed" tutorial.
The GNU gawk reference manual (gawk is the extended GNU version of awk available on Linux and BSD systems).
Trent Fisher's groff tutorial.
Mark Komarinski's Printing−Usage HOWTO.
There is some nice material on I/O redirection in chapter 10 of the textutils documentation at the University of Alberta site.
Rick Hohensee has written the osimpa i386 assembler entirely as Bash scripts.
−−−
The excellent "Bash Reference Manual", by Chet Ramey and Brian Fox, distributed as part of the "bash−2−doc" package (available as an rpm). See especially the instructive example scripts in this package.
The comp.os.unix.shell newsgroup.
Bibliography |
318 |