Pro Bash Programming - Chris Johnson

Pro Bash Programming (eBook)

Scripting the Linux Shell

(Autor)

eBook Download: PDF
2009 | 1st ed.
350 Seiten
Apress (Verlag)
978-1-4302-1998-9 (ISBN)
Systemvoraussetzungen
66,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen

The bash shell is a complete programming language, not merely a glue to combine external Linux commands. By taking full advantage of shell internals, shell programs can perform as snappily as utilities written in C or other compiled languages. And you will see how, without assuming Unix lore, you can write professional bash 4.0 programs through standard programming techniques.

  • Complete bash coverage
  • Teaches bash as a programming language
  • Helps you master bash 4.0 features


Chris F.A. Johnson was introduced to Unix in 1990 and learned shell scripting because there was no C compiler on the system. His first major project was a menu-driven, user-extensible database system with report generator. Chris uses the shell as his primary, general-purpose programming language, and his projects have included a member database, menuing system, and POP3 mail filtering and retrieval. Chris is the author of Shell Scripting Recipes: A Problem-Solution Approach (Apress, 2005). When not pushing shell scripting to the limit, he designs and codes web sites, teaches chess, and composes cryptic crosswords.
Introduction Although most users think of the shell as an interactive command interpreter, it is really a programming language in which each statement runs a command. Because it must satisfy both the interactive and programming aspects of command execution, it is a strange language, shaped as much by history as by design. Brian Kernighan and Rob Pike, The UNIX Programming Environment, Prentice-Hall, 1984 The shell is a programming language. Don't let anyone tell you otherwise. The shell is not just glue that sticks bits together. The shell is a lot more than a tool that runs other tools. The shell is a complete programming language! When a Linux user asked me about membership databases, I asked him what he really needed. He wanted to store names and addresses for a couple of hundred members and print mailing labels for each of them. I recommended using a text editor to store the information in a text file, and I provided a shell script to create the labels in PostScript. (The script, ps-labels, appeared in my first book, Shell Scripting Recipes: A Problem-Solution Approach. ) When the SWEN worm was dumping hundreds of megabytes of junk into my mailbox every few minutes, I wrote a shell script to filter them out on the mail server and download the remaining mail to my home computer. That script has been doing its job for several years.

Chris F.A. Johnson was introduced to Unix in 1990 and learned shell scripting because there was no C compiler on the system. His first major project was a menu-driven, user-extensible database system with report generator. Chris uses the shell as his primary, general-purpose programming language, and his projects have included a member database, menuing system, and POP3 mail filtering and retrieval. Chris is the author of Shell Scripting Recipes: A Problem-Solution Approach (Apress, 2005). When not pushing shell scripting to the limit, he designs and codes web sites, teaches chess, and composes cryptic crosswords.

