The System Administrator’s Guide to Bash Scripting

Course

Intro Video

Photo of Terrence Cox

Terrence Cox

Senior Vice President of Content

A veteran of twenty years in Information Technology in a variety of roles. He has worked in development, security and infrastructure well before they merged into what we now call DevOps. He provides training in Linux, VMWare, DevOps (Ansible, Jenkins, etc) as well as containers and AWS topics. He now leads the Training Architects to produce the courses and content we all know and love!

Length

07:28:37

Difficulty

Intermediate

Course Details

In this course, you will develop all of the skills needed to write effective and complex shell scripts that can automate mundane tasks and complex implementations. We will walk you through all of the major components and key words in shell scripting (loops, conditionals, functions, error handling, traps, signals and more) so you can effectively write your scripts to handle any situation. You will find a comprehensive study guide, an instructor note card set and a full SIXTEEN PAGES of sample shell scripts developed during the course, all available for your download and reference.

Syllabus

Introduction

Syllabus

00:08:02

Lesson Description:

We will present the topics to cover in the course.

History of Bash

00:06:01

Lesson Description:

A brief history of the Bash Shell and what came before.

Core Concepts

Bash Files - .bash_profile

00:07:32

Lesson Description:

Talking about .bash_profile

Bash Files - .bashrc

00:07:02

Lesson Description:

Talking about .bashrc

Bash Files - .bash_history

00:07:08

Lesson Description:

Talking about .bash_history

Bash Files - .bash_logout

00:07:01

Lesson Description:

Talking about .bash_logout  

What Makes a File a Shell Script?

00:08:32

Lesson Description:

Common characterisitics and requirements for a file to be a script

Displaying Environment Variables in a Script

00:07:23

Lesson Description:

Introduce how to refer to existing environment variables in a script for display

Using Variables on the Command Line

00:10:05

Lesson Description:

How to set and use variables in commands  

Setting and Using Variables in Scripts

00:09:06

Lesson Description:

Now that we know how to set and use variables at the command line, translate to our shell scripts  

Comment Types and Structure

00:08:34

Lesson Description:

How do we create private and public comments in our scripts.

Command Substitution

00:10:17

Lesson Description:

We can assign the output of commands to a variable, learn how to use that in our scripts to provide dynamic information.

Exit Status

00:07:53

Lesson Description:

Command and script exit status

Arithmetic Operations

00:07:53

Lesson Description:

Command line and script arithmetic operations

Global and Local Environment Variables

00:06:34

Lesson Description:

Show global environment variables and differentiate from local shell instance variables.

Special Characters - Quotes and Escapes

00:07:54

Lesson Description:

There are a number of special characters at the command line and within scripts, let's learn about quotes, single quotes, escapes and back ticks.

Using /dev/null

00:06:37

Lesson Description:

How we can use this special file/directory to hide output when needed

The Read Statement

00:08:07

Lesson Description:

Up to now, we have provided our own input. Using the 'read' statement, we can prompt our users for it.

Shell Expansion

00:08:05

Lesson Description:

Learn how to use brackets and other shortcuts to save time and reference important information quickly.

Types of Variables (Implicit vs. Explicit Definition)

00:11:19

Lesson Description:

Learn the difference and how to declare them.

Arrays

00:12:22

Lesson Description:

Learn what they are, how to declare and use them.

Exercise: Display Environment Variables

00:30:00

Exercise: Setting and Using Variables in Scripts

00:30:00

Exercise: Using Comments

00:30:00

Exercise: Using /dev/null

00:30:00

Exercise: Exit Status Types

00:30:00

Exercise: Evaluating Arithmetic Expressions

00:30:00

Exercise: Command Substitution

00:30:00

Exercise: Interactive Scripting

00:30:00

Exercise: Using Arrays

00:30:00

Conditional Statements

Passing Variables to Scripts at the Command Line

00:09:00

Lesson Description:

Learn another way to get information into our scripts.

The If Statement

00:16:49

Lesson Description:

Learn how to use 'if' to make decisions about how your script executes.

If/Then/Else

00:16:16

Lesson Description:

Learn how we can use multiple and nested tests to further lend flexibility to our scripts.

For Loop

00:09:54

Lesson Description:

