08-08-2012, 12:36 PM
Bourne Shell Programming in One Hour
shell.pdf (Size: 138.2 KB / Downloads: 64)
Introduction
Programming with the Bourne shell is similar to programming
in a conventional language. If you've ever
written code in C or Pascal, or even BASIC or FORTRAN,
you'll recognize many common features. For
instance, the shell has variables, conditional and looping
constructs, functions, and more.
Shell programming is also dierent from conventional
programming languages. For example, the
shell itself doesn't provide much useful functionality;
instead, most work must be done by invoking external
programs. As a result, the shell has powerful features
for using programs together in sequence to get work
done.
This article examines the features of the POSIX
shell, more commonly known as the Bourne shell.
The most common Bourne shell implementation on
GNU/Linux systems is bash, the \Bourne again
shell." bash incorporates several extensions to the
standard Bourne functionality; none of these will be
explored by this article. For a POSIX-compliant
Bourne shell without extensions, I recommend ash.
This article is by no means comprehensive. It just
skims the surface of many shell features. I recommend
referring to a good reference book or manpage
for more details on shell programming.
2 Shell command basics
You should already know how shell commands work
at a basic level. To start out, the command line you
typed is divided up into words. The rst word is used
as the command name, which is either understood by
the shell itself, or used as the name of an external
program to run. In either case, the rest of the words
are used as arguments to the command.
This basic description is fairly accurate, but there
is a little more going on behind the scenes. The following
aims to provide a brief explanation of what
Quoting
Sometimes you want to disable some of the shell word
expansion mechanisms above, or you want to group
what would normally be multiple space-separated
words into a single \word." Quoting takes care of
both of these.
Quoting can be done with single quotes (') or double
quotes ("):
When single quotes surround text, the contents
are treated as a single literal word. No changes
at all are made. Single quotes cannot be included
in a word surrounded by single quotes.
When double quotes surround text, the contents
are subjected to variable substitution, arithmetic
substitution, and command substitution. In addition,
the sequences n$, n`, n", and nn are replaced
by their second character.
In addition, single characters can be quoted by preceding
them with a backslash (n).
Command return values
Every command returns a value between 0 and 255.
This is separate from any output produced. The shell
interprets a return value of zero as success and a return
value of nonzero as failure.
This return value is used by several shell constructs
described below.
The character ! can be used as a command prex
to reverse the sense of a command's result; i.e., a
nonzero return value is interpreted as zero, and vice
versa.
Useful external commands
Most of what goes on in a shell program is actually
performed by external programs. Some of the most
important are listed below, along with their primary
purposes. To achieve prociency in shell programming
you should learn to use each of these. Unfortunately,
describing what each of them do in detail is
far beyond the scope of this article.
Most shells implement at least some of the programs
listed below as internal features.