EEC Logo

Bash Shell Programming

Duration: 3 days

This course is 3 days long but for students without extensive command line experience, additional topics can be added to provide background. See the topics marked with Note, below.

Audience

Individuals requiring a mastery of the command line interface to the UNIX operating system. These individual are interested in automating common tasks, from processing log files or controlling boot time scripts to installing and deploying in-house applications. This includes system administrators, programmers, and power users.

Course Contents

  1. Concepts

    1. What is UNIX?

    2. What is Linux?

    3. Open Source vs. Free Software vs. Public Domain

    4. Linux Components

    5. A Historical Overview

    6. Linux Features

    7. Linux Programming Support

  2. UNIX/Linux Basics Note

    1. Accessing the System

    2. Issuing Commands

    3. Control Keys

    4. Command Syntax

    5. Online Documentation

    6. Changing Your Password

    7. Logging Out

  3. Using Files and Directories Note

    1. Characteristics of Linux Files

    2. File System Hierarchy

    3. Directories

    4. Working With Directories

    5. Listing Directory Contents

    6. More Directory Listing Examples

    7. Linux System Directories

    8. Special Files

    9. Manipulating Files

    10. Managing Files

    11. Permissions

    12. Permission Examples

    13. Permission-setting Shorthand

    14. The umask Command

  4. Bash Shell Basics

    1. Linux Components

    2. I/O Redirection

    3. Examples of Redirection

    4. Using Pipes

    5. Wildcards For Filenames

    6. Command Substitution

    7. Variable Substitution

    8. Introducing Shell Scripts

    9. Quoting To Prevent Interpretation

    10. Examples of Quoting

    11. Command Parsing Order

    12. Special Shell Scripts

  5. Process Management

    1. Process Structure

    2. Monitoring Running Processes

    3. Process Creation

    4. Shell Script Execution

    5. Process Priorities

    6. Modifying the NICE Bias

    7. Background Processes and nohup

    8. Killing Processes

  6. Regular Expressions Note

    1. Background

    2. The grep Command

    3. Simple metacharacters

    4. Advanced metacharacters

    5. Extended metacharacters

    6. The sed Command

    7. Greedy Matching in sed

    8. An Example of awk

  7. Data Tools, Part I Note

    1. tee

    2. The cat Command

    3. head and tail

    4. expand and unexpand

    5. cut and paste

    6. The find Command

  8. Bash Scripts: Background

    1. Why Shell Programming?

    2. Steps to Creating a Script

    3. Menu Building -- Example Using select

    4. Comments in Shell Scripts

    5. Using Single-Value Variables

    6. Working With Arrays

    7. Parameter Expansion Modifiers

    8. How to Correctly Display Error Messages

    9. Performing Arithmetic (let and (( )))

    10. Using expr For String Matching

    11. Interactive Scripts (the read Command)

  9. Bash Scripts: Flow Control

    1. The if Command

    2. Test Operations (test, [ ], and [[ ]])

    3. The while Loop

    4. The for Loop

    5. Changing the Script Parameters (set)

    6. Loop Control (break and continue)

    7. The case Command

    8. Exiting a Shell Script

    9. Menu Building -- Complete Example

  10. Bash Scripts: User Interaction (as time permits)

    1. Using getopts For Handling Command Line Options

    2. The trap Command

    3. Setting Runtime Options in the Bash Shell

  11. Bash Scripts: Efficiency (as time permits)

    1. Subshells and IO Redirection (( ), { }, and exec)

    2. Conditional Execution (&& and ||)

    3. Temporary Files With mktemp

    4. Floating Point Arithmetic

    5. HERE Documents

    6. Shell Functions

  12. Appendix -- Using Bash Interactively

    1. Bash Shell Background

    2. Automatic Configuration

    3. Command History

    4. Command Prompt Customization

    5. Command Line Editing

    6. Other Set Options

    7. Aliases (Command Macros)

    8. Environment Variables

    9. Example Startup Scripts

  13. Appendix -- Data Tools, Part II

    1. Using xargs for Efficiency

    2. Finding Differences -- diff

    3. Side-by-Side Differences -- sdiff

    4. Character Translation -- tr

    5. Sorting Text Files -- sort

    6. Relational Joins -- join

Note Optional topic. Students with very strong command line experience may not need these topics. Contact us for details.

Course Objectives

Upon completion of this course, the student will be able to write Bash Shell scripts using the following features and more:

Shell scripting is equal parts of running commands and processing the results generated by those commands. In it's normal configuration, this course covers the most important of the text processing commands that are frequently used in efficient shell scripts, and mixes in the built-in control functions of the shell to make them fast and effective. Each chapter includes discussion of when to use each feature, so that students gain an understanding of what works best in a particular situation. Best practices are described throughout the course with rules of thumb in each chapter. (Removing the optional topics may be feasible for very experienced students, but is not recommended.)

Instructional Technique

Students are invited to bring their current ideas and questions to the classroom for discussion. Case studies, lecture, group problem solving, and online laboratories will be used. Students will be encouraged to enhance their skills utilizing the techniques presented through classroom problem solving and controlled online workshops.

Prerequisites

Completion of an Introduction to UNIX/Linux or similar course, and six months of command line experience on a UNIX/UNIX-like operating system. See below for a quick quiz to determine if you're ready for this fast-paced course.

Programming skills are not required, but are helpful.

Quiz

Are you ready for this course? If you can't immediately answer the following questions (no more than ten seconds of time to think about each answer!), then you should consider taking an Introduction to UNIX/Linux course first and practicing your command line skills prior to attending this course.

  1. The ls command normally displays the contents of the directory provided on the command line. What option turns OFF the display of a directory's contents? (For example, if the -l option was included, the output would consist of one line of data with a "d" in column zero.)

  2. The cat command is normally used to display the contents of a file, but it can also add line numbers to the output, display tabs as ^I characters, add end-of-line markers, and other functions. Name at least two valid option letters to the cat command and describe what they do.

  3. The tail command is a staple of the UNIX environment. It can be executed with the -n option followed by a positive or negative number. What do these numbers represent and what is the difference between using a plus sign prefix and a minus sign prefix on the number?

  4. Describe what the following command does in 25 words or less (for this one you're allowed more than ten seconds!). You're allowed to guess at what data is being extracted from the ps output:

    ps -eaf | cut -c56- | tail -n +2 | sort -u

  5. Which of these command groups, if any, produce errors or meaningless results (you get ten seconds per command group):

    Group A:
        ps -eaf | sort -k 1.56
    Group B:
        ls -l /var/spool/at > /tmp/frank | wc -l
    Group C:
        echo "%r" > format
        date +$(cat format)
    Group D:
        echo "+%r" > format
        var=$(date $(cat format))

Answers

If you didn't get at least three of the above questions correct immediately, you're probably not ready for this course.

The answers are:

  1. -d and --directory cause the information in the directory inode itself to be displayed, not the inodes contained within the directory.

  2. -n, -t, -e, and -v are valid options (among others). These options add line numbers, display tabs as ^I characters, display end-on-line as $ characters, and cause other normally invisible characters to be printed (such as formfeed being printed as ^L), respectively.

  3. Number of lines. A positive number starts printing at a position that is counted forward from the beginning of the file and a negative number starts printing at a position that is counted backwards from the end of the file.

  4. Creates an alphabetical list of command lines running on the system (removing duplicates). This assumes that ps -f prints the command line beginning in column 56.

  5. A, C, and D are fine. B has the problem (it produces an error in some versions of the shell and prints 0 in other versions).