UNIX Shell Programming (2 days)

UNIX Shell Programming course outline

Synopsis

While sexier languages like Perl and Java have been getting the recent headlines, there has never been a substitute for learning how to program properly in the shell, particularly for UNIX administrators whose daily duties may involve examining or modifying the numerous shell scripts that support the operating system.

This course covers all aspects of Korn shell programming, from creating and executing simple scripts to incorporating variables, conditional execution, loops, I/O redirection, shell functions and advanced process control.

Synopsis This course is designed for anyone with a working knowledge of UNIX, and is especially useful for UNIX system administrators who need to understand and modify the numerous administrative scripts on their host.
Prequisites

Attendees should already have a working knowledge of UNIX, including an understanding of the basic file and directory commands and UNIX file permissions. Some administrative experience would be an asset but is not required.

Lab exercises Students will write numerous shell scripts to reinforce the major concepts covered in this course, and will be able to take away both their own solutions and those provided as model solutions by the instructor.

1. What is shell programming?

  • What is a shell script?
  • Applications for shell scripts
  • Why learn shell scripting?
  • Some sample scripts
  • The "brains-on" approach to shell programming

2. A review of the shell

  • The layered architecture of UNIX
  • The variety of shells
  • The features of the shell
  • The login sequence and the startup files

3. Creating and running shell scripts

  • The choice of shells
  • The general structure of a shell script
  • Shell script execution
  • Debugging shell scripts
  • Script housekeeping techniques
  • Executing scripts in the current shell

4. The basic building blocks

  • Variables and their applications
  • Accessing command-line arguments
  • Some simple I/O
  • Scripts and redirection
  • Quotes
  • More on the command-line arguments

5. Conditional execution

  • The components of conditional constructs
  • The basic if construct
  • Command evaluation and its variations
  • String comparisons
  • File tests
  • Arithmetic and integer comparisons
  • Exit codes
  • The case and select constructs
  • String and pattern operators

6. Loops

  • The shell loop constructs
  • The while loop and its variations
  • The for loop
  • Loops and redirection
  • Input and the IFS
  • Command-line parsing with getopts
  • Arrays

7. Advanced I/O techniques

  • A review of basic I/O
  • Prompting for user input
  • Print escape sequences
  • File-oriented I/O
  • Writing "here" documents
  • The eval commmand

8. Writing shell functions

  • The purpose of shell functions
  • Function definition
  • Passing parameters to functions
  • Returning values from functions
  • Functions and variable scope
  • A couple of applications
  • Building your own function library

9. Shell process control

  • The script execution life cycle
  • Process IDs and temporary files
  • Script termination and signals
  • Signal handling with the trap command
  • Traps and functions

10. Script execution and debugging techniques

  • Shell debugging options
  • Writing inherently-debuggable scripts

11. Programming guidelines

  • A compendium of good advice