Shell Scripting

Hamish Whittal

Legal Notice

2005-01-25 22:35:38

Revision History
Revision 0.0.1 01/NOV/2004
Initial version

Table of Contents

1. Tour de Shell Scripting
Introduction to the Course structure
Adage 1.0:
What you will need
What is Shell Scripting
Introduction to info, man and the whatis database
info pages
Man Pages
The whatis Database
Revising some Basic Commands
The who command
The w Command
The "date" command
The 'echo' command
File Commands
System Commands
stdin, stdout, stderr
stdin
stdout
Using stdin and stdout simultaneously
Appending to a file
stderr
stdout, stderr and using the ampersand (&)
Exercises:
Unamed Pipes
2. The Shell
Introduction
What is the login shell?
Exercises
The job of the shell
Command Interpreter
Allows for variables to be set
I/O redirection
Pipelines
Customising your environment
Conclusion:
3. Regular Expressions
Introduction
What are regular expressions?
The fullstop
Let's explore "sed" syntax
Square brackets ( [ ] ), the caret ( ^ ) and the dollar ( $ )
Using sed and pipes
The splat (asterisk) ( * )
The plus operator ( + )
Returning from detour to our discussion on curly braces …
RE's are greedy for matching patterns
Placeholders and word boundaries
Word boundaries ( < and > ) - a formal explanation
The tr command
The cut command
First Example in stages:
Second Example in stages:
Third example in stages
Exercises:
The paste command
The uniq command
The Sort command
The grep command
grep, egrep and fgrep
Exercises:
Challenge sequence:
4. Practically Shell Scripting
Section Techniques to use when writing, saving and executing Shell Scripts
Detour: File Extension labels
Comments in scripts
Variables
Shebang or hashpling #!
Exit
Null and unset variables
Variable Expansion
Environmental vs shell variables
Arithmetic in the shell
Examples
Exercises:
5. Using Quotation marks in the Shell
Introduction
Single Quotes or "ticks"
Exercises:
Double Quotes
Exercises
Backticks
Exercises:
Shell Arithmetic's with expr and back quotes
Another tip when using quotation marks
6. So, you want an Argument?
Introduction
Positional Parameters 0 and 1 through 9
Exercises:
Challenge sequence:
Other arguments used with positional parameters
$# How many positional arguments have we got ?
$* - display all positional parameters
Using the "shift" command - for more than 9 positional parameters
Exit status of the previous command
7. Where to From Here?
Making Decisions
Testing for a true or false condition
Different types of tests
Logical Operators
Exercises:
Conditions in the shell
Using the "if" statement
The "if" "then" "else" statement
The "elif" statement
The "case" statement
Exercises
Challenge sequence:
Debugging your scripts
The NULL command
The || and && commands
Exercises:
8. Loops
Introduction
The "for" loop
while and until loops
getopts Using arguments and parameters
Exercises:
9. User input to a script
Introduction
The read command
Presenting the output
The echo command
The printf command
10. Additional Information
The shell environmental variables pertaining to scripting
The Source command
Exercises:
the exec command
Other methods of executing a script or a series of commands
11. Positional parameters & variables re-visited
Introduction
PARAM:-value
PARAM:=value
${param:+value}
?${variable%pattern}
MAGIC%%r*a
variable#pattern
variable:OFFSET:LENGTH
#variable
Re-assigning parameters with set
Explaining the default field separator field - IFS
Setting variables as "readonly"
Exercises:
Challenge sequences:
12. Bits and pieces - tying up the loose ends
The eval command
Running commands in the background using &
Traps and signals
Signals
Traps
Exercises:
File descriptors re-visited
Exercises
Here documents
Exercises
Functions
Exercises:
Challenge sequence
A. Writing html pages
B. An introduction to dialog
C. A Comparisson of bash, tsch and ksh
Index

List of Figures

1.1. The structure of the info pages
2.1. Parent- and sub-shells
6.1. Using Shift Command to access parameters
10.1. Parent- and sub-shells

List of Tables

1.1. Wildcards
1.2. Standard Input, Standard Output and Standard Error