THE UNIVERSITY OF DODOMA
COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION
DEGREE PROGRAM: BSc-CNISE 2.
COURSE NAME: INTRODUCTION TO LINUX /UNIX SYSTEM
COURSE CODE: CP 211
COURSE INSTRUCTOR: DR. JUSTIN WOISO
NATURE OF WORK: ASSIGNMENT 3
STUDENT NAME: BRIGHT PETER NG’ONDYAH
REGISTRATION NUMBER: T21-03-03649
1
ANSWER 1
#!/bin/bash
# Define the directory to search in
dir=$1
# Define the file extensions to search for
extensions=(".tiff" ".ogg" ".r")
# Index for iterating through the extensions array
index=0
# Iterate through the extensions array using a while loop
while [ $index -lt ${#extensions[@]} ]; do
extension=${extensions[index]}
# Search for files with the specified extension in the directory
found_files=($(find $dir -name "*$extension"))
# Check if any files were found
if [ ${#found_files[@]} -gt 0 ]; then
# Print the found files
echo "Files with extension $extension found:"
for file in "${found_files[@]}"; do
echo $file
done
else
# Print a message if no files were found
echo "No files with extension $extension found."
2
fi
# Increment the index for the next iteration
index=$((index + 1))
done
ANSWER 2
# Search for a keyword in all found files
for file in "${found_files[@]}";
do grep -i "keyword" $file
done