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

Chris F.A. Johnson

Contents

About the Authorxvii
About the Technical Reviewerxviii
Introductionxix
Chapter 1: Hello, World! Your First Shell Program 1
The Code1
The File2
The Naming of Scripts2
Selecting a Directory for the Script2
Creating the File and Running the Script3
Choosing and Using a Text Editor3
Building a Better "Hello, World!"5
Summary5
Commands5
Concepts6
Variables6
Exercises6
Chapter 2: Input, Output, and Throughput7
Parameter and Variables7
Positional Parameters7
Special *@#0$?_!- Parameters8
Variables8
Arguments and Options8
echo, and Why You Should Avoid It9
printf: Formatting and Printing Data9
Escape Sequences10
Format Specifiers10
Width Specification11
Printing to a Variable13
Line Continuation13
Standard Input/Output Streams and Redirection13
Redirection: >, >>, and <13
Reading Input15
Pipelines15
Command Substitution16
Summary16
Commands16
Concepts16
Exercises17
Chapter 3: Looping and Branching19
Exit Status19
Testing an Expression19
test, aka [ ... ] 20
[[ ... ]]: Evaluate an Expression21
(( ... )): Evaluate an Arithmetic Expression 22
Lists22
Conditional execution22
if22
Conditional Operators, && and ||23
case24
Looping25
while25
until26
for26
break26
continue27
Summary27
Commands27
Concepts28
Exercises28
Chapter 4: Command-Line Parsing and Expansion29
Quoting30
Brace Expansion31
Tilde Expansion32
Parameter and Variable Expansion33
Arithmetic Expansion33
Command Substitution35
Word Splitting36
Pathname Expansion37
Process Substitution37
Parsing Options38
Summary41
Commands41
Exercises41
Chapter 5: Parameters and Variables43
The Scope of a Variable: Can You See It from Here?43
Shell Variables44
The Naming of Variables46
Parameter Expansion46
Bourne Shell46
POSIX Shell49
Bash51
Bash-4.052
Positional Parameters53
Arrays54
Integer-Indexed Arrays54
Associative Arrays56
Summary56
Commands56
Concepts57
Exercises57
Chapter 6: Shell Functions59
Definition Syntax59
Compound Commands61
Getting Results62
Set Different Exit Codes62
Print the Result63
Place Results in One or More Variables63
Function Libraries64
Using Functions from Libraries64
Sample Script64
Summary66
Commands66
Exercises66
Chapter 7: String Manipulation67
Concatenation67
Repeat Character to a Given Length68
Processing Character by Character69
Reversal70
Case Conversion70
Comparing Contents Without Regard to Case72
Check for Valid Variable Name73
Insert One String into Another74
Examples74
Overlay74
Examples75
Trim Unwanted Characters75
Examples76
Index77
Summary78
Commands78
Functions78
Exercises78
Chapter 8: File Operations and Commands79
Reading a File79
External Commands81
cat81
head82
touch83
ls83
cut84
wc85
Regular Expressions85
grep86
sed87
awk88
File Name Expansion Options89
nullglob90
failglob91
dotglob91
extglob91
nocaseglob93
globstar93
Summary94
Shell Options94
External Commands94
Exercises95
Chapter 9: Reserved Words and Builtin Commands97
help, Display Information About Builtin Commands97
time, Print Time Taken for Execution of a Command98
read, Read a Line from an Input Stream99
-r, Read Backslashes Literally99
-e, Get Input with the readline Library100
-a, Read Words into an Array100
-d DELIM, Read Until DELIM Instead of a Newline101
-n NUM, Read a Maximum of NUM Characters101
-s, Do Not Echo Input Coming from a Terminal101
-p PROMPT:, Output PROMPT Without a Trailing Newline101
-t TIMEOUT, Only Wait TIMEOUT Seconds for Complete Input102
-u FD: Read from File Descriptor FD Instead of the Standard Input102
-i TEXT, Use TEXT as the Initial Text for readline103
eval, Expand Arguments and Execute Resulting Command103
Poor Man's Arrays104
Setting Multiple Variables from One Command106
type, Display Information About Commands106
builtin, Execute a Builtin Command108
command, Execute a Command or Display Information About Commands108
pwd, Print the Current Working Directory108
unalias, Remove One or More Aliases109
Deprecated Builtins109
Dynamically Loadable Builtins109
Summary110
Commands and Reserved Words110
Deprecated Commands111
Exercises111
Chapter 10: Writing Bug-Free Scripts and Debugging the Rest113
Prevention Is Better Than Cure113
Structure Your Programs113
Document Your Code116
Format Your Code Consistently117
The K.I.S.S. Principle117
Test As You Go118
Debugging a Script120
Summary123
Exercises123
Chapter 11: Programming for the Command Line125
Manipulating the Directory Stack125
cd125
pd126
cdm127
menu128
Filesystem Functions129
l129
lsr130
cp, mv131
md131
Miscellaneous Functions132
pr1132
calc133
Managing Man Pages133
sman133
sus134
k134
Games134
The Fifteen Puzzle136
Summary140
Exercises140
Chapter 12: Runtime Configuration141
Defining Variables141
Command-Line Options and Arguments141
Menus142
Q&A Dialogue143
Configuration Files143
Scripts with Several Names144
Environment Variables146
All Together Now146
## Script Information147
## Default Configuration147
## Screen Variables148
## Function Definitions148
## Parse Command-Line Options154
## Bits and Pieces155
Summary156
Exercises156
Chapter 13: Data Processing157
Arrays157
Holes in an Indexed Array157
Using an Array for Sorting158
Two-Dimensional Grids163
Data File Formats171
Line-Based Records172
Block File Formats175
Summary176
Exercises177
Chapter 14: Scripting the Screen179
Teletypewriter vs. Canvas179
Stretching the Canvas180
CSI: Command Sequence Introducer180
Priming the Canvas181
Moving the Cursor181
Changing Rendition Modes and Colors182
Placing a Block of Text on the Screen183
Scrolling Text186
Rolling Dice187
Summary189
Exercises189
Chapter 15: Entry-Level Programming191
Single-Key Entry191
Function Library, key-funcs191
History in Scripts197
Sanity Checking198
Form Entry199
Reading the Mouse200
Summary204
Exercises204
Appendix: Shell Variables205
Index219