COMP7004 - Systems Scripting
Lecture 18: Bash Revision
Dr. Vincent Emeakaroha
vincent.emeakaroha@mtu.ie
Semester 2, 2026
Writing Bash Script
Involves 3 key steps
1) Write the script using editor of choice
2) Assign permission to execute
3) Put it where shell can find it or execute from the current
working directory
2
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
3
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
4
“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
5
for1.sh
#!/bin/bash
sum=0
for num in 1 2 3 4 5
do
sum=$(($sum + $num))
done
echo $sum
6
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
Array declaration
array_name[index] = value; or declare a array_name = (value1,
value2,...)
E.g., declare -a array_name = (Stephen, Sean, Bill, John)
The key word “declare –a” can also be left out
Accessing array values
${array_name[index]}
7
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[@]}"
8
Array: Search and Replace
An element of an array can be replaced with another
element
Searches array until element is found
Uses regular expression to match element
Enables on the fly data manipulations
Example:
array4.sh
9
array4.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]/Ubuntu/Mint}
Output
Debian Red hat Mint Suse Fedora UTS OpenLinux
Note: this replacement is not permanent.
Create a new array to make it permanent.
10
Loading File Content into Array
A file content can be read line by line into an array.
Example file content:
Cat myfile.txt
Hello
How are you
Working
Hard
Big money
11
Loading File Content into Array:
“for Loop
array9.sh
#!/bin/bash
IFS=$'\r\n'
filecontent=( `cat "myfile.txt" `)
for t in "${filecontent[@]}"
do
echo $t
done
echo "Finished reading file content!"
Output:
Hello
How are you
Working
Hard
Big money
Finished reading file content!
12
Shell Functions
A shell function is similar to a shell script
stores a series of commands for execution later
shell stores functions in memory
Functions can be called upon to execute its task when needed.
shell executes a shell function in the same shell that called it
Where to define
In .profile
In your script
Or on the command line
Remove a function
Use unset built-in
13
Functions: Syntax
Functions are declared as follows
function_name(){
List_of_commands
}
The name of the function is function_name
Use to invoke the function anywhere in the script
Can be named anything
Function names should be descriptive of its actions
14
function.sh
#!/bin/bash
# Define your function here
Hello () {
echo "Hello Welcome to the World"
}
# Invoke your function
Hello
15
Functions: Passing Parameters
A Bash function can accept parameters during invocation
Unlike C-programming
Function parameters are not declared together with function name
Invalid: function_name(param1, param2,) {}
Function parameters are accessible inside functions as:
$1, $2, etc.
$# reflects number of parameters
$0 contains name of script (not name of function)
Example
function2.sh
16
function2.sh
#!/bin/bash
# Define your function here
Hello () {
echo "Hello $1 $2 Welcome to the World"
}
# Invoke your function
Hello Vincent Emeakaroha
17
Functions: Parameter Example
Write a function that accepts two parameter values
and do an addition.
#!/bin/bash
# Define your function to take two parameter
myAdd () {
echo “$1 + $2 = $(($1+$2))”
}
echo “Please enter first number:
read num1
echo “Please enter second number:
read num2
myAdd $num1 $num2
18
Functions: Parameter Example
#Proper Parameter Passing
#!/bin/bash
# Define your function to take two parameter
myAdd () {
echo “$1 + $2 = $(($1+$2))
}
echo “Please enter first number:
read num1
echo “Please enter second number:
read num2
myAdd $num1 $num2
#Use of Global Variable - Incorrect
#!/bin/bash
echo “Please enter first number:
read num1
echo “Please enter second number:
read num2
# Define your function to take two parameter
myAdd () {
echo “$num1 + $num2 = $(($num1+$num2))
}
myAdd $num1 $num2 ##OR myAdd alone
19
Functions: Return Value
Functions can return value using the syntax
return value
Returned value mostly numerical. String (characters) is possible but
complex
This command terminates the execution of the function
Returned value is stored in the shell variable “$? if not customised
$? can hold value between 0 255.
Using the commandexit in a function
Terminates the function and the whole script
Example
function3.sh
20
function3.sh
#!/bin/bash
# Define your function here
Hello () {
echo "Hey $1 $2 Welcome to the World"
return 20
}
# Invoke your function
Hello Vincent Emeakaroha
# Capture value returned by last command
ret=$?
echo "Return value is $ret"
21
AWK
22
Operation Style and Usages
awk operation:
scans a file line by line
splits each input line into fields
compares input line/fields to pattern
performs action(s) on matched lines
Useful for:
transforming data files
producing formatted reports
Programming constructs:
format output lines
arithmetic and string operations
conditionals and loops
23
Structure of an AWK Script
An awk script consists of:
An optional BEGIN segment
For preparing execution prior to reading input
pattern - action pairs
Processing the input data
For each pattern matched, the corresponding action is
taken
An optional END segment
Processing after end of input data
BEGIN {action}
pattern {action}
pattern {action}
.
.
.
pattern { action}
END {action}
24
Record and Fields
25
Local Variable
awk scripts can define and use variables
BEGIN { sum = 0 }
{ sum ++ }
END { print sum }
Some variables are predefined
Examples are the special variables
NR Number of Records
FS Field Separator
Etc.
26
AWK Special Variables
FS Field separator (default=whitespace)
RS Record separator (default=\n)
NF Number of fields in current record
NR Number of the current record
OFS Output field separator (default=space)
ORS Output record separator (default=\n)
FILENAME Current filename
Similar to Bash environment variables.
27
Basic AWK Command Types
1. Direct command:
awk [options] command file(s)
2. Script file:
awk [options] f scriptfile file(s)
-f indicates usage of script file containing script commands
Special characters
$0 is the entire line a record
$1 is the first field, $2 is the second field, ….
28
Example: Records and Fields
cat emps
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
awk '{print NR, $0}' emps
1 Tom Jones 4424 5/12/66 543354
2 Mary Adams 5346 11/4/63 28765
3 Sally Chang 1654 7/22/54 650000
4 Billy Black 1683 9/23/44 336500
29
Example: Space as Field Separator
cat emps
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
awk '{print NR, $1, $2, $5}' emps
1 Tom Jones 543354
2 Mary Adams 28765
3 Sally Chang 650000
4 Billy Black 336500
30
Example: Colon as Field Separator
cat em2
Tom Jones:4424:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500
awk -F: '/Jones/{print $1, $2}' em2
Tom Jones 4424
31
Logical Operators
Operator Meaning Example
&& Logical AND a && b
|| Logical OR a || b
! NOT ! a
Examples:
awk '($2 > 5) && ($2 <= 15) {print $0}'
file
awk '$3 == 100 || $4 > 50 {print $0}' file
32
Invoking Script File
File: grades.txt
John 85 92 78 94 88
Andrea 89 90 75 90 86
Jasper 84 88 80 92 84
awk script: average.awk
# average five grades
{ total = $2 + $3 + $4 + $5 + $6
avg = total / 5
print $1, avg }
Run as:
awk f average.awk grades.txt
Output
33
Control Statements
Conditional
if-else
Repetition
for
with counter
with array index
while
do-while
also: break, continue
34
if Example 1: Missing Scores
cat student-scores
Bill 2143 78 84 77
Jane 2321 56 58 45
James 2122 38 37
John 2537 87 97 95
Keith 2415 30 47
if.awk
#!/bin/awk
{
if ($3 =="" || $4 == "" || $5 == "")
print "Some score for the student",$1,"is
missing";
}
Invoking script
awk f if.awk student-scores
Output
Some score for the student James is missing
Some score for the student Keith is missing
35
SED
36
Sed: Stream-oriented, Non-
Interactive Text Editor
Looks for patterns one line at a time, like grep. That is, it
operates per line contents of a file
Non-interactive text editor
Borrows syntax from the interactive editor ed which accepts
the same commands
Can be used in many ways such as:
Text substitution,
Selective printing of text files,
In-a-place editing of text files,
Non-interactive editing of text files, and many more
37
sed Commands
sed commands have the general form
[address[, address]][!]command [arguments]
sed copies each input line into a pattern space
If the address of the command matches the line in the pattern space,
the command is applied to that line
If the command has no address, it is applied to each line as it enters
pattern space
If a command changes the line in pattern space, subsequent
commands operate on the modified line
When all commands have been read, the line in pattern space is
written to standard output and a new line is read into pattern
space
38
sed Address
An address can be either a line number or a pattern,
enclosed in slashes ( /pattern/ )
A pattern is described using regular expressions
Basic Regular Expression (BRE) as in grep
If no pattern is specified, the command will be applied
to all lines of the input file
To refer to the last line use: $ and the first line use: ^
39
sed Command
command is a single letter
Example: Deletion: d
[address1][,address2]d
Delete the addressed line(s) from the pattern space;
The removed line(s) are not passed to standard output.
40
Address and Command Examples
d deletes all lines
6d deletes line 6
/^$/d deletes all blank lines
1,10d deletes lines 1 through 10
1,/^$/d deletes from line 1 through the first blank line
/^$/,$d deletes from the first blank line through
the last line of the file
/^$/,10d deletes from the first blank line through line 10
/^ya*y/,/[0-9]$/d deletes from the first line that begins
with yay, yaay, yaaay, etc. through
the first line that ends with a digit
41
Examples
sed -e 'd' /etc/services
Outputs nothing
sed -e '1d' /etc/services
Delete first line and output the rest to standard output
sed -e '1,10d' /etc/services
Delete first 10 lines and output the rest
sed -e '/^#/d' /etc/services
Delete all lines starting with “#” and output the rest
42
Pattern Substitution
Syntax: [address(es)]s/pattern/replacement/[flags]
pattern - search pattern
replacement - replacement string for pattern
flags - optionally any of the following
n a number from 1 to 512 indicating which
occurrence of pattern should be replaced
g global, replace all occurrences of pattern in pattern space
p print contents of pattern space
43
Substitute Example
s/Puff Daddy/P. Diddy/
Substitute P. Diddy for the first occurrence of Puff Daddy
in pattern space
s/Tom/Dick/2
Substitutes Dick for the second occurrence of Tom in the
pattern space
s/wood/plastic/p
Substitutes plastic for the first occurrence of wood and
outputs (prints) pattern space
44
Using !
If an address is followed by an exclamation point (!), the
associated command is applied to all lines that dont match
the address or address range
Examples:
1,5!d would delete all lines except 1 through 5
/black/!s/cow/horse/ would substitute horse for
cow on all lines except those that contained black
The brown cow -> The brown horse
The black cow -> The black cow
45
Quit
Quit causes sed to stop reading new input lines and stop
sending them to standard output
It takes at most a single line address
Once a line matching the address is reached, the script will be
terminated
This can be used to save time when you only want to process
some portion of the beginning of a file
Example: to print the first 100 lines of a file (like head) use:
sed '100q' filename
sed will, by default, send the first 100 lines of filename to
standard output and then quit processing
46
Manipulating Files
sed prints modified content of a file to the screen but does NOT
change the file
Two ways to fix this:
Use “-i” option for GNU compatible sed
Output redirection
Example
sed -e 's/coat/bag/g' fileEg01 > fileEg03
Changes all occurrences of “coat” withbag” in file “fileEg01”
The modified output is saved in a new filefileEg03
FilefileEg01” remains unchanged
sed -i -e 's/coat/bag/g' fileEg01 (Changes the content of the file)
47
Thank You!