The OpenNET Project

Поиск:  Каталог документации | Shell | Языки программирования

Advanced Bash-Scripting HOWTO

A guide to shell scripting, using Bash

Mendel Cooper

v0.2, 30 October 2000

This is a major update on version 0.1. -- a couple of bugs fixed, plus much additional material and more example scripts added.

This document is both a tutorial and a reference on shell scripting with Bash. It assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction. The exercises and heavily-commented examples invite active reader participation. Still, it is a work in progress. The intention is to add much supplementary material in future updates to this HOWTO, so that it will gradually evolve into an LDP "guide", i.e., a complete book.

Table of Contents
1. Why Shell Programming?
2. Starting Off With a Sha-Bang
2.1. Invoking the script
2.2. Shell wrapper, self-executing script
3. Tutorial / Reference
3.1. exit and exit status
3.2. Special characters used in shell scripts
3.3. Introduction to Variables and Parameters
3.4. Quoting
3.5. Tests
3.6. Operations and Related Topics
3.7. Variables Revisited
3.8. Loops
3.9. Internal Commands and Builtins
3.10. External Filters, Programs and Commands
3.11. System and Administrative Commands
3.12. Backticks (`...`)
3.13. I/O Redirection
3.14. Recess Time
3.15. Regular Expressions
3.16. Subshells
3.17. Process Substitution
3.18. Functions
3.19. List Constructs
3.20. Arrays
3.21. Files
3.22. Here Documents
3.23. Of Zeros and Nulls
3.24. Debugging
3.25. Options
3.26. Gotchas
3.27. Miscellany
3.28. Bash, version 2
4. Credits
A. Contributed Scripts
B. Copyright
List of Tables
3-1. bash options
List of Examples
2-1. cleanup: A script to clean up the log files in /var/log
2-2. cleanup: An enhanced and generalized version of above script.
2-3. shell wrapper
2-4. A slightly more complex shell wrapper
3-1. exit / exit status
3-2. Code blocks and I/O redirection
3-3. Saving the results of a code block to a file
3-4. Backup of all files changed in last day
3-5. Variable assignment and substitution
3-6. Using param substitution and :
3-7. Renaming file extensions:
3-8. Using pattern matching to parse arbitrary strings
3-9. What is truth?
3-10. Equivalence of [ ] and test
3-11. Tests, command chaining, redirection
3-12. arithmetic and string comparisons
3-13. zmost
3-14. Compound Condition Tests Using && and ||
3-15. Representation of numerical constants:
3-16. Variable Assignment
3-17. Variable Assignment, plain and fancy
3-18. Positional Parameters
3-19. wh, whois domain name lookup
3-20. Using shift
3-21. Using declare to type variables
3-22. Indirect References
3-23. Generating random numbers
3-24. Simple for loops
3-25. Missing in [list] in a for loop
3-26. Using efax in batch mode
3-27. Simple while loop
3-28. Another while loop
3-29. until loop
3-30. Effects of break and continue in a loop
3-31. Using case
3-32. Creating menus using case
3-33. Creating menus using select
3-34. Creating menus using select in a function
3-35. Using getopts to read the flags/options passed to a script
3-36. Using set with positional parameters
3-37. basename and dirname
3-38. Variable assignment, using read
3-39. Changing the current working directory
3-40. "Including" a data file
3-41. Waiting for a process to finish before proceeding
3-42. Using ls to create a table of contents for burning a CDR disk
3-43. Badname, eliminate file names in current directory containing bad characters and white space.
3-44. Log file using xargs to monitor system log
3-45. copydir, copying files in current directory to another, using xargs
3-46. Showing the effect of eval
3-47. Forcing a log-off
3-48. Using expr
3-49. Letting let do some arithmetic.
3-50. printf in action
3-51. Using cpio to move a directory tree
3-52. toupper: Transforms a file to all uppercase.
3-53. lowercase: Changes all filenames in working directory to lowercase.
3-54. nl: A self-numbering script.
3-55. Formatted file listing.
3-56. Using date
3-57. uuencoding encoded files
3-58. Using seq to generate loop arguments
3-59. Effects of exec
3-60. killall, from /etc/rc.d/init.d
3-61. Perl embedded in a bash script
3-62. Variable scope in a subshell
3-63. Running parallel processes in subshells
3-64. Simple function
3-65. Function Taking Parameters
3-66. Converting numbers to Roman numerals
3-67. Local variable visibility
3-68. Recursion, using a local variable
3-69. Using an "and list" to test for command-line arguments
3-70. Using "or lists" in combination with an "and list"
3-71. Simple array usage
3-72. Some special properties of arrays
3-73. An old friend: The Bubble Sort
3-74. Complex array application: Sieve of Erastosthenes
3-75. dummyfile: Creates a 2-line dummy file
3-76. broadcast: Sends message to everyone logged in
3-77. Multi-line message using cat
3-78. upload: Uploads a file pair to "Sunsite" incoming directory
3-79. Setting up a swapfile using /dev/zero
3-80. test23, a buggy script
3-81. test24, another buggy script
3-82. Trapping at exit
3-83. Cleaning up after Control-C
3-84. String expansion
3-85. Indirect variable references - the new way
3-86. Using arrays and other miscellaneous trickery to deal four random hands from a deck of cards
A-1. manview: A script for viewing formatted man pages
A-2. manview: A script for uploading to an ftp site, using a locally encrypted password
A-3. behead: A script for removing mail and news message headers
A-4. ftpget: A script for downloading files via ftp

  Закладки на сайте
  Проследить за страницей
Created 1996-2014 by Maxim Chirkov  
RUNNet TopList