Pro Bash Programming, Scripting the GNU Linux, shell, Apress

Pro Bash Programming, Scripting the GNU Linux, shell, experts voice in linux
Pro Bash Programming, Scripting the GNU Linux, shell
Experts voice in linux
Chris F.A. Johnson

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 languageDon’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. I used to tell people that I did most of my programming in the shell but switched to C for anything that needed the extra speed. It has been several years since I have needed to use C, so I no longer mention it. I do everything in the shell.
A shell script is as much a program as anything written in C, Python, or any other language. 
Just because shell scripts are easier to write doesn’t mean they should take a backseat to compiled programs or other scripting languages. I use the terms script and program interchangeably when referring to tasks written in the shell.

Contents At A Glace

■About the Author................................................................................ xvi
■About the Technical Reviewer .......................................................... xvii
■Introduction ....................................................................................... xix
■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..........................................................67
■Chapter 8: File Operations and Commands ......................................79
■Chapter 9: Reserved Words and Builtin Commands .........................97
■Chapter 10: Writing Bug-Free Scripts and Debugging the Rest........113
■Chapter 11: Programming for the Command Line ...........................125
■Chapter 12: Runtime Configuration .................................................141
■Chapter 13: Data Processing .............................................................157
■Chapter 14: Scripting the Screen........................................................179
■Chapter 15: Entry-Level Programming ..............................................191
■Appendix: Shell Variables...................................................................205
■Index....................................................................................................221

