Linux - Advanced Bash Shell Scripting Guide.pdf

(4708 KB) Pobierz
Advanced Bash-Scripting Guide
Advanced Bash-Scripting Guide
Advanced Bash-Scripting Guide
An in-depth exploration of the gentle art of
shell scripting
Mendel Cooper
Brindle-Phlogiston Associates
thegrendel@theriver.com
16 June 2002
Revision History
Revision 0.1
14 June 2000
Revised by: mc
Initial release.
Revision 0.2 30 October 2000 Revised by: mc
Bugs fixed, plus much additional material and more example scripts.
Revision 0.3
12 February 2001
Revised by: mc
Another major update.
Revision 0.4 08 July 2001 Revised by: mc
More bugfixes, much more material, more scripts - a complete revision and expansion of
the book.
Revision 0.5 03 September 2001 Revised by: mc
Major update. Bugfixes, material added, chapters and sections reorganized.
Revision 1.0
14 October 2001
Revised by: mc
Bugfixes, reorganization, material added. Stable release.
Revision 1.1
06 January 2002
Revised by: mc
Bugfixes, material and scripts added.
Revision 1.2
31 March 2002
Revised by: mc
Bugfixes, material and scripts added.
Revision 1.3
02 June 2002
Revised by: mc
http://tldp.org/LDP/abs/html/ (1 of 11) [7/15/2002 6:33:43 PM]
Advanced Bash-Scripting Guide
'TANGERINE' release: A few bugfixes, much more material and scripts added.
Revision 1.4 16 June 2002 Revised by: mc
'MANGO' release: Quite a number of typos fixed, more material and scripts added.
This tutorial assumes no previous knowledge of scripting or programming, but progresses
rapidly toward an intermediate/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 shell scripting techniques. The exercises and heavily-
commented examples invite active reader participation, under the premise that the only
way to really learn scripting is to write scripts .
The latest update of this document, as an archived, bzip2-ed "tarball" including both the
SGML source and rendered HTML, may be downloaded from the author's home site . See
the change log for a revision history.
Dedication
For Anita, the source of all the magic
http://tldp.org/LDP/abs/html/ (2 of 11) [7/15/2002 6:33:43 PM]
99150776.002.png 99150776.003.png
Advanced Bash-Scripting Guide
List of Tables
11-1. Job Identifiers
31-1. bash options
B-1. Basic sed operators
B-2. Examples
C-1. "Reserved" Exit Codes
H-1. Batch file keywords / variables / operators, and their shell equivalents
H-2. DOS Commands and Their UNIX Equivalents
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.
3-1. exit / exit status
3-2. Negating a condition using !
4-1. Code blocks and I/O redirection
4-2. Saving the results of a code block to a file
4-3. Running a loop in the background
4-4. Backup of all files changed in last day
5-1. Variable assignment and substitution
5-2. Plain Variable Assignment
5-3. Variable Assignment, plain and fancy
5-4. Integer or string?
5-5. Positional Parameters
5-6. wh , whois domain name lookup
5-7. Using shift
6-1. Echoing Weird Variables
http://tldp.org/LDP/abs/html/ (4 of 11) [7/15/2002 6:33:43 PM]
99150776.005.png
Advanced Bash-Scripting Guide
6-2. Escaped Characters
7-1. What is truth?
7-2. Equivalence of test, /usr/bin/test , [ ], and /usr/bin/[
7-3. Arithmetic Tests using (( ))
7-4. arithmetic and string comparisons
7-5. testing whether a string is null
7-6. zmost
8-1. Greatest common divisor
8-2. Using Arithmetic Operations
8-3. Compound Condition Tests Using && and ||
8-4. Representation of numerical constants:
9-1. $IFS and whitespace
9-2. Timed Input
9-3. Once more, timed input
9-4. Timed read
9-5. Am I root?
9-6. arglist : Listing arguments with $* and $@
9-7. Inconsistent $* and $@ behavior
9-8. $* and $@ when $IFS is empty
9-9. underscore variable
9-10. Converting graphic file formats, with filename change
9-11. Alternate ways of extracting substrings
9-12. Using param substitution and :
9-13. Length of a variable
9-14. Pattern matching in parameter substitution
9-15. Renaming file extensions:
9-16. Using pattern matching to parse arbitrary strings
9-17. Matching patterns at prefix or suffix of string
9-18. Using declare to type variables
9-19. Indirect References
9-20. Passing an indirect reference to awk
9-21. Generating random numbers
9-22. Rolling the die with RANDOM
9-23. Reseeding RANDOM
9-24. Pseudorandom numbers, using awk
9-25. C-type manipulation of variables
http://tldp.org/LDP/abs/html/ (5 of 11) [7/15/2002 6:33:43 PM]
99150776.001.png
Zgłoś jeśli naruszono regulamin