Week 9 Lecture Notes
Week 9 Lecture Notes
Week 9 Lecture Notes
WEEK 9 LESSON 1
PHOTOS AND ICONS USED IN THIS SLIDE SHOW ARE LICENSED UNDER CC BY-SA
LESSON 1 TOPICS
Shell Scripts
• Purpose
• Considerations When Creating Shell Scripts
• Creating Shell Scripts
Shell Variables
• Purpose
• Environment Variables
• User Defined Variables
Definition
Reference: https://en.wikipedia.org/wiki/Shell_script
SHELL SCRIPTING
Examples:
clean-directory.bash
copy-directory-structure.sh
SHELL SCRIPTING
Reference: https://linuxize.com/post/bash-shebang/
SHELL SCRIPTING
You can use the which command to determine the full pathname
of the shell. For example: which bash
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.
SHELL SCRIPTING
Example:
Example:
You can run a shell script that does not have permissions by issuing
Example: the shell command with the shell script as an argument.
/home/username/myscript.bash
You can add the directory that contains the shell script so it can be issued
~/myscript.bash by just name and not pathname by adding the directory pathname into the
PATH environment variable
Example:
PATH=$PATH:directory-pathname
SHELL SCRIPTING
Task1:
Create a Bash Shell script to clear the screen and then display all
users that are logged onto the system.
Task2:
Create a Bash Shell script to clear the screen, prompt the user for
their full name, and then display the text message:
Hello, your name is (your full name)
SHELL SCRIPTING
Using Variables
Reference: https://launchschool.com/books/ruby/read/variables
SHELL SCRIPTING
Using 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)
Environment Variables
Examples:
echo “My current location is:
$PWD” who | grep $USER
echo $HOST
SHELL SCRIPTING
Reference: https://mariadb.com/kb/en/user-defined-variables/
SHELL SCRIPTING
name=value
Example:
name=
OR
unset name
Examples:
customerName=
unset userAge
SHELL SCRIPTING
Including the keyword readonly before the command assignment prevents you
from changing the variable afterwards
Examples:
name=“Murray Saul”
readonly name
readonly phone="123-4567"
If no variable name is supplied a list of defined read only variables will be displayed.
NOTE: Read-only variables cannot be removed – you must log out for them to
be cleared
SHELL SCRIPTING
Getting Practice
To get practice to help perform assignment #3, perform Week 10 Tutorial:
• INVESTIGATION 1
WEEK 9: LESSON 2
POSITIONAL PARAMETERS
TEST STATEMENT / CONTROL FLOW STATEMENTS (LOGIC / LOOPS)
PHOTOS AND ICONS USED IN THIS SLIDE SHOW ARE LICENSED UNDER CC BY-SA
LESSON 2 TOPICS
Positional Parameters
• Purpose
• Usage
Reference: http://osr600doc.xinuos.com/en/SDK_tools/_Positional_Parameters.html
POSITIONAL PARAMETERS
arg1 arg2 arg3 … argN
• Use the set command with the values as argument after the
set command
Examples:
echo $1
echo $2
echo $3
POSITIONAL PARAMETERS
arg1 arg2 arg3 … argN
You would use positional parameters in your shell script that would
expand the positional parament with its stored value.
#!/bin/bash
Examples:
echo ${10}
ls ${23}
POSITIONAL PARAMETERS
arg1 arg2 arg3 … argN
Examples:
shift
shift 2
SPECIAL PARAMETERS
$* $# $?
Parameter Purpose
$* Display all positional parameters.
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.
Examples:
PWD
echo $?
pwd
echo $?
CONTROL FLOW STATEMENTS
Examples:
name=“Murray”
test $name = “Murray”
echo $?
test $name = “David”
echo $?
CONTROL FLOW STATEMENTS
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.
Option Purpose
-eq Equal to
-ne Not equal to
-lt , -le Less than, Less than or equal to
-gt, -ge Greater than, greater than or equal to
CONTROL FLOW STATEMENTS
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.
Option Purpose
-f file_pathname Regular filename exists
-d file_pathname Directory filename exists
-s file_pathname Regular filename is non-empty
-w file_pathname file exists / write permission is granted
CONTROL FLOW STATEMENTS
Logic Statements
if test condition
then
command(s)
fi
CONTROL FLOW STATEMENTS
Logic Statements
Logic Statements
Example:
num1=5
num2=10
if [ $num1 –lt $num2 ]
then
echo “Less Than”
fi
CONTROL FLOW STATEMENTS
Loop Statements
Reference:
https://www.chegg.com/homework-help/definitions/loop-statement-3
CONTROL FLOW STATEMENTS
Example:
Instructor Demonstration
Getting Practice
To get practice to help perform assignment #2, perform Week 10 Tutorial:
• INVESTIGATION 2