Contents
■About the Author
■About the Technical Reviewer
■Introduction
■Chapter 1: Hello, World! Your First Shell Program
The Code
The File 
The Naming of Scripts
Selecting a Directory for the Script 
Creating the File and Running the Script
Choosing and Using a Text Editor
Building a Better “Hello, World!” 
Summary 
Commands
Concepts
Variables
Exercises
■Chapter 2: Input, Output, and Throughput 
Parameter and Variables 
Positional Parameters 
Special *@#0$?_!- Parameters
Variables
Arguments and Options
echo, and Why You Should Avoid It 
printf: Formatting and Printing Data 
Escape Sequences
Format Specifiers 
Width Specification
Printing to a Variable 
Line Continuation
Standard Input/Output Streams and Redirection
Redirection: >, >>, and <
Reading Input
Pipelines 
Command Substitution 
Summary
Commands
Concepts
Exercises 
■Chapter 3: Looping and Branching
Exit Status
Testing an Expression
test, aka [ ... ] 
[[ ... ]]: Evaluate an Expression
(( ... )): Evaluate an Arithmetic Expression
Lists 
Conditional execution
if 
Conditional Operators, && and || 
case
Looping 
while
until
for
break
continue
Summary
Commands
Concepts
Exercises 
■Chapter 4: Command-Line Parsing and Expansion
Quoting 
Brace Expansion
Tilde Expansion
Parameter and Variable Expansion
Arithmetic Expansion
Command Substitution
Word Splitting 
Pathname Expansion
Process Substitution
Parsing Options
Summary 
Commands
Exercises 
■Chapter 5: Parameters and Variables
The Scope of a Variable: Can You See It from Here?
Shell Variables
The Naming of Variables
Parameter Expansion
Bourne Shell
POSIX Shell 
Bash
Bash-4.0
Positional Parameters
Arrays 
Integer-Indexed Arrays 
Associative Arrays
Summary
Commands
Concepts
Exercises
■Chapter 6: Shell Functions
Definition Syntax
Compound Commands
Getting Results
Set Different Exit Codes
Print the Result
Place Results in One or More Variables
Function Libraries 
Using Functions from Libraries
Sample Script
Summary 
Commands
Exercises 
■Chapter 7: String Manipulation
Concatenation
Repeat Character to a Given Length
Processing Character by Character 
Reversal
Case Conversion 
Comparing Contents Without Regard to Case
Check for Valid Variable Name 
Insert One String into Another 
Examples
Overlay
Examples 
Trim Unwanted Characters 
Examples
Index 
Summary
Commands
Functions
Exercises 
■Chapter 8: File Operations and Commands
Reading a File
External Commands.
cat
head
touch
ls
cut
wc
Regular Expressions
grep 
sed
awk
File Name Expansion Options
nullglob
failglob
dotglob
extglob
nocaseglob 
globstar 
Summary
Shell Options
External Commands
Exercises
■Chapter 9: Reserved Words and Builtin Commands
help, Display Information About Builtin Commands 
time, Print Time Taken for Execution of a Command 
read, Read a Line from an Input Stream
-r, Read Backslashes Literally
-e, Get Input with the readline Library
-a, Read Words into an Array 
-d DELIM, Read Until DELIM Instead of a Newline
-n NUM, Read a Maximum of NUM Characters
-s, Do Not Echo Input Coming from a Terminal
-p PROMPT:, Output PROMPT Without a Trailing Newline 
-t TIMEOUT, Only Wait TIMEOUT Seconds for Complete Input
-u FD: Read from File Descriptor FD Instead of the Standard Input 
-i TEXT, Use TEXT as the Initial Text for readline
eval, Expand Arguments and Execute Resulting Command
Poor Man’s Arrays
Setting Multiple Variables from One Command
type, Display Information About Commands
builtin, Execute a Builtin Command
command, Execute a Command or Display Information About Commands
pwd, Print the Current Working Directory
unalias, Remove One or More Aliases 
Deprecated Builtins
Dynamically Loadable Builtins
Summary
Commands and Reserved Words
Deprecated Commands
Exercises
■Chapter 10: Writing Bug-Free Scripts and Debugging the Rest
Prevention Is Better Than Cure
Structure Your Programs
Document Your Code
Format Your Code Consistently 
The K.I.S.S. Principle 
Test As You Go
Debugging a Script
Summary 
Exercises
■Chapter 11: Programming for the Command Line
Manipulating the Directory Stack
cd
pd
cdm
menu
Filesystem Functions
l
lsr
cp, mv
md
Miscellaneous Functions
pr1 
calc
Managing Man Pages
sman
sus
k
Games
The Fifteen Puzzle
Summary
Exercises 
■Chapter 12: Runtime Configuration
Defining Variables
Command-Line Options and Arguments
Menus
Q&A Dialogue
Configuration Files
Scripts with Several Names
Environment Variables
All Together Now
## Script Information
## Default Configuration
## Screen Variables
## Function Definitions
## Parse Command-Line Options
## Bits and Pieces
Summary
Exercises
■Chapter 13: Data Processing
Arrays 
Holes in an Indexed Array
Using an Array for Sorting 
Two-Dimensional Grids
Data File Formats
Line-Based Records 
Block File Formats
Summary 
Exercises
■Chapter 14: Scripting the Screen
Teletypewriter vs. Canvas
Stretching the Canvas
CSI: Command Sequence Introducer
Priming the Canvas
Moving the Cursor
Changing Rendition Modes and Colors
Placing a Block of Text on the Screen
Scrolling Text 
Rolling Dice
Summary 
Exercises 
■Chapter 15: Entry-Level Programming
Single-Key Entry 
Function Library, key-funcs
History in Scripts
Sanity Checking
Form Entry 
Reading the Mouse 
Summary
Exercises
■Appendix: Shell Variables
BASH
BASHPID
BASH_ALIASES 
BASH_ARGC
BASH_ARGV
BASH_CMDS
BASH_COMMAND 
BASH_EXECUTION_STRING
BASH_LINENO
BASH_REMATCH
BASH_SOURCE
BASH_SUBSHELL
BASH_VERSINFO
BASH_VERSION
COMP_CWORD
COMP_KEY
COMP_LINE
COMP_POINT
COMP_TYPE
COMP_WORDBREAKS
COMP_WORDS
DIRSTACK
EUID 
FUNCNAME
GROUPS 
HISTCMD
HOSTNAME
HOSTTYPE
LINENO
MACHTYPE
OLDPWD
OPTARG
OPTIND
OSTYPE
PIPESTATUS
PPID 
PWD
RANDOM 
REPLY
SECONDS
SHELLOPTS
SHLVL
UID 
BASH_ENV
CDPATH
COLUMNS
COMPREPLY
EMACS
FCEDIT
FIGNORE
GLOBIGNORE
HISTCONTROL
HISTFILE
HISTFILESIZE
HISTIGNORE
HISTSIZE 
HISTTIMEFORMAT
HOME
HOSTFILE 
IFS
IGNOREEOF 
INPUTRC
LANG
LC_ALL
LC_COLLATE 
LC_CTYPE 
LC_MESSAGES
LC_NUMERIC
LINES 
MAI
MAILCHECK
MAILPATH
OPTERR
PATH
POSIXLY_CORRECT
PROMPT_COMMAND
PROMPT_DIRTRIM
PS1
PS2
PS3
PS4
SHELL
TIMEFORMAT 
TMOUT 
TMPDIR
auto_resume
histchars
■Index