Contents at a Glance 4
Contents 5
About the Author 16
About the Technical Reviewer 17
Introduction 18
Why the Shell? 18
Just the Shell, Ma’am, Just the Shell 19
A Brief History of sh 19
Which Version of Bash? 19
Who Will Benefit from This Book? 19
What’s in the Book? 20
Downloading the Code 21
Contacting the Author 21
Hello, World! Your First Shell Program 22
The Code 22
The File 23
Choosing and Using a Text Editor 24
Building a Better “Hello, World!” 26
Summary 26
Exercises 27
Input, Output, and Throughput 28
Parameter and Variables 28
Arguments and Options 29
echo, and Why You Should Avoid It 30
printf: Formatting and Printing Data 30
Line Continuation 34
Standard Input/Output Streams and Redirection 34
Reading Input 36
Pipelines 36
Command Substitution 37
Summary 37
Exercises 38
Looping and Branching 39
Exit Status 39
Testing an Expression 39
Lists 42
Conditional execution 42
Looping 45
Summary 47
Exercises 48
Command-Line Parsing and Expansion 49
Quoting 50
Brace Expansion 51
Tilde Expansion 52
Parameter and Variable Expansion 53
Arithmetic Expansion 53
Command Substitution 55
Word Splitting 56
Pathname Expansion 57
Process Substitution 57
Parsing Options 58
Summary 61
Exercises 61
Parameters and Variables 62
The Scope of a Variable: Can You See It from Here? 62
Shell Variables 63
The Naming of Variables 65
Parameter Expansion 65
Positional Parameters 72
Arrays 73
Summary 75
Exercises 76
Shell Functions 77
Definition Syntax 77
Compound Commands 79
Getting Results 80
Function Libraries 82
Sample Script 82
Summary 84
Exercises 84
String Manipulation 85
Concatenation 85
Processing Character by Character 87
Case Conversion 88
Comparing Contents Without Regard to Case 90
Check for Valid Variable Name 91
Insert One String into Another 92
Overlay 92
Trim Unwanted Characters 93
Index 95
Summary 96
Exercises 96
File Operations and Commands 97
Reading a File 97
External Commands 99
Regular Expressions 103
File Name Expansion Options 107
Summary 112
Exercises 113
Reserved Words and Builtin Commands 114
help, Display Information About Builtin Commands 114
time, Print Time Taken for Execution of a Command 115
read, Read a Line from an Input Stream 116
type, Display Information About Commands 123
builtin, Execute a Builtin Command 125
command, Execute a Command or Display Information About Commands 125
pwd, Print the Current Working Directory 125
Deprecated Builtins 126
Dynamically Loadable Builtins 126
Summary 127
Exercises 128
Writing Bug-Free Scripts and Debugging the Rest 129
Prevention Is Better Than Cure 129
Debugging a Script 136
Summary 139
Exercises 139
Programming for the Command Line 140
Manipulating the Directory Stack 140
Filesystem Functions 144
Miscellaneous Functions 147
Managing Man Pages 148
Games 149
Summary 155
Exercises 155
Runtime Configuration 156
Defining Variables 156
Command-Line Options and Arguments 156
Menus 157
Q& A Dialogue
Configuration Files 158
Scripts with Several Names 159
Environment Variables 161
All Together Now 161
Summary 171
Exercises 171
Data Processing 172
Arrays 172
Data File Formats 186
Summary 191
Exercises 192
Scripting the Screen 193
Teletypewriter vs. Canvas 193
Stretching the Canvas 194
Priming the Canvas 195
Moving the Cursor 195
Changing Rendition Modes and Colors 196
Placing a Block of Text on the Screen 197
Scrolling Text 200
Rolling Dice 201
Summary 203
Exercises 203
Entry-Level Programming 204
Single-Key Entry 204
History in Scripts 210
Sanity Checking 211
Form Entry 212
Reading the Mouse 213
Summary 217
Exercises 217
Shell Variables 218
BASH 218
BASHPID 218
BASH_ALIASES 218
BASH_ARGC 218
BASH_ARGV 219
BASH_CMDS 219
BASH_COMMAND 219
BASH_EXECUTION_STRING 219
BASH_LINENO 219
BASH_REMATCH 219
BASH_SOURCE 220
BASH_SUBSHELL 220
BASH_VERSINFO 220
BASH_VERSION 220
COMP_CWORD 220
COMP_KEY 220
COMP_LINE 221
COMP_POINT 221
COMP_TYPE 221
COMP_WORDBREAKS 221
COMP_WORDS 221
DIRSTACK 221
EUID 222
FUNCNAME 222
GROUPS 222
HISTCMD 222
HOSTNAME 222
HOSTTYPE 222
LINENO 223
MACHTYPE 223
OLDPWD 223
OPTARG 223
OPTIND 223
OSTYPE 223
PIPESTATUS 223
PPID 224
PWD 224
RANDOM 224
REPLY 224
SECONDS 224
SHELLOPTS 224
SHLVL 224
UID 225
BASH_ENV 225
CDPATH 225
COLUMNS 225
COMPREPLY 225
EMACS 225
FCEDIT 225
FIGNORE 226
GLOBIGNORE 226
HISTCONTROL 226
HISTFILE 226
HISTFILESIZE 226
HISTIGNORE 227
HISTSIZE 227
HISTTIMEFORMAT 227
HOME 227
HOSTFILE 227
IFS 228
IGNOREEOF 228
INPUTRC 228
LANG 228
LC_ALL 228
LC_COLLATE 228
LC_CTYPE 228
LC_MESSAGES 229
LC_NUMERIC 229
LINES 229
MAIL 229
MAILCHECK 229
MAILPATH 229
OPTERR 230
PATH 230
POSIXLY_CORRECT 230
PROMPT_COMMAND 230
PROMPT_DIRTRIM 230
PS1 230
PS2 231
PS3 231
PS4 231
SHELL 231
TIMEFORMAT 231
TMOUT 232
TMPDIR 232
auto_resume 232
histchars 232
Index 233

Erscheint lt. Verlag 5.12.2009
Zusatzinfo 350 p.
Verlagsort Berkeley
Sprache englisch
Themenwelt Mathematik / Informatik Informatik Programmiersprachen / -werkzeuge
Mathematik / Informatik Informatik Theorie / Studium
Schlagworte C programming language • Debugging • Linux • Processing • programming • Programming language
ISBN-10 1-4302-1998-X / 143021998X
ISBN-13 978-1-4302-1998-9 / 9781430219989
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 1,2 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich
Das umfassende Handbuch

von Johannes Ernesti; Peter Kaiser

eBook Download (2023)
Rheinwerk Computing (Verlag)
44,90
Das Handbuch für Webentwickler

von Philip Ackermann

eBook Download (2023)
Rheinwerk Computing (Verlag)
49,90