Shell Programming
Private Training

Course Summary

Shell programs, or scripts, are the means by which a UNIX shell is used as a programming language. UNIX commands and shell language control constructs are entered into a file by the programmer, then the file is executed as a command and interpreted just as if the commands had been typed on the shell command line. Thus, shell scripts provide a way to automate commonly executed groups of commands – but shell scripts can do much more than this. Although many simple tasks are automated with small scripts, large scripts hundreds of lines long are very common. These larger scripts are written by system administrators, database administrators, testers, utility programmers, and others to create utilities that are largely composed of powerful UNIX commands, such as find, sed, awk, and hundreds of others.

In this course, students learn to read, write, and debug Korn shell scripts. Back at work they can greatly increase productivity by automating repetitive tasks (for themselves or others), and by creating specifically tailored utilities designed to meet their precise needs. Students will read and write many shell scripts in this class, which will additionally increase their overall UNIX knowledge and skills.

Audience
UNIX users, programmers, and system administrators.
Course Length
3 Days
Prerequisites
Fundamentals of UNIX.

Objectives

Explain the purpose of shell programs.

Recognize applications for shell programs.

Design and write shell programs of moderate complexity, using variables, special variables, flow control mechanisms, operators, arithmetic, and functions.

Debug shell programs using several different debugging techniques.

Write “real-time” shell scripts that respond to and handle asynchronous events with the trap command.

Manage multiple concurrent processes to achieve higher utilization of UNIX.

Detailed Outline

  1. Course Introduction
    • Course Overview
    • Objectives
    • Suggested References
  2. UNIX Processes
    • What is a Process?
    • Process Structure
    • The ps Utility
    • Options to the ps Utility
    • Background Commands (&)
    • Killing Background Processes
    • Redirecting the Standard Error
  3. Getting Started
    • What is a Shell?
    • Running Scripts
    • Specifying the Script’s Interpreter
    • The PATH Environment Variable
    • Sub-shells
  4. Variables
    • Shell Variables
    • The read Command
    • The export Command
    • The Shell Environment
    • Variable Substitution
    • Command Substitution
  5. The Login Process
    • The Login Process
    • The System Profile Script
    • Your .profile Script
    • The . Command
  6. Conditional Statements
    • The Exit Status of Commands
    • Command Line Examples
    • The test Command
    • The if-then-else Construct
    • The elif Construct
    • case Statements
  7. Loops
    • The for Loop
    • The while Loop
    • break and continue
    • Reading Lines From Files
    • Using Arrays with Loops
  8. Special Variables
    • $$ – PID of Shell
    • Command-Line Arguments
    • $# – Number of Arguments
    • $* – All Arguments
    • The shift Command
    • The set Command
    • Getting Options
  9. Quoting Mechanisms
    • Single vs. Double Quotes
    • What is a Here Document?
    • Using a Here Document
    • Here Document Quoting
    • Ignoring Leading Tabs
  10. Functions
    • Shell Functions
    • Passing Arguments to Functions
    • Returning Values from Functions
    • Function Libraries
  11. Advanced Programming
    • Shell Arithmetic
    • The select Statement
    • Terminal Independence in Scripts
    • The eval Command
  12. Debugging Techniques
    • Using echo
    • Using Standard Error
    • Options for Debugging
    • Script Tracing
    • Conditional Debugging
  13. Shell IPC
    • Co-processes
    • The print and read Commands
    • Signals
    • The trap Command
    • Named Pipes
    • The wait Command