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:
- 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.
- 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.
- 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.
- 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.
- 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.
- Functions: You can define functions in the Bourne shell, which can help structure shell scripts and reduce code redundancy.
- 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.
- Parameter Expansion: The Bourne shell supports parameter and variable expansion. This means that a variable's value can be manipulated or used within strings.
- 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).
- 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:
- Create a shell script that can add and remove lines from a text file
- Create a shell script that searches text files for a given pattern
- Create a shell script that prints a subset of columns from a text file
- Create a shell script that uses command line arguments and options
- 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:
- Series of Images,
- Diagrams,
- Exercises and
- Quizzes
The next lesson explains the prerequisites to this course.