Home page
My Books
Covering Bash 4.0 Pro
Bash Programming
Scripting the GNU/Linux Shell

Chris F.A. Johnson

Contents

About the Author xvii
About the Technical Reviewer xviii
Introduction xix
Chapter 1: Hello, World! Your First Shell Program 1
The Code 1
The File 2
The Naming of Scripts 2
Selecting a Directory for the Script 2
Creating the File and Running the Script 3
Choosing and Using a Text Editor 3
Building a Better "Hello, World!" 5
Summary 5
Commands 5
Concepts 6
Variables 6
Exercises 6
Chapter 2: Input, Output, and Throughput 7
Parameter and Variables 7
Positional Parameters 7
Special *@#0$?_!- Parameters 8
Variables 8
Arguments and Options 8
echo, and Why You Should Avoid It 9
printf: Formatting and Printing Data 9
Escape Sequences 10
Format Specifiers 10
Width Specification 11
Printing to a Variable 13
Line Continuation 13
Standard Input/Output Streams and Redirection 13
Redirection: >, >>, and < 13
Reading Input 15
Pipelines 15
Command Substitution 16
Summary 16
Commands 16
Concepts 16
Exercises 17
Chapter 3: Looping and Branching 19
Exit Status 19
Testing an Expression 19
test, aka [ ... ] 20
[[ ... ]]: Evaluate an Expression 21
(( ... )): Evaluate an Arithmetic Expression 22
Lists 22
Conditional execution 22
if 22
Conditional Operators, && and || 23
case 24
Looping 25
while 25
until 26
for 26
break 26
continue 27
Summary 27
Commands 27
Concepts 28
Exercises 28
Chapter 4: Command-Line Parsing and Expansion 29
Quoting 30
Brace Expansion 31
Tilde Expansion 32
Parameter and Variable Expansion 33
Arithmetic Expansion 33
Command Substitution 35
Word Splitting 36
Pathname Expansion 37
Process Substitution 37
Parsing Options 38
Summary 41
Commands 41
Exercises 41
Chapter 5: Parameters and Variables 43
The Scope of a Variable: Can You See It from Here? 43
Shell Variables 44
The Naming of Variables 46
Parameter Expansion 46
Bourne Shell 46
POSIX Shell 49
Bash 51
Bash-4.0 52
Positional Parameters 53
Arrays 54
Integer-Indexed Arrays 54
Associative Arrays 56
Summary 56
Commands 56
Concepts 57
Exercises 57
Chapter 6: Shell Functions 59
Definition Syntax 59
Compound Commands 61
Getting Results 62
Set Different Exit Codes 62
Print the Result 63
Place Results in One or More Variables 63
Function Libraries 64
Using Functions from Libraries 64
Sample Script 64
Summary 66
Commands 66
Exercises 66
Chapter 7: String Manipulation 67
Concatenation 67
Repeat Character to a Given Length 68
Processing Character by Character 69
Reversal 70
Case Conversion 70
Comparing Contents Without Regard to Case 72
Check for Valid Variable Name 73
Insert One String into Another 74
Examples 74
Overlay 74
Examples 75
Trim Unwanted Characters 75
Examples 76
Index 77
Summary 78
Commands 78
Functions 78
Exercises 78
Chapter 8: File Operations and Commands 79
Reading a File 79
External Commands 81
cat 81
head 82
touch 83
ls 83
cut 84
wc 85
Regular Expressions 85
grep 86
sed 87
awk 88
File Name Expansion Options 89
nullglob 90
failglob 91
dotglob 91
extglob 91
nocaseglob 93
globstar 93
Summary 94
Shell Options 94
External Commands 94
Exercises 95
Chapter 9: Reserved Words and Builtin Commands 97
help, Display Information About Builtin Commands 97
time, Print Time Taken for Execution of a Command 98
read, Read a Line from an Input Stream 99
-r, Read Backslashes Literally 99
-e, Get Input with the readline Library 100
-a, Read Words into an Array 100
-d DELIM, Read Until DELIM Instead of a Newline 101
-n NUM, Read a Maximum of NUM Characters 101
-s, Do Not Echo Input Coming from a Terminal 101
-p PROMPT:, Output PROMPT Without a Trailing Newline 101
-t TIMEOUT, Only Wait TIMEOUT Seconds for Complete Input 102
-u FD: Read from File Descriptor FD Instead of the Standard Input 102
-i TEXT, Use TEXT as the Initial Text for readline 103
eval, Expand Arguments and Execute Resulting Command 103
Poor Man's Arrays 104
Setting Multiple Variables from One Command 106
type, Display Information About Commands 106
builtin, Execute a Builtin Command 108
command, Execute a Command or Display Information About Commands 108
pwd, Print the Current Working Directory 108
unalias, Remove One or More Aliases 109
Deprecated Builtins 109
Dynamically Loadable Builtins 109
Summary 110
Commands and Reserved Words 110
Deprecated Commands 111
Exercises 111
Chapter 10: Writing Bug-Free Scripts and Debugging the Rest 113
Prevention Is Better Than Cure 113
Structure Your Programs 113
Document Your Code 116
Format Your Code Consistently 117
The K.I.S.S. Principle 117
Test As You Go 118
Debugging a Script 120
Summary 123
Exercises 123
Chapter 11: Programming for the Command Line 125
Manipulating the Directory Stack 125
cd 125
pd 126
cdm 127
menu 128
Filesystem Functions 129
l 129
lsr 130
cp, mv 131
md 131
Miscellaneous Functions 132
pr1 132
calc 133
Managing Man Pages 133
sman 133
sus 134
k 134
Games 134
The Fifteen Puzzle 136
Summary 140
Exercises 140
Chapter 12: Runtime Configuration 141
Defining Variables 141
Command-Line Options and Arguments 141
Menus 142
Q&A Dialogue 143
Configuration Files 143
Scripts with Several Names 144
Environment Variables 146
All Together Now 146
## Script Information 147
## Default Configuration 147
## Screen Variables 148
## Function Definitions 148
## Parse Command-Line Options 154
## Bits and Pieces 155
Summary 156
Exercises 156
Chapter 13: Data Processing 157
Arrays 157
Holes in an Indexed Array 157
Using an Array for Sorting 158
Two-Dimensional Grids 163
Data File Formats 171
Line-Based Records 172
Block File Formats 175
Summary 176
Exercises 177
Chapter 14: Scripting the Screen 179
Teletypewriter vs. Canvas 179
Stretching the Canvas 180
CSI: Command Sequence Introducer 180
Priming the Canvas 181
Moving the Cursor 181
Changing Rendition Modes and Colors 182
Placing a Block of Text on the Screen 183
Scrolling Text 186
Rolling Dice 187
Summary 189
Exercises 189
Chapter 15: Entry-Level Programming 191
Single-Key Entry 191
Function Library, key-funcs 191
History in Scripts 197
Sanity Checking 198
Form Entry 199
Reading the Mouse 200
Summary 204
Exercises 204
Appendix: Shell Variables 205
Index 219