Unix Shell Programming   «Prev  Next»
Lesson 1

UNIX Shell Programming

This course introduces you to the advanced features of the UNIX Bourne shell and teaches you how to apply them effectively. You will also learn new UNIX commands like sed and awk, which are commonly used in shell scripts.
This is the second in a two-part series that teaches UNIX shell programming. This course teaches you to use advanced features of the shell including embedded command execution, command line argument processing, debugging techniques, use of sed to edit files, and use of awk to format command output.
Along the way, you will work on a series of your own shell script projects that will let you put your new skills to work in common shell script tasks in an actual UNIX environment.
What are some of the advanced features of the UNIX Bourne shell?
The Bourne shell (sh) is one of the original Unix shells. It was developed by Stephen Bourne at AT&T Bell Labs in the late 1970s. Despite its age, the Bourne shell is still widely used today due to its simplicity, efficiency, and compatibility across Unix and Unix-like systems.
Here are some of the advanced features of the Bourne shell:
  1. Scripting Language: The Bourne shell includes a scripting language. This makes it possible to automate tasks, run scripts at specified times, and build complex pipelines and redirections to handle data. The scripting capabilities of the Bourne shell have served as the foundation for many other Unix shell scripting languages.
  2. Command Substitution: The Bourne shell supports command substitution, which allows the output of a command to be used as an input for another command. This is done using the $(command) or legacy `command` syntax.
  3. Pipeline and Redirection: Bourne shell supports pipeline (|) and redirection (>, <, >v, <<, 2>, etc.) operations. A pipeline chains commands together so that the output from one command becomes the input to the next. Redirection can redirect output to a file or get input from a file.
  4. Environment Variables: The Bourne shell allows setting and accessing environment variables. These are dynamic-named values that can affect the way running processes will behave on a computer.
  5. Conditional Statements: The Bourne shell supports conditional statements (if, else, elif, case, etc.) and looping constructs (for, while, until). This allows for complex control flow in shell scripts.
  6. Functions: You can define functions in the Bourne shell, which can help structure shell scripts and reduce code redundancy.
  7. Job Control: While not as advanced as in some later shells, the Bourne shell offers basic job control. You can start processes in the background and bring them to the foreground.
  8. Parameter Expansion: The Bourne shell supports parameter and variable expansion. This means that a variable's value can be manipulated or used within strings.
  9. Command Exit Status: Every command returns an exit status (a number) when it finishes executing. This status can be used in scripts to check if a command executed successfully (0 indicates success, any other number indicates an error).
  10. Here Documents: Here Documents are a form of I/O redirection allowing the input of a command to come from a literal string or a file.

Although some of these features might seem basic today, the Bourne shell was groundbreaking when these features were introduced. Note that later shells like the Bourne Again shell (bash) and the Korn shell (ksh) have extended these capabilities and added many more features, but the basic principles remain the same.

Course Goals

Completing the course, you will be able to:
  1. Create a shell script that can add and remove lines from a text file
  2. Create a shell script that searches text files for a given pattern
  3. Create a shell script that prints a subset of columns from a text file
  4. Create a shell script that uses command line arguments and options
  5. Identify and debug common problems found in shell scripts
In this course, you will learn and practice UNIX shell programming skills using various types of interactive tools:
  1. Series of Images,
  2. Diagrams,
  3. Exercises and
  4. Quizzes
The next lesson explains the prerequisites to this course.