Tutorial10: Shell Scripting - Part 1
Contents
INTRODUCTION TO SHELL SCRIPTING
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
Variables |
Control Flow Statements
|
Brauer Instructional Videos: |
KEY CONCEPTS
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
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
If you are learning Bash scripting by reading other people’s code you might have noticedthat 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
Example:
chmod u+x myscript.bash
./myscript.bash
/home/username/myscript.bash
~/myscript.bash
Using Variables in Shell Scripts
Definition
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
(you can issue the pipeline command set | more to view all variables)
Placing a dollar sign ($) prior to the variable name will cause the variable to expand to the value contained in the variable.
User Defined Variables
User-defined variables are variables which can be created by the user and exist in the session. This means that no one can access user-defined variables that have been set by another user,
and when the session is closed these variables expire.
Reference: https://mariadb.com/kb/en/user-defined-variables/
Data can be stored and removed within a variable using an equal sign.
The read command can be used to prompt the user to enter data into a variable.
Refer to the diagram on the right-side to see how user-defined variables are assigned data.
Positional Parameters and Special Parameters
A positional parameter is a variable within a shell program; its value is set from an argument specified on the command line that invokes the program.Positional parameters are numbered and are referred to with a preceding "$": $1, $2, $3, and so on. The positional parameter $0 refers to either the name of shell where command was issued, or name of shell script being executed. If using positional parameters greater than 9, then you need to include number within braces.
Examples: echo ${10}, ls ${23}
The shift command can be used with positional parameters to shift positional parameters
to the left by one or more positions.
There are a few ways to assign values as positional parameters:
- Use the set command with the values as argument after the set command
- Run a shell script containing arguments
There are a group of special parameters that can be used for shell scripting.
A few of these special parameters and their purpose are displayed below:
$* , “$*” , "$@" , $# , $?
Refer to the diagram to the right for examples using positional and special parameters.
Using Control Flow Statements in Shell Scripts
Control Flow Statement are used to make your shell scripts more flexible and can adapt to changing situations.
The special parameter $? Is used to determine the exit status of the previously issued Linux command. The exit status will either display a zero (representing TRUE) or a non-zero number (representing FALSE). This can be used to determined if a Linux command was correctly or incorrectly executed.
The test Linux command is used to test conditions to see if they are TRUE (i.e. value zero) or FALSE (i.e. value non-zero) so they can be used with control flow statements to control the sequence of a shell script.
You CANNOT use the > or < symbols when using the test command since these are redirection symbols. Instead, you need to use options when performing numerical comparisons. Refer to the table below for test options and their purposes.
There are other comparison options that can be used with the test command such as testing to see if a regular file or directory pathname exists, or if the regular file pathname is –non-empty.
Refer to diagrams to the right involving the test command.
Logic Statements
A logic statement is used to determine which Linux commands to be executed based
on the result of a condition (i.e. TRUE (zero value) or FALSE (non-zero value)).
There are several logic statements, but we will just concentrate on the if statement.
if test condition then command(s) fi
Refer to the diagram relating to logic statements on the right side for an example.
Loop Statements
A loop statement is a series of steps or sequence of statements executed repeatedly zero or more times satisfying the given condition is satisfied.
Reference: https://www.chegg.com/homework-help/definitions/loop-statement-3
There are several loops, but we will look at the for loop using a list.
for item in list do command(s) done
Refer to the diagram relating to looping statements on the right side for an example.
INVESTIGATION 1: CREATING A SHELL SCRIPT
In this section, you will learn how to ...
Perform the Following Steps:
- x
In the next investigation, you will ...
INVESTIGATION 2: USING VARIABLES IN SHELL SCRIPTS
In this section, you will learn how to ...
Perform the Following Steps:
- x
In the next investigation, you will ...
INVESTIGATION 3: USING CONTROL FLOW STATEMENTS IN SHELL SCRIPTS
In this section, you will learn how to ...
Perform the Following Steps:
- x
LINUX PRACTICE QUESTIONS
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:
https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week10_practice.docx
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:
- x
- x
- x
- x
- x
- x
- x
- x