Tutorial10: Shell Scripting - Part 1

From CDOT Wiki
Revision as of 09:02, 14 July 2020 by Msaul (talk | contribs) (Using Variables in Shell Scripts)
Jump to: navigation, search


Main Objectives of this Practice Tutorial

  • Understand the process for planning prior to writing a shell script.
  • Understand the purpose of a she-bang line contained at the top of a shell script.
  • Setting permissions for a shell script and properly execute a shell script.
  • Understand and use environment and user-defined variables within a shell script.
  • Understand the purpose of control flow statements used with shell scripts.
  • Use the test command to test various conditions.
  • Use the if logic statement and the for loop statement within shell scripts.

Tutorial Reference Material

Course Notes
Linux Command/Shortcut Reference
YouTube Videos
Course Notes:

Shell Scripting
  • Purpose
  • Planning a Shell Script
  • Executing a Shell Script


  • Environment
  • User Defined
Control Flow Statements
  • Purpose
  • test command
  • if statement
  • for loop
Brauer Instructional Videos:


A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter.
The various dialects of shell scripts are considered to be scripting languages.

Reference: https://en.wikipedia.org/wiki/Shell_script

Creating & Executing Shell Scripts

An IPSO Diagram (INPUT, PROCESSING, STORAGE, OUTPUT) can be used to map-out and then list the sequence of steps to assist when coding your shell script.

It is recommended to plan out on a piece of paper the purpose of the shell script.
You can do this by creating a simple IPSO diagram (stands for INPUT, PROCESSING, STORAGE, OUTPUT).

First, list the INPUTS into the script (eg. prompting user for data, reading data from file, etc), then listing the expected OUTPUTS from the script. You can then list the steps to process the INPUT to provide the OUTPUT (including file storage).

Once you have planned your shell script by listing the sequence of steps in your script, you need to create a file (using a text editor) that will contain your Linux commands.
NOTE: Avoid using filenames of already existing Linux Commands to avoid confusion. Using shell script filenames that include the file extension of the shell that the script will run within is recommended.

Using a Shebang Line

The shebang line must appear on the first line and at the beginning of the shell script.
If you are learning Bash scripting by reading other people’s code you might have noticed
that the first line in the scripts starts with the #! characters and the path to the Bash interpreter.

This sequence of characters (#!) is called shebang and is used to tell the operating system
which interpreter to use to parse the rest of the file. Reference: https://linuxize.com/post/bash-shebang/

The shebang line must appear on the first line and at the beginning of the shell script,
otherwise, it will be treated as a regular comment and ignored.

Setting Permissions & Running a Shell Script

To run your shell script by name, you need to assign execute permissions for the user.
To run the shell script, you can execute it using a relative, absolute, or relative-to-home pathname


chmod u+x myscript.bash

Using Variables in Shell Scripts


It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.� Reference: https://launchschool.com/books/ruby/read/variables
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.

Reference: https://launchschool.com/books/ruby/read/variables

Environment Variables

Shell environment variables shape the working environment whenever you are logged in Common shell. Some of these variables are displayed in the table below (you can issue the pipeline command set | more to view all variables

Using Control Flow Statements in Shell Scripts



In this section, you will learn how to ...

Perform the Following Steps:

  1. x

In the next investigation, you will ...


In this section, you will learn how to ...

Perform the Following Steps:

  1. x

In the next investigation, you will ...


In this section, you will learn how to ...

Perform the Following Steps:

  1. x


The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.

Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:


Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).

Review Questions:

  1. x
  2. x
  3. x
  4. x
  5. x
  6. x
  7. x
  8. x