Transcription of Advanced Bash-Scripting Guide - Del Mar College
1 Advanced Bash-Scripting GuideAn in-depth exploration of the art of shell scriptingMendel March 2010 Revision HistoryRevision Mar 2009 Revised by: mc'THIMBLEBERRY' release: Major Sep 2009 Revised by: mc'BUFFALOBERRY' release: Minor Mar 2010 Revised by: mc'ROWANBERRY' releaseThis tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward anintermediate/ Advanced level of instruction .. all the while sneaking in little nuggets of UNIX wisdom andlore. It serves as a textbook, a manual for self-study, and a reference and source of knowledge on shellscripting techniques.
2 The exercises and heavily-commented examples invite active reader participation, underthe premise that the only way to really learn scripting is to write book is suitable for classroom use as a general introduction to programming Anita, the source of all the magicTable of ContentsChapter 1. Shell Programming!..1 Chapter 2. Starting Off With a Invoking the Preliminary 2. 3. Special 4. Introduction to Variables and Variable Variable bash Variables Are Special Variable 5. Quoting 6. Exit and Exit 7. Test File test Other Comparison Nestedif/then Condition Testing Your Knowledge of 8.
3 Operations and Related Numerical The Double-Parentheses Operator 3. Beyond the 9. Another Look at Internal Typing variables: declare or Another use for $RANDOM: generate random 10. Manipulating Manipulating Manipulating strings using Further Parameter Bash-Scripting GuideiTable of ContentsChapter 11. Loops and Nested Loop Testing and 12. Command 13. Arithmetic 14. Recess 4. 15. Internal Commands and Job Control 16. External Filters, Programs and Basic Complex Time / Date Text Processing File and Archiving Communications Terminal Control Math Miscellaneous 17.
4 System and Administrative Analyzing a System 5. Advanced 18. Regular A Brief Introduction to Regular 19. Here Here 20. I/O Using Redirecting Code 21. Bash-Scripting GuideiiTable of ContentsChapter 22. Restricted 23. Process 24. Complex Functions and Function Local Local variables and Recursion Without Local 25. 26. List 27. 28. Indirect 30. Of Zeros and 31. 32. 33. 34. scripting With Unofficial Shell scripting 35. Interactive and non-interactive shells and Shell Tests and Comparisons: A script calling itself (recursion).
5 "Colorizing" Assorted Ideas for more powerful Security Infected Shell Hiding Shell Script Writing Secure Shell Portability Bash-Scripting GuideiiiTable of ContentsChapter 35. A Test Shell scripting Under 36. bash , versions 2, 3, and bash , version bash , version bash , version bash , version bash , version 37. Author's About the Where to Go For Tools Used to Produce This Software and A. Contributed B. Reference C. A Sed and Awk D. Exit Codes With Special E. A Detailed Introduction to I/O and I/O F.
6 Command-Line Standard Command-Line bash Command-Line G. Important H. Important System I. An Introduction to Programmable J. Bash-Scripting GuideivTable of ContentsAppendix K. History L. A M. Converting DOS Batch Files to Shell N. Analyzing Writing O. Revision P. Download and Mirror Q. To Do R. S. ASCII Bash-Scripting GuidevChapter 1. Shell Programming!No programming language is perfect. There isnot even a single best language; there are onlylanguages well suited or perhaps poorly suitedfor particular MayerA working knowledge of shell scripting is essential to anyone wishing to become reasonably proficient atsystem administration, even if they do not anticipate ever having to actually write a script.
7 Consider that as aLinux machine boots up, it executes the shell scripts in / to restore the system configuration andset up services. A detailed understanding of these startup scripts is important for analyzing the behavior of asystem, and possibly modifying craft of scripting is not hard to master, since the scripts can be built in bite-sized sections and there is onlya fairly small set of shell-specific operators and options [1] to learn. The syntax is simple and straightforward,similar to that of invoking and chaining together utilities at the command line, and there are only a few "rules"governing their use.
8 Most short scripts work right the first time, and debugging even the longer ones isstraightforward. In the 1970s, the BASIC language enabled anyone reasonably computer proficient to write programs on an early generation of microcomputers. Decades later, the bash scripting language enables anyone with a rudimentary knowledge of Linux or UNIX to do the same on much more powerful shell script is a quick-and-dirty method of prototyping a complex application. Getting even a limited subsetof the functionality to work in a script is often a useful first stage in project development.
9 This way, thestructure of the application can be tested and played with, and the major pitfalls found before proceeding tothe final coding in C, C++, Java, Perl, or scripting hearkens back to the classic UNIX philosophy of breaking complex projects into simplersubtasks, of chaining together components and utilities. Many consider this a better, or at least moreesthetically pleasing approach to problem solving than using one of the new generation of high poweredall-in-one languages, such as Perl, which attempt to be all things to all people, but at the cost of forcing you toalter your thinking processes to fit the to Herbert Mayer, "a useful language needs arrays, pointers, and a generic mechanism for buildingdata structures.
10 " By these criteria, shell scripting falls somewhat short of being "useful." Or, perhaps not..When not to use shell scriptsResource-intensive tasks, especially where speed is a factor (sorting, hashing, recursion [2] ..) Procedures involving heavy-duty math operations, especially floating point arithmetic, arbitraryprecision calculations, or complex numbers (use C++ or FORTRAN instead) Cross-platform portability required (use C or Java instead) Chapter 1. Shell Programming!1 Complex applications, where structured programming is a necessity (type-checking of variables,function prototypes, etc.)