What’s in the Book?
From writing your first program to using the mouse in your scripts, this book runs the gamut from
simple to complex and from the obvious to the obscure. It covers every technique you will need to write efficient shell programs.
Chapter 1, Hello, World! Your First Shell Program, presents the traditional first program in any
language. It prints “Hello, World!”
The chapter discusses how to write the script, what to name it, and where to put it.
Chapter 2, Input, Output, and Throughput, demonstrates output using the echo and printf
commands and introduces the read command for input.
It also examines redirecting both input and output and connecting commands with pipelines.
Chapter 3, Looping and Branching, explains the looping statements, for, while, and until; the
branching statement if; and the conditional operators && and ||.
Chapter 4, Command-Line Parsing and Expansion, describes how the shell parses a command
line, from word splitting to parameter expansion.
Chapter 5, Variables and Parameters, covers all the possibilities of parameters and variables,
from scalar variables to associative arrays and from default substitution to search and replace.
Chapter 6, Shell Functions, delves into the syntax of function definitions and defines a number
of useful routines.
Chapter 7, String Manipulation, contains a number of functions for dicing and splicing strings.
Chapter 8, File Operations and Commands, uses more external commands than the rest of the book put together.  That’s because looping through a large file with the shell is painfully slow, and the Unix utilities are very efficient.  This chapter also tells you when not to use those utilities.
Chapter 9, Reserved Words and Builtin Commands, looks at a number of commands that are built into the shell itself.
Chapter 10, Writing Bug-Free Scripts and Debugging the Rest, takes a buggy script and takes
you step-by-step through fixing it, as well as showing you how to prevent bugs in the first place.
Chapter 11, Programming for the Command Line, is for those people who spend a lot of time at
the command prompt. These programs and functions reduce the typing to a minimum.
Chapter 12, Runtime configuration, describes seven methods of altering a program’s runtime
behavior.
Chapter 13, Data Processing, deals with manipulating different types of data in the shell.
Chapter 14, Scripting the Screen, shows you how to write to the screen as if it were a canvas
rather than a teletypewriter.
Chapter 15, Entry-Level Programming, presents techniques for getting input from a user, both
using single keypresses and entering and editing a line of text. It concludes with routines for using the mouse in shell scripts.
The appendix lists all the variables used by bash with a description of each.

 Screenshot 

Pro Bash Programming, Scripting the GNU Linux, shell, experts voice in linux, Apress

Purchase Now !
Just with Paypal



Product details
 Price
 File Size
 5,340 KB
 Pages
 257 p
 File Type
 PDF format
 ISBN-13
 978-1-4302-1998-9
 Copyright
 2009 by Chris F.A. Johnson  
●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●


═════ ═════

Previous Post Next Post