Pro_Bash_Programming_-_Scripting_the_GNU-Linux_Shell.pdf
(
2772 KB
)
Pobierz
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
About the Authors�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½xxi
Acknowledgments �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½xxiii
■Chapter
1: Hello, World: Your First Shell Program �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
1
■Chapter
2: Input, Output, and Throughput �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
7
■Chapter
3: Looping and Branching �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
19
■Chapter
4: Command-Line Parsing and Expansion �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
29
■Chapter
5: Parameters and Variables �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
43
■Chapter
6: Shell Functions �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
59
■Chapter
7: String Manipulation �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
69
■Chapter
8: File Operations and Commands �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
83
■Chapter
9: Reserved Words and Built-In Commands�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
101
■Chapter
10: Writing Bug-Free Scripts and Debugging the Rest �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
117
■Chapter
11: Programming for the Command Line �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
129
■Chapter
12: Runtime Configuration �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
145
■Chapter
13: Data Processing �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
161
■Chapter
14: Scripting the Screen �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
183
■Chapter
15: Entry-Level Programming �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
195
■Appendix
A: Shell Variables�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
209
Index �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
223
v
Chapter 1
Hello, World: Your First Shell
Program
A
shell script
is a file containing one or more commands that you would type on the command line.
This chapter describes how to create such a file and make it executable. It also covers some other issues
surrounding shell scripts, including what to name the files, where to put them, and how to run them.
I will begin with the first program traditionally demonstrated in every computer language: a program
that prints “Hello, World!” in your terminal. It’s a simple program, but it is enough to demonstrate a number
of important concepts. The code itself is the simplest part of this chapter. Naming the file and deciding
where to put it are not complicated tasks, but they are important.
For most of this chapter, you will be working in a terminal. It could be a virtual terminal, a terminal
window, or even a dumb terminal. In your terminal, the shell will immediately execute any commands you
type (after you press Enter, of course).
You should be in your home directory, which you can find in the variable
$HOME:
echo "$HOME"
You can find the current directory with either the
pwd
command or the
PWD
variable:
pwd
echo "$PWD"
If you are not in your home directory, you can get there by typing
cd
and pressing Enter at the shell
prompt.
■
Caution
If you try the code from this book on a Mac, please note that the current version of Mac OS X,
Yosemite, officially supports Bash version 3.2.53(1). The current version of Bash is 4.3, and it has the fix for the
Shellshock vulnerability. Bash 4.3 is available with most Linux distributions. Some of the code / functionality
might not be available on Mac OS X systems as it is specific to Bash 4.x.
1
ChapTer 1
■
heLLO, WOrLd: YOur FIrST SheLL prOgraM
The Code
The code is nothing more than this:
echo Hello, World!
There are three words on this command line: the command itself and two arguments. The command,
echo,
prints its arguments separated by a single space and terminated with a newline.
The File
Before you turn that code into a script, you need to make two decisions: what you will call the file and where
you will put it. The name should be unique (that is, it should not conflict with any other commands), and
you should put it where the shell can find it.
The Naming of Scripts
Beginners often make the mistake of calling a trial script
test.
To see why that is bad, enter the following at
the command prompt:
type test
The
type
command tells you what the shell will execute (and where it can be found if it is an external
file) for any given command. In
bash, type -a test
will display all the commands that match the
name
test:
$
type test
test is a shell builtin
$
type -a test
test is a shell builtin
test is /usr/bin/test
As you can see, a command called
test
already exists; it is used to test file types and to compare values.
If you call your script
test,
it will not be run when you type
test
at the shell prompt; the first command
identified by
type
will be run instead. (I’ll talk more about both
type
and
test
in later chapters.)
Typically, Unix command names are as short as possible. They are often the first two consonants of
a descriptive word (for example,
mv
for
move
or
ls
for
list)
or the first letters of a descriptive phrase
(for example,
ps
for
process status
or
sed
for
stream editor).
For this exercise, call the script
hw.
Many shell programmers add a suffix, such as
.sh,
to indicate that
the program is a shell script. The script doesn’t need it, and I use one only for programs that are being
developed. My suffix is
-sh,
and when the program is finished, I remove it. A shell script becomes another
command and doesn’t need to be distinguished from any other type of command.
2
Plik z chomika:
Expearre4
Inne pliki z tego folderu:
Linux Server Hacks 100 industr - Rob Fickenger_769.pdf
(34825 KB)
boot-time-slides.pdf
(19140 KB)
Linux Bible 2009 Edition (Bible Series) - Christopher Negus 9780470373675.pdf
(37035 KB)
Linux_Network_Servers.pdf
(36256 KB)
Linux Mint Essentials - Jay Lacroix 9781782168157.pdf
(25450 KB)
Inne foldery tego chomika:
G_CYBSEC
ha
ha2
Hakin
Magick
Zgłoś jeśli
naruszono regulamin