Automatically iterate through lists depending on their length.

Case Statement

00:08:43

Lesson Description:

We will show how a few lines of code can test for a large array of options.

While Loop

00:08:25

Lesson Description:

Learn how to control exactly how many times you iterate through a block of code.

Execution Operators (&& and ||)

00:09:30

Lesson Description:

Learn how to use these shortcuts to quickly control how your script behaves

Exercise: Passing Variables to Scripts at the Command Line

00:30:00

Exercise: The If Statement

00:30:00

Exercise: Using If/Then/Else

00:30:00

Exercise: The For Statement

00:30:00

Exercise: Using the Case Statement

00:30:00

Exercise: While Looping

00:30:00

Input and Output

Reading Files

00:07:06

Lesson Description:

Simple method for reading non-binary files and processing the contents.

File Descriptors and Handles

00:08:33

Lesson Description:

Learn the more "development centric" way of opening our files for reading and writing by assigning them a descriptor for reference.

IFS and Delimiting

00:09:10

Lesson Description:

This important environment variable will help us process file information once read in using one of the most common situation, delimiting. Example spacedelim.txt file: i7 4Ghz 32gb 4TBLesson Reference ifsdelim.sh : #!/bin/bash # delimiter example using IFS echo "Enter filename to parse: " read FILE echo "Enter the Delimiter: " read DELIM IFS="read -r $DELIM" while read -r CPU MEMORY DISK; do echo "CPU: $CPU" echo "Memory: $MEMORY" echo "Disk: $DISK" done <"$FILE"

Traps and Signals

00:11:35

Lesson Description:

Learn how to 'trap' or capture events and redirect how the script is affected by common signals and how they are generated.

Exercise: Reading Files

00:30:00

Exercise: File Descriptors and Handles

00:30:00

Exercise: IFS and Delimiting

00:30:00

Exercise: Traps and Signals

00:30:00

Debugging and Error Handling

Debugging Your Script

00:08:45

Lesson Description:

How to debug your entire script or just portions of it.

Error Handling

00:10:10

Lesson Description:

Learn how to test for and react to script errors.

Exercise: Error Handling with Exit

00:30:00

Functions

Simple Functions

00:08:06

Lesson Description:

Learn what a function is and the very general definition of one and how to call it.

Structure of Functions in a Script

00:11:19

Lesson Description:

We need to demonstrate how to organize our scripts now that functions will be playing a part.

Variable Scope in Functions

00:09:47

Lesson Description:

We will talk about global and local scope of variables and how that applies to functions.  

Functions with Parameters

00:08:02

Lesson Description:

How can we pass parameters into our functions for processing?

Nested Functions

00:12:20

Lesson Description:

What are nested functions and how can we take advantage of them?

Function Return and Exit

00:14:09

Lesson Description:

Functions can also return information that we can react to and help us with better error handling.

Exercise: Creating a Function

00:30:00

Exercise: Variable Scope in Functions

00:30:00

Exercise: Functions with Parameters

00:30:00

Exercise: Nested Functions

00:30:00

Samples/Use Cases

Using the InfoBox

00:12:38

Lesson Description:

Looking at displaying a pop up notification for user interaction

Display a MessageBox for Confirmation

00:12:00

Lesson Description:

Pop up a message box to confirm execution

Advanced UI - Building a Menu System

00:12:20

Lesson Description:

Build a menu system to interact with users and allow choices

The InputBox

00:08:20

Lesson Description:

Get direct information from users and determine what to do based on it

Overriding Events

00:10:45

Lesson Description:

Learn how to override a system event so we can do clean up and provide custom messages before we exit

Quickly Checking Command Line Parameters

00:07:23

Lesson Description:

Two easy ways to check that your script receives the correct number of parameters or provide a usage message if not.

Building a Document Generator

00:09:49

Lesson Description:

In this example, we are going to build a self-executing document reader using the output of ALL scripts we have written in this course.

Exercise: Simple Infobox

00:30:00

Exercise: Displaying a Message Box

00:30:00

Exercise: A User Input Box

00:30:00

Exercise: Creating a Menu

00:30:00

Conclusion

Conclusion and Next Steps

00:03:54

Lesson Description:

Talk a little about what we have done, where to go from here and how to get there.