You are on page 1of 27

bash Execution Control

COMP2101
Fall 2019
Scripting Environment

The process environment

Shell environment files

Environment variables
Shell Process

The bash shell is just a program stored in the file /bin/bash

When you tell the operating system to run that program, it


creates an instance of the program known as a process

A process is a chunk of code and data that reside in RAM and


has at least one thread of control

Bash is single-threaded and does things sequentially (think turn-


based games like chess)
Shell Process

Processes are started by other processes when they fork

Processes inherit their environment from the process that


started them - the new process is called the child process and
the original process is called the parent process

Programs often look for environment ( sometimes called


startup ) files to modify their inherited operating environment
and provide configuration of the program
Environment Variables
Variables are only usable by the process that creates them

Processes have a way to store variables in their environment, which is a special part of
process memory

Environment variables get copied to child processes

Environment variables are normally named using capital letters and numbers only

Variables can be put into, or taken out of, the environment

export VARNAME
export VARNAME=“Data”
export -n VARNAME
Lab 3 - checkMYVAR.sh
Login Shell

When you login to the system, the program that allowed you to login runs
whatever shell program is configured for your login account, typically bash for
Linux users

The first shell process started when you log onto a Linux system is called your
login shell

It is used to start other programs, manage files, and observe and control the system

If you start additional shells such as in terminal windows or by running scripts, they
are called interactive non-login shells

When your login shell process ends, you are logged out
bash Environment Files

Environment files are shell scripts that run commands to configure programs,
set variables, manipulate files, and/or send messages

Each user's environment files are kept in their home directory, and users can
create, modify, or remove them

bash runs /etc/profile before running user-specific environment script files, in


order to provide a global minimum configuration for all bash users

bash looks for ~/.bash_profile, ~/.bash_login, ~/.profile and ~/.bash_logout for


login shells

bash looks for ~/.bashrc for interactive non-login shells


Lab 3 - login-welcome.sh
Predefined Variables

bash provides a number of variables containing useful


information, the env command will display them

Some are inherited from ancestor processes (e.g. login, getty, init)

More are often set up by user environment files to customize the


user experience

bash also has some special variables that dynamically retrieve


data for realtime uses
Special Variables

$ - current process id

# - number of parameters on the command line of a script

0-n - command line parameters

RANDOM - a random 15-bit integer

REPLY - default variable name for the read command

? - exit status of most recent command, zero means success


Test Expressions

Testing data

Testing files
Test Command

The test command evaluates an expression and sets its exit status
based on whether the expression evaluates as true or false

The test command can also be run using the [ alias, which uses a ]
to mark the end of the expression on the command line

Multiple expressions can be evaluated by putting -a (and) or -o (or)


between expressions

test -d /etc && echo "/etc exists"


[ -d /etc ] && echo "/etc exists"
[ -d /etc -a -r /etc ] && echo "/etc exists and is readable"
File Testing
The following are commonly used file tests, although there are more not included here

-e filename : filename exists as any kind of filesystem object

-f filename : filename exists and is a regular file (can hold data)

-d filename : filename exists and is a directory

-r filename : filename exists and is readable by whoever is doing the test

-w filename : filename exists and is writable by whoever is doing the test

-h filename : filename exists and is a symbolic link

-s filename : filename exists and is not empty

Putting ! in front of the test operator ( the letter with a dash in front of it ) inverts the test
Test Expressions

Test is generally used to either test data (usually in variables), or to test file attributes

Test expressions may test single things for some characteristic, or attribute - this is
known as a unary test

Test expressions may compare two things - this is known as a binary test

Unary test expressions take the form -x thing, where -x is the test operator
specifying what kind of test to perform

Binary test expressions take the form thing1 operator thing2 where operator tells
the test command what kind of comparison to perform

Putting ! in front of the test operator inverts the test


Unary Operators

The only unary operator that checks a variable is -v variablename


which is true if the variable exists and false if it doesn't

Text strings can be tested to see if they have no text in them


with -z "sometext" or have some text with -n "sometext"

Since it would make no sense to do such a test on literal text, it


is easy to see that we would use some kind of dynamic data with
the -n or -z tests
Binary Operators For Text

Text strings can be compared using the following operators

"string1" = "string2" is true if the two strings are identical

"string1" != "string2" is true if the two strings are not identical

Strings consisting of digits are compared as text by these


operators, not as numbers
Binary Operators For Numbers
To compare numbers, there are several binary test operators available

X -eq Y is true if X and Y are the same number

X -ne Y is true if X and Y are not the same number

X -lt Y is true if X is numerically less than Y

X -gt Y is true if X is numerically more than Y

X -le Y is true if X is numerically less than Y or X is equal to Y

X -ge Y is true if X is numerically more than Y or X is equal to Y


Testing Command Success

When a child process exits, the shell


can retrieve the child's exit status
from the special variable ? grep -q dennis /etc/passwd
if [ $? -ne 0 ]; then
It can be used in a test expression
echo "Adding user"
when it matters if a command has
sudo adduser dennis
errors
else
An exit status of zero means success echo "user already exists"
fi
Scripts can set their exit status with
the exit command e.g. exit 3
Lab 3 - tests.sh
Conditional Script Blocks

Running multiple commands


based on a test - using if

Running multiple commands


multiple times - looping with
while
Bash Execution Control

Scripts commonly can evaluate situations and make simple decisions about
actions to take

Simple evaluations and actions can be accomplished using && and ||

Complex evaluations and multi-step actions are better handled using more
sophisticated execution control commands

Regardless of the complexity of a test, the resulting action command(s) may


need to be run more than once, in a loop

We may have multiple data items on which we need to perform one or more
commands, also in a loop
Conditional Script Block Execution

A list of action commands can be run, or not if [ expr ]; then


run, based on the success or failure of a testing actionlist
command list fi
The test command can evaluate expressions, so if testlist; then
it is the most common command for the testlist
actionlist
An alternate testlist can be specified using elif, elif testlist; then
and you can use as many alternate testlist/
actionlist elifs as necessary
actionlist
else
A default actionlist to run if no testlists are actionlist
successful can be included by using else
fi
Lab 3 - passwordguesser.sh
Script Block Looping

An action list can be executed repeatedly (known as a loop)


based on the success or failure of a testlist using the while
command

The break or continue commands can be used in the action list


to get out of a loop early
while testlist; • break jumps to the done command and
continues the script past the loop
do
actionlist • continue jumps back to the while
done command to redo the testing list
Lab 3 - guessinggame.sh
Show me your
work to get the
marks for it

You might also like