COMP7004 - Systems Scripting
Lecture 06: Array, Control Flow
Dr. Vincent Emeakaroha
vincent.emeakaroha@mtu.ie
Semester 2, 2026
Manipulating 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
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
Manipulating Array: Adding an
Element
By default, it puts the new element after the last index
Changes array content permanently
Example:
array5.sh
array5.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "Mint" "Solaris")
echo ${Unix[@]}
Output
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux Mint Solaris
Manipulating Array: Adding an
Element Start and Specific Position
To add element at the beginning of array
Place the new elements before the existing array
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("Mint" "Solaris" "${Unix[@]}" )
To add element at a specific index position. E.g., index 2
Get all elements before index position2 Unix[0] and Unix[1];
Add an element to the array;
Get all elements from index position2 to the last Unix[2], Unix[3], ....
Unix=( "${Unix[@]:0:2}" "new_element" "${Unix[@]:2}" )
Manipulating Array: Removing an
Element
Command unsetremoves an element and set the value
to
Example:
array6.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
unset Unix[3]
echo ${Unix[@]}
Output
Debian Red hat Ubuntu Fedora UTS OpenLinux
Manipulating Array: Copying
Copy by expanding an array into a new one.
Example:
array7.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Linux=("${Unix[@]}")
echo ${Linux[@]}
Output
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux
Concatenation of Two Arrays
Expand elements of two arrays and assign to new array.
Example:
array8.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');
UnixShell=("${Unix[@]}" "${Shell[@]}")
echo ${UnixShell[@]}
echo ${#UnixShell[@]}
Output
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh
14
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
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!
A C-Like “for” Loop
An alternative form of the for structure is
for (( EXPR1 ; EXPR2 ; EXPR3 ))
do
statements
done
First, the arithmetic expression EXPR1 is evaluated. EXPR2
is then evaluated repeatedly until it evaluates to 0. Each
time EXPR2 is evaluates to a non-zero value, statements
are executed and EXPR3 is evaluated.
Example
for4.sh
for4.sh
#!/bin/bash
echo n "Enter a number: "; read x
sum=0
for (( i=1 ; $i<$x ; i=$i+1 ))
do
sum=$(($sum+$i))
done
echo "The sum of the first $x numbers is: $sum"
While Statement
The while structure is used to execute a set of commands
while a specified condition is true.
The loop terminates as soon as the condition becomes false.
If condition never becomes false, loop will never exit.
while expression
do
statements
done
Example
while.sh
while.sh
#!/bin/bash
echo n "Enter a number: "; read n
sum=0; i=1
while [ $i -le $n ]
do
sum=$(($sum+$i))
((i=$i+1))
done
echo "The sum of the first $n numbers is: $sum"
“while Loop: menu.sh
#!/bin/bash
clear ; loop=y
while [ "$loop" == "y " ] ;
do
echo "Menu"; echo "===="
echo "D: print the date"
echo "W: print the users who are currently log on."
echo "P: print the working directory"
echo "Q: quit."
echo
read choice
case $choice in
D | d) date ;;
W | w) who ;;
P | p) pwd ;;
Q | q) loop=n ;;
*) echo "Illegal choice." ;;
esac
echo
done
Continue Statement
The continue statement causes a jump to the next
iteration of a loop.
Skips all the remaining commands in that particular loop
cycle.
It initiates the beginning of the next loop cycle
Example:
continue.sh
continue.sh
#!/bin/bash
LIMIT=19
echo
echo "Printing Numbers 1 through 20 (but not 3 and 11)"
a=0
while [ $a -le $LIMIT ]; do
a=$(($a+1))
if [ $a -eq 3 ] || [ $a -eq 11 ]
then
continue
fi
echo -n "$a "
done
Break Statement
A break command terminates a loop and continue
program execution.
Breaks out of a loop
Returns execution back to the main program
Example
break.sh
break.sh
#!/bin/bash
LIMIT=19
echo
echo "Printing Numbers 1 through 20, but something happens after 2 … "
a=0
while [ $a -le $LIMIT ]
do
a=$(($a+1))
if [ $a -gt 2 ]
then
break
fi
echo -n "$a "
done
echo; echo; echo
exit 0
Until Statement
The until structure is very similar to the while structure.
The until structure loops until the condition is true.
So basically it isuntil this condition is true, do this”.
until [expression]
do
statements
done
Example
until.sh
until.sh
#!/bin/bash
echo "Enter a number: "; read x
echo ; echo "Count Down"
until [ $x -le 0 ]
do
echo $x
x=$(($x -1))
sleep 1
done
echo ; echo "GO !"
Debugging Bash Scripts
Bash provides two options which will give useful
information for debugging
-x : displays each line of the script with variable substitution
and before execution
-v : displays each line of the script as typed before execution
Usage syntax:
#!/bin/bash v or #!/bin/bash x or #!/bin/bash xv
Example using for loop
debug.sh
debug.sh
#!/bin/bash -x
echo n "Enter a number: "; read x
sum=0
for (( i=1 ; $i<$x ; i=$i+1 ))
do
sum=$(($sum+$i))
done
echo "The sum of the first $x numbers is: $sum"
Output with inputting 3
+ echo $'?\200\223n' 'Enter a number: '
n Enter a number:
+ read x
3
+ sum=0
+ (( i=1 ))
+ (( 1<3 ))
+ sum=0+1
+ (( i=1+1 ))
+ (( 2<3 ))
+ sum=1+2
+ (( i=2+1 ))
+ (( 3<3 ))
+ echo 'The sum of the first 3 numbers is:3'
The sum of the first 3 numbers is: 3
Thank You!