COMP7004 - Systems Scripting
Lecture 05: Arithmetic Operators and Controls Flow
Dr. Vincent Emeakaroha
vincent.emeakaroha@mtu.ie
Semester 2, 2026
Arithmetic Operators
Available operators include:
+ Add two numbers
- Subtract two numbers
/ Divide two numbers
* Multiply two numbers
% Modular operation
Operator precedence
*, /, %, +, - (Priority from left to right)
Example (Slide 4)
arithmetic.sh
Arithmetic Evaluations
The let built-in command can be used to do mathematical functions:
let X=10+2*7
echo $X
24
let Y=X+2*4
echo $Y
32
Two methods of evaluating arithmetic expression are by
$[expression] or $((expression))
echo $((123+20))”
143
VALORE=$[123+20]
echo $[123*$VALORE]”
17589
arithmetic.sh
#!/bin/bash
echo -n "Enter the first number: "; read x
echo -n "Enter the second number: "; read y
add=$(($x + $y))
sub=$(($x - $y))
mul=$(($x * $y))
div=$(($x / $y))
mod=$(($x % $y))
# print out the answers:
echo "Sum: $add"
echo "Difference: $sub"
echo "Product: $mul"
echo "Quotient: $div"
echo "Remainder: $mod"
String Operator Expressions
An expression can be: String comparison, Numeric comparison, File operators
and Logical operators and it is represented by [ expression ]
String Comparisons:
== compare if two strings are equal
!= compare if two strings are not equal
-n evaluate if string length is greater than zero
-z evaluate if string length is equal to zero
Examples:
[ s1 == s2 ] (true if s1 same as s2, else false)
[ s1 != s2 ] (true if s1 not same as s2, else false)
[ s1 ] (true if s1 is not empty, else false)
[ -n s1 ] (true if s1 has a length greater then 0, else false)
[ -z s2 ] (true if s2 has a length of 0, otherwise false)
Number Comparison Operators
-eq compare if two numbers are equal
-ge compare if one number is greater than or equal to a number
-le compare if one number is less than or equal to a number
-ne compare if two numbers are not equal
-gt compare if one number is greater than another number
-lt compare if one number is less than another number
Examples:
[ n1 -eq n2 ] (true if n1 same as n2, else false)
[ n1 -ge n2 ] (true if n1 greater then or equal to n2, else false)
[ n1 -le n2 ] (true if n1 less then or equal to n2, else false)
[ n1 -ne n2 ] (true if n1 is not same as n2, else false)
[ n1 -gt n2 ] (true if n1 greater then n2, else false)
[ n1 -lt n2 ] (true if n1 less then n2, else false)
Control Flow
Rather than just executing a predetermined sequence
of commands, a script can be made flexible by using
the control flow constructs available:
if-then-else
case
loops
while
for
until
select
“if” Conditional Statement
Conditionals let us decide whether to perform an action or not, this
decision is taken by evaluating an expression. A basic full form is:
if [ expression ]
then
statements
elif [ expression ]
then
statements
else
statements
fi
The elif (else if) and else sections are optional
Put spaces after [ and before ], and around the operators and operands.
if.sh
#!/bin/bash
USER="bill"
echo -n "Enter your login name: "
read name
if [ $name == $USER ]
then
echo "Hello, $name. How are you today ?"
else
echo "You are not $USER, so who are you ?
fi
File Operator Expression
Files operators:
-d check if path given is a directory
-f check if path given is a file
-e check if file name exists
-r check if read permission is set for file or directory
-s check if a file has a length greater than 0
-w check if write permission is set for a file or directory
-x check if execute permission is set for a file or directory
Examples:
[ -d fname ] (true if fname is a directory, otherwise false)
[ -f fname ] (true if fname is a file, otherwise false)
[ -e fname ] (true if fname exists, otherwise false)
[ -s fname ] (true if fname length is greater then 0, else false)
[ -r fname ] (true if fname has the read permission, else false)
[ -w fname ] (true if fname has the write permission, else false)
[ -x fname ] (true if fname has the execute permission, else false)
fileexpress.sh
#!/bin/bash
if [ -e /etc/passwd ]
then
echoGreat it exist”
else
echoThis file does not exist.”
exit 1
fi
Boolean Operators
! negate (NOT) a logical expression
-a logically AND two logical expressions
-o logically OR two logical expressions
Example:
Logical1.sh
Logical1.sh
#!/bin/bash
echo -n "Enter a number 1 <= x < 10: "
read num
if [ $num -ge 1 -a $num -lt 10 ]
then
echo "$num*$num=$(($num*$num))"
else
echo "Wrong input !"
fi
Logical Operator
&& logically AND two logical expressions
|| logically OR two logical expressions
Example:
logical2.sh
logical2.sh
#!/bin/bash
echo -n "Enter a number 1 <= x < 10: "
read num
if [ $num -ge 1 ] && [ $num -lt 10 ]
then
echo$num*$num is $(($num*$num))”
else
echoWrong input !”
fi
Case Statement
Used to execute statements based on specific values. Often used in place of
an if statement when there are large number of conditions.
Value used can be a number, string, an expression, etc.
Each set of statements must be ended by a pair of semicolons;
a *) is used to accept any value not matched with list of values
case $var in
val1)
statements;;
val2)
statements;;
*)
statements;;
esac
case.sh
#!/bin/bash
echo -n "Enter a number 1 <= x < 10: "
read x
case $x in
1) echo "Value of x is 1.";;
2) echo "Value of x is 2.";;
3) echo "Value of x is 3.";;
4) echo "Value of x is 4.";;
5) echo "Value of x is 5.";;
6) echo "Value of x is 6.";;
7) echo "Value of x is 7.";;
8) echo "Value of x is 8.";;
9) echo "Value of x is 9.";;
0 | 10) echo "wrong number.";;
*) echo "Unrecognized value.";;
esac
“for Statement
The for structure is used when you are looping through a
range of command list.
Syntax structure:
for var in list
do
statements
done
Statements are executed withvarset to each value in the
list.
Example
for1.sh
for1.sh
#!/bin/bash
sum=0
for num in 1 2 3 4 5
do
sum=$(($sum + $num))
done
echo $sum
“for Examples
If the list part of the “for” statement is left off, “var” is
set to each parameter passed to the script.
That is the positional parameters ( $1, $2, $3,…) /
arguments given when the script is called.
Example
for3.sh
for3.sh
#!/bin/bash
for x
do
echo "The value of variable x is: $x"
sleep 1
done
Calling script as “./for3.sh arg1 arg2outputs:
The value of variable x is: arg1
The value of variable x is: arg2
Array Variable
Unlike scalar variable, array is capable of holding multiple
values at the same time.
Provides a means of grouping variables
Same naming convention as for scalar variables
There are two types of arrays
1. Indexed array represents arrays whose keys are ordered
integer
array_name[index] = value; or declare a array_name = (value1, value2,...)
E.g., declare -a array_name = (Stephen, Sean, Bill, John)
The key word “declarea” can also be left out
2. Associative array represents arrays whose keys are strings
declare A array_name=([foo]=bar [baz]=foobar)
The key word “declareA” must be specified.
Array Manipulation
Indexing content
Array uses indexes to organise content.
Positive indexes start at 0 and not 1
E.g,: array_name[0] = “hello
Negative number can be used as offset to extract values
but not as indexes.
Accessing array values
${array_name[index]}
array1.sh
#!/bin/bash
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}”
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"
Array: Extracting Element by Offset
and Length
Array content can be manipulated using many different
mechanisms.
It is possible to extract elements at different positions.
Offset determines a beginning position and
Length show how many elements
Example:
array2.sh
array2.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]:3:2}
Output
Suse Fedora
Here Offset is 3 and length is 2.
Array: Extracting Particular Element
If we use Offset and Length on a particular string
element of an array
We end up extracting the parts of that element.
Example
array3.sh
array3.sh
#! /bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS'
'OpenLinux');
echo ${Unix[2]:0:4}
Output
Ubun
Here we extracted parts of the element at index 2, which is
Ubuntu”
Thank You!