ebook img

Advanced bash-scripting guide PDF

978 Pages·2004·3.002 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Advanced bash-scripting guide

Advanced Bash-Scripting Guide Advanced Bash-Scripting Guide Table of Contents Advanced Bash-Scripting Guide.......................................................................................................................1 An in-depth exploration of the art of shell scripting................................................................................1 Mendel Cooper..................................................................................................................................1 Dedication............................................................................................................................................................3 Part 1. Introduction..........................................................................................................................................15 Chapter 1. Shell Programming!.......................................................................................................................17 Notes......................................................................................................................................................18 Chapter 2. Starting Off With a Sha-Bang......................................................................................................21 2.1. Invoking the script.....................................................................................................................................25 Notes......................................................................................................................................................25 2.2. Preliminary Exercises................................................................................................................................27 Part 2. Basics.....................................................................................................................................................29 Chapter 3. Special Characters.........................................................................................................................31 Notes......................................................................................................................................................50 Chapter 4. Introduction to Variables and Parameters..................................................................................51 4.1. Variable Substitution.................................................................................................................................53 Notes......................................................................................................................................................55 4.2. Variable Assignment..................................................................................................................................57 4.3. Bash Variables Are Untyped.....................................................................................................................59 4.4. Special Variable Types..............................................................................................................................61 Notes......................................................................................................................................................65 Chapter 5. Quoting...........................................................................................................................................67 5.1. Quoting Variables......................................................................................................................................69 Notes......................................................................................................................................................70 5.2. Escaping......................................................................................................................................................73 Chapter 6. Exit and Exit Status.......................................................................................................................79 Notes......................................................................................................................................................81 Chapter 7. Tests................................................................................................................................................83 i Advanced Bash-Scripting Guide Table of Contents 7.1. Test Constructs...........................................................................................................................................85 Notes......................................................................................................................................................92 7.2. File test operators.......................................................................................................................................93 Notes......................................................................................................................................................95 7.3. Other Comparison Operators...................................................................................................................97 Notes....................................................................................................................................................102 7.4. Nestedif/then Condition Tests................................................................................................................103 7.5. Testing Your Knowledge of Tests...........................................................................................................105 Chapter 8. Operations and Related Topics..................................................................................................107 8.1. Operators..................................................................................................................................................109 Notes....................................................................................................................................................115 8.2. Numerical Constants...............................................................................................................................117 8.3. The Double-Parentheses Construct........................................................................................................119 8.4. Operator Precedence...............................................................................................................................121 Notes....................................................................................................................................................123 Part 3. Beyond the Basics...............................................................................................................................125 Chapter 9. Another Look at Variables.........................................................................................................127 9.1. Internal Variables....................................................................................................................................129 Notes....................................................................................................................................................147 9.2. Typing variables: declare or typeset......................................................................................................149 9.2.1. Another use for declare..............................................................................................................151 Notes..............................................................................................................................................151 9.3. $RANDOM: generate random integer...................................................................................................153 Notes....................................................................................................................................................163 Chapter 10. Manipulating Variables.............................................................................................................165 10.1. Manipulating Strings.............................................................................................................................167 10.1.1. Manipulating strings using awk...............................................................................................174 10.1.2. Further Reference.....................................................................................................................175 Notes..............................................................................................................................................175 ii Advanced Bash-Scripting Guide Table of Contents 10.2. Parameter Substitution.........................................................................................................................177 Notes....................................................................................................................................................186 Chapter 11. Loops and Branches..................................................................................................................187 11.1. Loops.......................................................................................................................................................189 Notes....................................................................................................................................................202 11.2. Nested Loops...........................................................................................................................................203 11.3. Loop Control..........................................................................................................................................205 Notes....................................................................................................................................................208 11.4. Testing and Branching..........................................................................................................................209 Chapter 12. Command Substitution.............................................................................................................217 Notes....................................................................................................................................................222 Chapter 13. Arithmetic Expansion................................................................................................................223 Chapter 14. Recess Time................................................................................................................................225 Part 4. Commands..........................................................................................................................................227 Chapter 15. Internal Commands and Builtins.............................................................................................235 15.1. Job Control Commands........................................................................................................................263 Notes....................................................................................................................................................266 Chapter 16. External Filters, Programs and Commands...........................................................................269 16.1. Basic Commands....................................................................................................................................271 Notes....................................................................................................................................................276 16.2. Complex Commands..............................................................................................................................277 Notes....................................................................................................................................................286 16.3. Time / Date Commands.........................................................................................................................287 16.4. Text Processing Commands..................................................................................................................291 Notes....................................................................................................................................................312 16.5. File and Archiving Commands.............................................................................................................313 Notes....................................................................................................................................................330 16.6. Communications Commands................................................................................................................331 Notes....................................................................................................................................................344 iii Advanced Bash-Scripting Guide Table of Contents 16.7. Terminal Control Commands...............................................................................................................345 16.8. Math Commands....................................................................................................................................347 16.9. Miscellaneous Commands.....................................................................................................................359 Notes....................................................................................................................................................371 Chapter 17. System and Administrative Commands..................................................................................373 17.1. Analyzing a System Script.....................................................................................................................401 Notes....................................................................................................................................................402 Part 5. Advanced Topics.................................................................................................................................403 Chapter 18. Regular Expressions..................................................................................................................405 18.1. A Brief Introduction to Regular Expressions......................................................................................407 Notes....................................................................................................................................................410 18.2. Globbing..................................................................................................................................................413 Notes....................................................................................................................................................414 Chapter 19. Here Documents.........................................................................................................................415 19.1. Here Strings............................................................................................................................................427 Chapter 20. I/O Redirection...........................................................................................................................431 20.1. Using exec...............................................................................................................................................435 Notes....................................................................................................................................................438 20.2. Redirecting Code Blocks.......................................................................................................................439 20.3. Applications............................................................................................................................................445 Chapter 21. Subshells.....................................................................................................................................447 Notes....................................................................................................................................................451 Chapter 22. Restricted Shells.........................................................................................................................453 Chapter 23. Process Substitution...................................................................................................................455 Notes....................................................................................................................................................458 Chapter 24. Functions....................................................................................................................................459 24.1. Complex Functions and Function Complexities.................................................................................463 Notes....................................................................................................................................................473 iv Advanced Bash-Scripting Guide Table of Contents 24.2. Local Variables.......................................................................................................................................475 24.2.1. Local variables and recursion..................................................................................................476 Notes..............................................................................................................................................478 24.3. Recursion Without Local Variables.....................................................................................................479 Chapter 25. Aliases.........................................................................................................................................483 Notes....................................................................................................................................................485 Chapter 26. List Constructs...........................................................................................................................487 Chapter 27. Arrays.........................................................................................................................................491 Chapter 28. Indirect References....................................................................................................................519 Chapter 29./dev and/proc.............................................................................................................................523 29.1./dev..........................................................................................................................................................525 Notes....................................................................................................................................................527 29.2./proc.........................................................................................................................................................529 Notes....................................................................................................................................................534 Chapter 30. Of Zeros and Nulls.....................................................................................................................535 Chapter 31. Debugging...................................................................................................................................539 Notes....................................................................................................................................................548 Chapter 32. Options........................................................................................................................................549 Chapter 33. Gotchas.......................................................................................................................................553 Notes....................................................................................................................................................560 Chapter 34. Scripting With Style..................................................................................................................561 34.1. Unofficial Shell Scripting Stylesheet....................................................................................................563 Notes....................................................................................................................................................565 Chapter 35. Miscellany...................................................................................................................................567 35.1. Interactive and non-interactive shells and scripts..............................................................................569 35.2. Shell Wrappers.......................................................................................................................................571 Notes....................................................................................................................................................575 35.3. Tests and Comparisons: Alternatives..................................................................................................577 v Advanced Bash-Scripting Guide Table of Contents 35.4. A script calling itself (recursion)..........................................................................................................579 35.5. "Colorizing" Scripts..............................................................................................................................583 Notes....................................................................................................................................................595 35.6. Optimizations.........................................................................................................................................597 Notes....................................................................................................................................................597 35.7. Assorted Tips..........................................................................................................................................599 35.7.1. Ideas for more powerful scripts...............................................................................................599 35.7.2. Widgets....................................................................................................................................609 35.8. Security Issues........................................................................................................................................613 35.8.1. Infected Shell Scripts...............................................................................................................613 35.8.2. Hiding Shell Script Source......................................................................................................613 35.8.3. Writing Secure Shell Scripts....................................................................................................613 Notes..............................................................................................................................................613 35.9. Portability Issues....................................................................................................................................615 35.9.1. A Test Suite.............................................................................................................................615 Notes..............................................................................................................................................616 35.10. Shell Scripting Under Windows.........................................................................................................617 Chapter 36. Bash, versions 2, 3, and 4..........................................................................................................619 36.1. Bash, version 2........................................................................................................................................621 36.2. Bash, version 3........................................................................................................................................627 36.2.1. Bash, version 3.1......................................................................................................................629 36.2.2. Bash, version 3.2......................................................................................................................629 36.3. Bash, version 4........................................................................................................................................631 Notes....................................................................................................................................................637 Chapter 37. Endnotes.....................................................................................................................................639 37.1. Author's Note.........................................................................................................................................641 Notes....................................................................................................................................................641 37.2. About the Author...................................................................................................................................643 Notes....................................................................................................................................................643 37.3. Where to Go For Help...........................................................................................................................645 Notes....................................................................................................................................................645 vi Advanced Bash-Scripting Guide Table of Contents 37.4. Tools Used to Produce This Book.........................................................................................................647 37.4.1. Hardware..................................................................................................................................647 37.4.2. Software and Printware............................................................................................................647 37.5. Credits.....................................................................................................................................................649 37.6. Disclaimer...............................................................................................................................................651 Bibliography....................................................................................................................................................653 Notes....................................................................................................................................................659 Appendix A. Contributed Scripts..................................................................................................................661 Appendix B. Reference Cards........................................................................................................................849 Appendix C. A Sed and Awk Micro-Primer................................................................................................855 C.1. Sed............................................................................................................................................................857 Notes....................................................................................................................................................859 C.2. Awk...........................................................................................................................................................861 Notes....................................................................................................................................................863 Appendix D. Exit Codes With Special Meanings.........................................................................................865 Notes....................................................................................................................................................865 Appendix E. A Detailed Introduction to I/O and I/O Redirection.............................................................867 Appendix F. Command-Line Options...........................................................................................................869 F.1. Standard Command-Line Options........................................................................................................871 F.2. Bash Command-Line Options................................................................................................................873 Appendix G. Important Files.........................................................................................................................875 Notes....................................................................................................................................................875 Appendix H. Important System Directories.................................................................................................877 Notes....................................................................................................................................................878 Appendix I. An Introduction to Programmable Completion.....................................................................879 Notes....................................................................................................................................................881 Appendix J. Localization................................................................................................................................883 Appendix K. History Commands..................................................................................................................887 vii Advanced Bash-Scripting Guide Table of Contents Appendix L. A Sample.bashrc File...............................................................................................................889 Appendix M. Converting DOS Batch Files to Shell Scripts........................................................................903 Notes....................................................................................................................................................906 Appendix N. Exercises....................................................................................................................................907 N.1. Analyzing Scripts....................................................................................................................................909 N.2. Writing Scripts........................................................................................................................................911 Notes....................................................................................................................................................919 Appendix O. Revision History.......................................................................................................................921 Appendix P. Download and Mirror Sites.....................................................................................................925 Appendix Q. To Do List..................................................................................................................................927 Appendix R. Copyright..................................................................................................................................929 Notes....................................................................................................................................................930 Appendix S. ASCII Table...............................................................................................................................933 Index.................................................................................................................................................................935 viii

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.