Advanced Bash-Scripting
A guide for those wishing to start shell scripting. Assumes no knowledge of shell
scripting but advances... |
Writing Scripts
M.2. Writing Scripts. Write a script to carry out each of the following tasks.
EASY. Home Directory Listing. Perform a recursive directory listing on the ... |
A Brief Introduction to Regular Expressions
17.1. A Brief Introduction to Regular Expressions. An expression is a string of
characters. Those characters having an interpretation above and beyond their ... |
Test Constructs
7.1. Test Constructs. An if/then construct tests whether the exit status of a
list of commands is 0 (since 0 means "success" by UNIX convention), and if so, ... |
Variable Substitution
4.1. Variable Substitution. The name of a variable is a placeholder for its value,
the data it holds. Referencing its value is called variable substitution. ... |
Recursion
33.4. Recursion. Can a script recursively call itself? Indeed. Example 33-8.
A (useless) script that recursively calls itself ... |
Miscellaneous Commands
15.9. Miscellaneous Commands. Command that fit in no special category. jot, seq.
These utilities emit a sequence of integers, with a user-selected increment ... |
Command Substitution
Chapter 11. Command Substitution. Command substitution reassigns the output of
a command [1] or even multiple commands; it literally plugs the command ... |
Using exec
19.1. Using exec. An exec <filename command redirects stdin to a file. From that
point on, all stdin comes from that file, rather than its normal source ... |
Bash, version 3
34.2. Bash, version 3. On July 27, 2004, Chet Ramey released version 3 of Bash.
This update fixes quite a number of bug in Bash and adds some new features. ... |
Indirect References to Variables
9.5. Indirect References to Variables. Assume that the value of a variable is
the name of a second variable. Is it somehow possible to retrieve the value of ... |
Localization
Appendix I. Localization. Localization is an undocumented Bash feature. A localized
shell script echoes its text output in the language defined as the ... |
Here Strings
18.1. Here Strings. A here string can be considered as a stripped-down form of
a here document. It consists of nothing more than COMMAND <<<$WORD, ... |
File test operators
7.2. File test operators. Returns true if... -e. file exists. -a. file exists.
This is identical in effect to -e. It has been "deprecated," and its use is ... |
Complex Functions and Function Complexities
23.1. Complex Functions and Function Complexities. Functions may process arguments
passed to them and return an exit status to the script for further ... |
Operators
8.1. Operators. assignment. variable assignment. Initializing or changing the
value of a variable. =. All-purpose assignment operator, which works for both ... |
/proc
27.2. /proc. The /proc directory is actually a pseudo-filesystem. The files
in /proc mirror currently running system and kernel processes and contain ... |
Of Zeros and Nulls
Chapter 28. Of Zeros and Nulls. /dev/zero and /dev/null. Uses of /dev/null.
Think of /dev/null as a "black hole." It is the nearest equivalent to a ... |
Internal Commands and Builtins
Chapter 14. Internal Commands and Builtins. A builtin is a command contained
within the Bash tool set, literally built in. This is either for performance ... |
Text Processing Commands
15.4. Text Processing Commands. Commands affecting text and text files. sort.
File sort utility, often used as a filter in a pipe. This command sorts a text ... |
Subshells
Chapter 20. Subshells. Running a shell script launches another instance of the
command processor. Just as your commands are interpreted at the command line ... |
Recursion Without Local Variables
23.3. Recursion Without Local Variables. A function may recursively call itself
even without use of local variables. Example 23-14. The Towers of Hanoi ... |
Complex Commands
15.2. Complex Commands. Commands for more advanced users. find. -exec COMMAND \;.
Carries out COMMAND on each file that find matches. ... |
Job Control Commands
14.1. Job Control Commands. Certain of the following job control commands take
a "job identifier" as an argument. See the table at end of the chapter. ... |
Credits
35.5. Credits. Community participation made this project possible. The author
gratefully acknowledges that writing this book would have been an impossible ... |
Converting DOS Batch Files to Shell Scripts
Appendix L. Converting DOS Batch Files to Shell Scripts. Quite a number of
programmers learned scripting on a PC running DOS. Even the crippled DOS batch ... |
Time / Date Commands
15.3. Time / Date Commands. Time/date and timing. date. Simply invoked, date
prints the date and time to stdout. Where this command gets interesting is in ... |
Loop Control
10.3. Loop Control. Commands Affecting Loop Behavior. break, continue. The break
and continue loop control commands [1] correspond exactly to their ... |
Colorizing Scripts
33.5. "Colorizing" Scripts. The ANSI [1] escape sequences set screen attributes,
such as bold text, and color of foreground and background. ... |
Aliases
Chapter 24. Aliases. A Bash alias is essentially nothing more than a keyboard
shortcut, an abbreviation, a means of avoiding typing a long command sequence. ... |
Escaping
5.2. Escaping. Escaping is a method of quoting single characters. The escape (\)
preceding a character tells the shell to interpret that character literally ... |
Bash, version2
34.1. Bash, version2. The current version of Bash, the one you have running on
your machine, is version 2.xx.y or 3.xx.y. ... |
Special Variable Types
4.4. Special Variable Types. local variables. variables visible only within a
code block or function (see also local variables in functions) ... |
System and Administrative Commands
Chapter 16. System and Administrative Commands. The startup and shutdown scripts
in /etc/rc.d illustrate the uses (and usefulness) of many of these comands. ... |
Functions
Chapter 23. Functions. Table of Contents; 23.1. Complex Functions and Function
Complexities; 23.2. Local Variables; 23.3. Recursion Without Local Variables ... |
Internal Variables
9.1. Internal Variables. Builtin variables. variables affecting bash script behavior.
$BASH. the path to the Bash binary itself ... |
Globbing
17.2. Globbing. Bash itself cannot recognize Regular Expressions. Inside scripts,
it is commands and utilities -- such as sed and awk -- that interpret RE's ... |
File and Archiving Commands
15.5. File and Archiving Commands. Archiving. tar. The standard UNIX archiving
utility. [1] Originally a Tape ARchiving program, it has developed into a ... |
/dev
27.1. /dev. The /dev directory contains entries for the physical devices that
may or may not be present in the hardware. [1] The hard drive partitions ... |
A Detailed Introduction to I/O and I/O Redirection
Appendix E. A Detailed Introduction to I/O and I/O Redirection. written by Stéphane
Chazelas, and revised by the document author ... |
Shell Wrappers
33.2. Shell Wrappers. A "wrapper" is a shell script that embeds a system command
or utility, that saves a set of parameters passed to that command. ... |
Loops
10.1. Loops. A loop is a block of code that iterates (repeats) a list of commands
as long as the loop control condition is true. for loops ... |
Communications Commands
15.6. Communications Commands. Certain of the following commands find use in
chasing spammers, as well as in network data transfer and analysis. ... |
Typing variables: declare or typeset
9.4. Typing variables: declare or typeset. The declare or typeset builtins (they
are exact synonyms) permit restricting the properties of variables. ... |
Reference Cards
Appendix B. Reference Cards. The following reference cards provide a useful
summary of certain scripting concepts. The foregoing text treats these matters ... |
Bibliography
Edited by Peter Denning, Computers Under Attack: Intruders, Worms, and Viruses,
ACM Press, 1990, 0-201-53067-8. This compendium contains a couple of ... |
Gotchas
case=value0 # Causes problems. 23skidoo=value1 # Also problems. # Variable names
starting with a digit are reserved by the shell. # Try _23skidoo=value1. ... |
$RANDOM: generate random integer
9.6. $RANDOM: generate random integer. $RANDOM is an internal Bash function (not
a constant) that returns a pseudorandom [1] integer in the range 0 - 32767. ... |
Basic Commands
15.1. Basic Commands. The first commands a novice learns. ls. The basic file "list"
command. It is all too easy to underestimate the power of this humble ... |
Quoting Variables
5.1. Quoting Variables. When referencing a variable, it is generally advisable
to enclose its name in double quotes. This prevents reinterpretation of all ... |
Unofficial Shell Scripting Stylesheet
32.1. Unofficial Shell Scripting Stylesheet. Comment your code. This makes it
easier for others to understand (and appreciate), and easier for you to ... |
Arrays
Chapter 26. Arrays. Newer versions of Bash support one-dimensional arrays.
Array elements may be initialized with the variable[xx] notation. ... |
Awk
C.2. Awk. Awk is a full-featured text processing language with a syntax reminiscent
of C. While it possesses an extensive set of operators and capabilities, ... |
Testing and Branching
10.4. Testing and Branching. The case and select constructs are technically not
loops, since they do not iterate the execution of a code block. ... |
Sed
C.1. Sed. Sed is a non-interactive line editor. It receives text input, whether
from stdin or from a file, performs certain operations on specified lines of ... |
Assorted Tips
33.7. Assorted Tips. To keep a record of which user scripts have run during a
particular session or over a number of sessions, add the following lines to ... |
Math Commands
bc. Bash can't handle floating point calculations, and it lacks operators for
certain important mathematical functions. Fortunately, bc comes to the rescue. ... |
Local Variables
23.2. Local Variables. What makes a variable "local"? local variables. A variable
declared as local is one that is visible only within the block of code in ... |
Redirecting Code Blocks
19.2. Redirecting Code Blocks. Blocks of code, such as while, until, and for
loops, even if/then test blocks can also incorporate redirection of stdin. ... |
Revision History
This document first appeared as a 60-page HOWTO in the late spring of 2000.
Since then, it has gone through quite a number of updates and revisions. ... |
Debugging
Debugging is twice as hard as writing the code in the first place. Therefore, if
you write the code as cleverly as possible, you are, by definition, ... |
Commands
Part 4. Commands. Mastering the commands on your Linux machine is an indispensable
prelude to writing effective shell scripts. ... |
Copyright
Appendix Q. Copyright. The Advanced Bash Scripting Guide is copyright © 2000, by
Mendel Cooper. The author also asserts copyright on all previous versions ... |
Analyzing a System Script
16.1. Analyzing a System Script. Using our knowledge of administrative commands,
let us examine a system script. One of the shortest and simplest to ... |
Options
Chapter 30. Options. Options are settings that change shell and/or script behavior.
The set command enables options within a script. ... |
Variable Assignment
Caution. Do not confuse this with = and -eq, which test, rather than assign!
Note that = can be either an assignment or a test operator, depending on ... |
Quoting
Chapter 5. Quoting. Table of Contents; 5.1. Quoting Variables; 5.2. Escaping.
Quoting means just that, bracketing a string in quotes. ... |
The Double Parentheses Construct
9.7. The Double Parentheses Construct. Similar to the let command, the ((...))
construct permits arithmetic expansion and evaluation. In its simplest form, ... |
Important System Directories
Appendix H. Important System Directories. Sysadmins and anyone else writing
administrative scripts should be intimately familiar with the following system ... |
Analyzing Scripts
M.1. Analyzing Scripts. Examine the following script. Run it, then explain what
it does. Annotate the script and rewrite it in a more compact and elegant ... |
Applications
19.3. Applications. Clever use of I/O redirection permits parsing and stitching
together snippets of command output (see Example 14-7). ... |
Exit Codes With Special Meanings
1, Catchall for general errors, let "var1 = 1/0", Miscellaneous errors, such
as "divide by zero". 2, Misuse of shell builtins (according to Bash ... |
Interactive and non-interactive shells and scripts
33.1. Interactive and non-interactive shells and scripts. An interactive shell
reads commands from user input on a tty. Among other things, such a shell ... |
Invoking the script
2.1. Invoking the script. Having written the script, you can invoke it by sh
scriptname, [1] or alternatively bash scriptname. (Not recommended is using sh ... |
Optimizations
33.6. Optimizations. Most shell scripts are quick 'n dirty solutions to non-complex
problems. As such, optimizing them for speed is not much of an issue. ... |
Author's Note
How did I come to write a Bash scripting book? It's a strange tale. It seems that
a few years back I needed to learn shell scripting -- and what better way ... |
Bash Variables Are Untyped
4.3. Bash Variables Are Untyped. Unlike many other programming languages, Bash
does not segregate its variables by "type". Essentially, Bash variables are ... |
Arithmetic Expansion
Chapter 12. Arithmetic Expansion. Arithmetic expansion provides a powerful tool
for performing (integer) arithmetic operations in scripts. ... |
Terminal Control Commands
15.7. Terminal Control Commands. Command affecting the console or terminal. tput.
Initialize terminal and/or fetch information about it from terminfo data. ... |
Nested Loops
10.2. Nested Loops. A nested loop is a loop within a loop, an inner loop within
the body of an outer one. How this works is that the first pass of the outer ... |
To Do List
Appendix P. To Do List. A comprehensive survey of incompatibilities between Bash
and the classic Bourne shell. Same as above, but for the Korn shell (ksh). ... |
Important Files
Appendix G. Important Files. startup files. These files contain the aliases and
environmental variables made available to Bash running as a user shell and ... |
Security Issues
33.8.1. Infected Shell Scripts. A brief warning about script security is appropriate.
A shell script may contain a worm, trojan, or even a virus. ... |
Portability Issues
33.9. Portability Issues. This book deals specifically with Bash scripting on a
GNU/Linux system. All the same, users of sh and ksh will find much of value ... |
Advanced Topics
Part 5. Advanced Topics. At this point, we are ready to delve into certain of
the difficult and unusual aspects of scripting. Along the way, we will attempt ... |
Tests and Comparisons: Alternatives
33.3. Tests and Comparisons: Alternatives. For tests, the [[ ]] construct may be
more appropriate than [ ]. Likewise, arithmetic comparisons might benefit ... |
Testing Your Knowledge of Tests
7.5. Testing Your Knowledge of Tests. The systemwide xinitrc file can be used to
launch the X server. This file contains quite a number of if/then tests. ... |
Introduction
Part 1. Introduction. The shell is a command interpreter. More than just the
insulating layer between the operating system kernel and the user, ... |
History Commands
Appendix J. History Commands. The Bash shell provides command-line tools for
editing and manipulating a user's command history. ... |
About the Author
35.2. About the Author. Who is this guy anyhow? The author claims no credentials
or special qualifications, other than a compulsion to write. ... |
Tools Used to Produce This Book
35.4.1. Hardware. A used IBM Thinkpad, model 760XL laptop (P166, 104 meg RAM)
running Red Hat 7.1/7.3. Sure, it's slow and has a funky keyboard, ... |
A Sed and Awk Micro-Primer
Appendix C. A Sed and Awk Micro-Primer. Table of Contents; C.1. Sed; C.2. Awk.
This is a very brief introduction to the sed and awk text processing ... |
Where to Go For Help
35.3. Where to Go For Help. The author will sometimes, if not too busy (and in
a good mood), answer general scripting questions. [1] However, if you have a ... |
Manipulating Strings
9.2. Manipulating Strings. Bash supports a surprising number of string manipulation
operations. Unfortunately, these tools lack a unified focus. ... |
Other Comparison Operators
7.3. Other Comparison Operators. A binary comparison operator compares two
variables or quantities. Note that integer and string comparison use a different ... |
Why Shell Programming?
No programming language is perfect. There is not even a single best language;
there are only languages well suited or perhaps poorly suited for particular ... |
Basics
Part 2. Basics. Table of Contents; 3. Special Characters; 4. Introduction to
Variables and Parameters. 4.1. Variable Substitution; 4.2. ... |
Variables Revisited
Chapter 9. Variables Revisited. Table of Contents; 9.1. Internal Variables; 9.2.
Manipulating Strings. 9.2.1. Manipulating strings using awk; 9.2.2. ... |
Mirror Sites
Appendix O. Mirror Sites. The latest update of this document, as an archived "tarball"
including both the SGML source and rendered HTML, may be downloaded ... |
External Filters, Programs and Commands
Chapter 15. External Filters, Programs and Commands. Table of Contents; 15.1.
Basic Commands; 15.2. Complex Commands; 15.3. Time / Date Commands; 15.4. ... |