Ebooks File Shell Programming in Unix Linux and OS X Stephen G. Kochan All Chapters
Ebooks File Shell Programming in Unix Linux and OS X Stephen G. Kochan All Chapters
com
https://textbookfull.com/product/shell-
programming-in-unix-linux-and-os-x-stephen-g-
kochan/
https://textbookfull.com/product/wicked-cool-shell-
scripts-101-scripts-for-linux-os-x-and-unix-systems-dave-taylor/
textbookfull.com
https://textbookfull.com/product/mastering-linux-shell-scripting-a-
practical-guide-to-linux-command-line-bash-scripting-and-shell-
programming-ebrahim/
textbookfull.com
https://textbookfull.com/product/issues-decisive-for-china-s-rise-or-
fall-an-international-law-perspective-yuwa-wei/
textbookfull.com
Biography of a yogi : Paramahansa Yogananda and the
origins of modern yoga 1st Edition Foxen
https://textbookfull.com/product/biography-of-a-yogi-paramahansa-
yogananda-and-the-origins-of-modern-yoga-1st-edition-foxen/
textbookfull.com
https://textbookfull.com/product/automata-and-computability-a-
programmers-perspective-ganesh-gopalakrishnan/
textbookfull.com
https://textbookfull.com/product/android-studio-3-0-development-
essentials-android-8-edition-neil-smyth/
textbookfull.com
https://textbookfull.com/product/visualizing-graph-data-meap-edition-
corey-lanum/
textbookfull.com
https://textbookfull.com/product/on-critical-pedagogy-2nd-edition-
henry-a-giroux/
textbookfull.com
Demography of Russia: From the Past to the Present 1st
Edition Tatiana Karabchuk
https://textbookfull.com/product/demography-of-russia-from-the-past-
to-the-present-1st-edition-tatiana-karabchuk/
textbookfull.com
Shell Programming
in Unix, Linux
and OS X
Fourth Edition
Stephen G. Kochan
Patrick Wood
ISBN-13: 978-0-13-4449600-9
ISBN-10: 0-13-449600-0
Printed in the United States of America
First Printing: August 2016
The Library of Congress Control Number is on file.
Brief Contents
Introduction 1
4 And Away We Go 93
Index 363
Contents
Introduction 1
How This Book Is Organized 2
Accessing the Free Web Edition 3
4 And Away We Go 93
Command Files 93
Comments 96
Variables 97
Displaying the Values of Variables 98
Undefined Variables Have the Null Value 100
Filename Substitution and Variables 101
The ${variable} Construct 102
Built-in Integer Arithmetic 103
Index 363
Introduction
It’s no secret that the family of Unix and Unix-like operating systems has emerged over the last
few decades as the most pervasive, most widely used group of operating systems in computing
today. For programmers who have been using Unix for many years, this came as no surprise:
The Unix system provides an elegant and efficient environment for program development.
That’s exactly what Dennis Ritchie and Ken Thompson sought to create when they developed
Unix at Bell Laboratories way back in the late 1960s.
Note
Throughout this book we’ll use the term Unix to refer generically to the broad family of
Unix-based operating systems, including true Unix operating systems such as Solaris
as well as Unix-like operating systems such as Linux and Mac OS X.
One of the strongest features of the Unix system is its wide collection of programs. More than
200 basic commands are distributed with the standard operating system and Linux adds to it,
often shipping with 700–1000 standard commands! These commands (also known as tools)
do everything from counting the number of lines in a file, to sending electronic mail, to
displaying a calendar for any desired year.
But the real strength of the Unix system comes not from its large collection of commands but
from the elegance and ease with which these commands can be combined to perform far more
sophisticated tasks.
The standard user interface to Unix is the command line, which actually turns out to be a
shell, a program that acts as a buffer between the user and the lowest levels of the system itself
(the kernel ). The shell is simply a program that reads in the commands you type and converts
them into a form more readily understood by the system. It also includes core programming
constructs that let you make decisions, loop, and store values in variables.
The standard shell distributed with Unix systems derives from AT&T’s distribution, which
evolved from a version originally written by Stephen Bourne at Bell Labs. Since then,
the IEEE has created standards based on the Bourne shell and the other more recent shells.
The current version of this standard, as of this writing, is the Shell and Utilities volume
of IEEE Std 1003.1-2001, also known as the POSIX standard. This shell is what we use as the
basis for the rest of this book.
The examples in this book were tested on a Mac running Mac OS X 10.11, Ubuntu Linux 14.0,
and an old version of SunOS 5.7 running on a Sparcstation Ultra-30. All examples, with the
2 Introduction
exception of some Bash examples in Chapter 14, were run using the Korn shell, although all of
them also work fine with Bash.
Because the shell offers an interpreted programming language, programs can be written, modified,
and debugged quickly and easily. We turn to the shell as our first choice of programming
language and after you become adept at shell programming, you will too.
Chapter 2, “What Is the Shell?,” reveals what the shell really is, how it works, and how it ends
up being your primary method of interacting with the operating system itself. You’ll learn
about what happens every time you log in to the system, how the shell program gets started,
how it parses the command line, and how it executes other programs for you. A key point
made in Chapter 2 is that the shell is just another program; nothing more, nothing less.
Chapter 3, “Tools of the Trade,” provides tutorials on tools useful in writing shell programs.
Covered in this chapter are cut, paste, sed, grep, sort, tr, and uniq. Admittedly, the
selection is subjective, but it does set the stage for programs that we’ll develop throughout the
remainder of the book. Also in Chapter 3 is a detailed discussion of regular expressions, which
are used by many Unix commands, such as sed, grep, and ed.
Chapters 4 through 9 teach you how to put the shell to work for writing programs. You’ll
learn how to write your own commands; use variables; write programs that accept arguments;
make decisions; use the shell’s for, while, and until looping commands; and use the read
command to read data from the terminal or from a file. Chapter 5, “Can I Quote you on
That?”, is devoted entirely to a discussion of one of the most intriguing (and often confusing)
aspects of the shell: the way it interprets quotes.
By that point in the book, all the basic programming constructs in the shell will have been
covered, and you will be able to write shell programs to solve your particular problems.
Chapter 10, “Your Environment,” covers a topic of great importance for a real understanding
of the way the shell operates: the environment. You’ll learn about local and exported variables;
subshells; special shell variables, such as HOME, PATH, and CDPATH; and how to set up
your .profile file.
Chapter 11, “More on Parameters,” and Chapter 12, “Loose Ends,” tie up some loose ends, and
Chapter 13, “Rolo Revisited,” presents a final version of a phone directory program called
rolo that is developed throughout the book.
Accessing the Free Web Edition 3
Chapter 14, “Interactive and Nonstandard Shell Features,” discusses features of the shell that
either are not formally part of the IEEE POSIX standard shell (but are available in most
Unix and Linux shells) or are mainly used interactively instead of in programs.
Appendix A, “Shell Summary,” summarizes the features of the IEEE POSIX standard shell.
Appendix B, “For More Information,” lists references and resources, including the Web sites
where different shells can be downloaded.
The philosophy this book uses is to teach by example. We believe that properly chosen
examples do a far better job of illustrating how a particular feature is used than ten times as
many words. The old “A picture is worth …” adage seems to apply just as well to coding.
We encourage you to type in each example and test it on your own system, for only by doing
can you become adept at shell programming. Don’t be afraid to experiment. Try changing
commands in the program examples to see the effect, or add different options or features to
make the programs more useful or robust.
The Web Edition can be viewed on all types of computers and mobile devices with any modern
web browser that supports HTML5.
To get access to the Web Edition of Shell Programming with Unix, Linux, and OS X all you need to
do is register this book:
1. Go to www.informit.com/register.
The Web Edition will appear under the Digital Purchases tab on your Account page. Click the
Launch link to access the product.
1
A Quick Review
of the Basics
This chapter provides a review of the Unix system, including the file system, basic commands,
filename substitution, I/O redirection, and pipes.
date prints the day of the week, month, day, time (24-hour clock, the system’s time zone),
and year. Throughout the code examples in this book, whenever we use boldface type like
this, it’s to indicate what you, the user, type in. Normal face type like this is used to
indicate what the Unix system prints. Italic type is used for comments in interactive sequences.
Every Unix command is submitted to the system with the pressing of the Enter key. Enter
says that you are finished typing things in and are ready for the Unix system to do its thing.
Here, three users are logged in: pat, ruth, and steve. Along with each user ID is listed the
tty number of that user and the day and time that user logged in. The tty number is a unique
identification number the Unix system gives to each terminal or network device that a user is
on when they log into the system.
The who command also can be used to get information about yourself:
$ who am i
pat tty29 Jul 19 14:40
$
who and who am i are actually the same command: who. In the latter case, the am and i are
arguments to the who command. (This isn’t a good example of how command arguments work;
it’s just a curiosity of the who command.)
You will notice from the preceding example that echo squeezes out extra blanks between
words. That’s because on a Unix system, the words are important while the blanks are only
there to separate the words. Generally, the Unix system ignores extra blanks (you’ll learn more
about this in the next chapter).
A filename can be composed of just about any character directly available from the keyboard
(and even some that aren’t) provided that the total number of characters contained in the
name is not greater than 255. If more than 255 characters are specified, the Unix system
simply ignores the extra characters.
Working with Files 7
The Unix system provides many tools that make working with files easy. Here we’ll review
some of the basic file manipulation commands.
This output indicates that three files called READ_ME, names, and tmp are contained in the
current directory. (Note that the output of ls may vary from system to system. For example,
on many Unix systems ls produces multicolumn output when sending its output to a
terminal; on others, different colors may be used for different types of files. You can always
force single-column output with the –1 option—that’s the number one.)
The wc command lists three numbers followed by the filename. The first number represents the
number of lines in the file (5), the second the number of words (7), and the third the number
of characters (27).
8 Chapter 1 A Quick Review of the Basics
Command Options
Most Unix commands allow the specification of options at the time a command is executed.
These options generally follow the same format:
-letter
That is, a command option is a minus sign followed immediately by a single letter. For
example, to count just the number of lines contained in a file, the option -l (that’s the letter l)
is given to the wc command:
$ wc -l names
5 names
$
Finally, the -w option can be used to count the number of words contained in the file:
$ wc -w names
7 names
$
Some commands require that the options be listed before the filename arguments. For example,
sort names -r is acceptable, whereas wc names -l is not. Still, the former is unusual and
most Unix commands are designed for you to specify command options first, as exemplified by
wc -l names.
Execution of this command causes the contents of the file names to be copied into a new file
named saved_names. As with many Unix commands, the fact that no output other than a
command prompt was displayed after the cp command was typed indicates that the command
executed successfully.
renamed, and the second argument is the new name. So, to change the name of the file
saved_names to hold_it, for example, the following command would do the trick:
$ mv saved_names hold_it
$
Be careful! When executing an mv or cp command, the Unix system does not care whether the
file specified as the second argument already exists. If it does, the contents of the file will be
lost. For example, if a file called old_names exists, executing the command
cp names old_names
would copy the filenames to old_names, destroying the previous contents of old_names in the
process. Similarly, the command
mv names old_names
would rename names to old_names, even if the file old_names existed prior to execution of
the command.
You can remove more than one file at a time with the rm command by simply specifying all
such files on the command line. For example, the following would remove the three files wb,
collect, and mon:
$ rm wb collect mon
$
documents programs
The file directory documents contains the files plan, dact, sys.A, new.hire, no.JSK, and
AMG.reply. The directory programs contains the files wb, collect, and mon. At some point,
you may decide to further categorize the files in a directory. This can be done by creating
subdirectories and then placing each file into the appropriate subdirectory. For example,
you might want to create subdirectories called memos, proposals, and letters inside your
documents directory, as shown in Figure 1.2.
documents programs
documents contains the subdirectories memos, proposals, and letters. Each of these
subdirectories in turn contains two files: memos contains plan and dact; proposals contains
sys.A and new.hire; and letters contains no.JSK and AMG.reply.
Although each file in a given directory must have a unique name, files contained in different
directories do not. So you could have a file in your programs directory called dact, even
though a file by that name also exists in the memos subdirectory.
Although the location of users’ home directories can vary from one system to the next,
let’s assume that your home directory is called steve and that this directory is actually a
subdirectory of a directory called users. Therefore, if you had the directories documents and
programs, the overall directory structure would actually look something like Figure 1.3.
A special directory named / (pronounced “slash”) is shown at the top of the directory tree.
This directory is known as the root.
Whenever you are “inside” a particular directory (called your current working directory), the
files contained within that directory are immediately accessible, without specifying any path
information. If you want to access a file from another directory, you can either first issue a
command to “change” to the appropriate directory and then access the particular file, or you
can specify the particular file by its pathname.
Working with Directories 11
users
…
pat steve ruth
documents programs
A pathname enables you to uniquely identify a particular file to the Unix system. In the
specification of a pathname, successive directories along the path are separated by the slash
character /. A pathname that begins with a slash character is known as a full or absolute
pathname because it specifies a complete path from the root. For example, the pathname
/users/steve identifies the directory steve contained within the directory users. Similarly,
the pathname /users/steve/documents references the directory documents as contained
in the directory steve within users. As a final example, the pathname /users/steve/
documents/letters/AMG.reply identifies the file AMG.reply contained along the appropriate
directory path.
To help reduce the typing that would otherwise be required, Unix provides certain notational
conveniences. A pathname that does not begin with a slash is known as a relative pathname:
the path is relative to your current working directory. For example, if you just logged in to the
system and were placed into your home directory /users/steve, you could directly reference
the directory documents simply by typing documents. Similarly, the relative pathname
programs/mon could be typed to access the file mon contained inside your programs directory.
By convention, .. always references the directory that is one level higher than the current
directory, known as the parent directory. For example, if you were in your home directory
/users/steve, the pathname .. would reference the directory users. If you had issued
the appropriate command to change your working directory to documents/letters, the
pathname .. would reference the documents directory, ../.. would reference the directory
steve, and ../proposals/new.hire would reference the file new.hire contained in the
proposals directory. There is usually more than one way to specify a path to a particular
file, a very Unix-y characteristic.
12 Chapter 1 A Quick Review of the Basics
Another notational convention is the single period., which always refers to the current
directory. That’ll become more important later in the book when you want to specify a shell
script in the current directory, not one in the PATH. We’ll explain this in more detail soon.
Recall the directory structure from Figure 1.3. The directory that you are placed in after you log
in to the system is called your home directory. You can assume from Figure 1.3 that the home
directory for the user steve is /users/steve. Therefore, whenever steve logs in to
the system, he will automatically be placed inside this directory. To verify that this is the
case, the pwd (print working directory) command can be issued:
$ pwd
/users/steve
$
The output from the command verifies that steve’s current working directory is /users/
steve.
Let’s assume that you just logged in to the system and were placed in your home directory,
/users/steve. This is depicted by the arrow in Figure 1.4.
You know that two directories are directly “below” steve’s home directory: documents and
programs. This can be easily verified at the terminal by issuing the ls command:
$ ls
documents
programs
$
The ls command lists the two directories documents and programs the same way it listed
other ordinary files in previous examples.
Working with Directories 13
users
…
pat steve ruth
documents programs
To change your current working directory, issue the cd command, followed by the name of the
new directory:
$ cd documents
$
After executing this command, you will be placed inside the documents directory, as depicted
in Figure 1.5.
users
…
pat steve ruth
documents programs
INTRODUCTION[58]
Igneous rocks, those varied masses that have consolidated from a
state of fusion, attracted attention in the eighteenth century through
their active appearance in volcanoes. James Hutton in 1785 showed
that the crystalline granite of the Scottish highlands "had been made
to invade that country in a fluid state." More than a hundred years,
however, elapsed before geologists on the continent of Europe were
willing to connect superficial lavas with the materials exposed by
denudation in consolidated cauldrons of the crust.
It is interesting therefore to note that G. P. Scrope in 1825 treated of
granite, without apology or hesitation, in a work entitled
"Considerations on Volcanoes." So far from separating deep-seated
from superficial products, Scrope wrote of the molten magma in the
crust as "the general subterranean bed of lava." He conceived this
fundamental magma, "the original or mother-rock," to be capable of
consolidating as ordinary granite. Successive meltings and physical
modifications of this granite gave rise, in his view, to all the other
igneous rocks. Scrope laid no stress, however, on chemical variations
within the magma, but urged that the transitions observable
between different types of igneous material established a community
of origin.
The connexion between lavas and highly crystalline deep-seated
rocks, so simply accepted by Scrope, was worked out some fifty
years later by J. W. Judd for areas in Hungary and in the Inner
Hebrides. The features displayed in thin sections under the
microscope were used by Judd, in a series of papers, to substantiate
his views; but in France and Germany these features became the
source of subtle distinctions between the igneous rocks of Cainozoic
and pre-Cainozoic days. The lavas, in which some glassy matter
could be traced, were said to be typically post-Cretaceous, and
essentially different from those earlier types in which glass was
replaced by finely crystalline matter; while the coarsely crystalline
igneous rocks were uniformly regarded as pre-Cainozoic. Glassy
rocks, such as pitchstone, interbedded contemporaneously in
Permian or Devonian strata, were described as "vitreous porphyries,"
while those known to be of post-Cretaceous date might be styled
andesites, trachytes, or rhyolites. Luckily common sense has recently
triumphed in this matter, and the relative scarcity of glassy types of
igneous rocks in early geological formations has been recognised as
due to the readiness with which glass undergoes secondary
crystallisation. The discussion has ended by showing that we have
no evidence of world-wide changes in the types of material erupted
during geological time.
At the present day, attention has been focused on the processes that
go on in subterranean cauldrons, in the hope of explaining the
differences between one type of extruded rock and another.
Doctrines of descent have been elaborated, and one of the most
subtle systems of classification[59] has been based upon characters
that the igneous rock might have possessed, had circumstances not
imparted others to it during the process of consolidation. The
principle of this classification is, however, obviously correct, if we
wish to trace back a rock bearing certain characters at the present
day to the molten source from which it came.