322 lines
13 KiB
Bash
Executable File
322 lines
13 KiB
Bash
Executable File
#!/bin/bash
|
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
# Champlain College SDEV-415-81
|
|
#
|
|
# Linux/Unix Programming Week 14/15: Final Project - (2020/12/18)
|
|
#
|
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
#
|
|
# This script is add, delete, and view entries in the datafile
|
|
# The datafile has the following format:
|
|
# First Last Name:Phone Number:Address:Birth date:Salary
|
|
# Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
|
|
#
|
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
#
|
|
# Written by Llewellyn van der Merwe <llewellyn.vandermerw@mymail.champlain.edu>, December 2020
|
|
# Copyright (C) 2020. All Rights Reserved
|
|
# License GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
|
|
#
|
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
#█████████████████████████████████████████████████████████ Function - Main ███
|
|
function main() {
|
|
# we first check if our data file exist and is readable and writable
|
|
if [ ! -e datafile ]; then
|
|
# give console error
|
|
printo "The datafile does not exist." F
|
|
# break out here
|
|
exit 1
|
|
elif [ ! -w datafile ]; then
|
|
# give console error
|
|
printo "The datafile is not writable." F
|
|
# break out here
|
|
exit 1
|
|
fi
|
|
# some house cleaning
|
|
export PS3_old=$PS3
|
|
# some defaults
|
|
export PS3="Please make a selection: "
|
|
# Start our little Menu
|
|
select menu in \
|
|
'Add entry' \
|
|
'Delete entry' \
|
|
'View entry' \
|
|
'View all entries' \
|
|
'Exit'
|
|
do
|
|
case $REPLY in
|
|
1 ) addEntry;;
|
|
2 ) deleteEntry;;
|
|
3 ) viewEntry;;
|
|
4 ) viewEntries;;
|
|
5 )
|
|
# final greeting
|
|
echo
|
|
printo "Goodbye! ${USER^}" S
|
|
echo
|
|
# restore the default
|
|
export PS3=$PS3_old
|
|
exit 0;;
|
|
* )
|
|
printo "Invalid entry, try again." F
|
|
printo "Select option 1..5" F
|
|
displayMenu;;
|
|
esac
|
|
# we can add a normal break here if the loop must just run once
|
|
# break;
|
|
done
|
|
# restore the default
|
|
export PS3=$PS3_old
|
|
# we should not get here
|
|
exit 1
|
|
}
|
|
|
|
#███████████████████████████ Function - To check if user wants to continue ███
|
|
function continueOrNot(){
|
|
# ask the user if they would like to see the menu again
|
|
printo "████████████████████████████████████████████████████ CONTINUE? ███" H1
|
|
printo "Would you like to see the menu again?" Q
|
|
echo -n " [y/N]: "
|
|
read -r answer
|
|
if [ ${answer^^} == "Y" ] || [ ${answer^^} == "YES" ]; then
|
|
displayMenu
|
|
return 0
|
|
else
|
|
# final greeting
|
|
echo
|
|
printo "Goodbye! ${USER^}" S
|
|
echo
|
|
# restore the default
|
|
export PS3=$PS3_old
|
|
exit 0;
|
|
fi
|
|
}
|
|
|
|
#█████████████████████████████████████████████████ Function - Display Menu ███
|
|
function displayMenu(){
|
|
echo
|
|
echo "1) Add entry"
|
|
echo "2) Delete entry"
|
|
echo "3) View entry"
|
|
echo "4) View all"
|
|
echo "5) Exit"
|
|
}
|
|
|
|
#████████████████████████████████████████████████████ Function - Add Entry ███
|
|
function addEntry() {
|
|
# get the new entry
|
|
if getNewEntry; then
|
|
# sort the file
|
|
sortDataFile
|
|
# give success notice
|
|
printo "████████████████████████████████████████████████ ADDED SUCCESS ███" S
|
|
printo "Entry was successfully added!" S
|
|
# show line and line number where the entry was added
|
|
echo -n "on-line=>"; grep -En "^${FULL_NAME}:" datafile | column -t -s :
|
|
else
|
|
# get the exit status
|
|
why_fail=$?
|
|
# show error on failure
|
|
printo "██████████████████████████████████████████████████████ FAILURE ███" F
|
|
printo "Entry failed!" F
|
|
# check exist status
|
|
if [ $why_fail == 2 ]; then
|
|
printo "Since (${FULL_NAME}) already exist in file!" F
|
|
fi
|
|
fi
|
|
# check if we are continuing
|
|
continueOrNot
|
|
}
|
|
|
|
#█████████████████████████████████████████████████ Function - Delete Entry ███
|
|
function deleteEntry() {
|
|
# Ask the user the full name of the entry they would like to view
|
|
printo "██████████████████████████████████████████████ ENTRY TO DELETE ███" H1
|
|
printo "Please enter full name of the entry you would like to delete." Q
|
|
printo "NAME FORMAT ==> [first last middle]" Q
|
|
echo -n " [ENTER]: "
|
|
read -r search_value
|
|
# force value to be given
|
|
while [[ ${#search_value} = 0 ]]; do
|
|
printo "Empty value not allowed" F
|
|
echo -n " [ENTER]: "
|
|
read -r search_value
|
|
done
|
|
# if found remove
|
|
if grep -Eq "^${search_value}:" datafile; then
|
|
# now remove from file
|
|
sed -iE "/^${search_value}:/d" datafile
|
|
# give success found notice
|
|
printo "███████████████████████████████████████████████ ENTRY DELETED! ███" S
|
|
printo "Entry was found and deleted!" S
|
|
else
|
|
# give not found notice
|
|
printo "███████████████████████████████████████████████████ NOT FOUND! ███" F
|
|
printo "No entry (${search_value}) found!" F
|
|
fi
|
|
# check if we are continuing
|
|
continueOrNot
|
|
}
|
|
|
|
#███████████████████████████████████████████████████ Function - View Entry ███
|
|
function viewEntry() {
|
|
# Ask the user the full name of the entry they would like to view
|
|
printo "█████████████████████████████████████████████████ ENTRY SEARCH ███" H1
|
|
printo "Please enter any details of entry you would like to view." Q
|
|
echo -n " [ENTER]: "
|
|
read -r search_value
|
|
# force value to be given
|
|
while [[ ${#search_value} = 0 ]]; do
|
|
printo "Empty value not allowed" F
|
|
echo -n " [ENTER]: "
|
|
read -r search_value
|
|
done
|
|
# check if this value exist
|
|
if fgrep -iq "${search_value}" datafile; then
|
|
# give success found notice
|
|
printo "███████████████████████████████████████████████████████ FOUND! ███" S
|
|
printo "Entry was found!" S
|
|
# show line and line number where the entry was added
|
|
grep -iE "${search_value}" datafile | column -t -s :
|
|
else
|
|
# give not found notice
|
|
printo "███████████████████████████████████████████████████ NOT FOUND! ███" F
|
|
printo "No entry (${search_value}) found!" F
|
|
fi
|
|
# check if we are continuing
|
|
continueOrNot
|
|
}
|
|
|
|
#█████████████████████████████████████████████████ Function - View Entries ███
|
|
function viewEntries() {
|
|
# Display the sorted data as a table from file
|
|
sort -k2 datafile | column -t -s :
|
|
# above code explained:
|
|
# sort get the file content
|
|
# sort use space to detect columns
|
|
# sort use -k2 to identify last names
|
|
# sort by this 2nd column (last names)
|
|
# pipe pass data to next command
|
|
# column convert the data to formatted table
|
|
# column use the -t -s : to break up the columns
|
|
# column prints data to standard output.
|
|
|
|
# display the number of lines/entries in the file
|
|
echo
|
|
echo -n "Entries: "
|
|
cat datafile | wc -l
|
|
echo
|
|
# check if we are continuing
|
|
continueOrNot
|
|
}
|
|
|
|
#█████████████████████████████████████████████████████████████ Color Codes ███
|
|
normorange="\033[0;33m"
|
|
lightGreen="\033[1;32m"
|
|
lightCyan="\033[1;36m"
|
|
lightRed="\033[1;31m"
|
|
NC="\033[0m"
|
|
|
|
#████████████████████████████████████████████████████████ Function - PRINT ███
|
|
function printo() {
|
|
if [ -z "$2" ]; then
|
|
echo -e "$1"
|
|
elif [ "$2" == "S" ]; then
|
|
echo -e "${lightGreen}$1${NC}"
|
|
elif [ "$2" == "F" ]; then
|
|
echo -e "${lightRed}$1${NC}"
|
|
elif [ "$2" == "H1" ]; then
|
|
echo -e "${lightCyan}$1${NC}"
|
|
elif [ "$2" == "Q" ]; then
|
|
echo -e "${normorange}$1${NC}"
|
|
fi
|
|
}
|
|
|
|
#████████████████████████████████████████████ Function - Inplace sort data ███
|
|
function sortDataFile() {
|
|
# Sorted data in place
|
|
sort -k2 -o datafile datafile
|
|
# above code explained:
|
|
# sort get the file content
|
|
# sort use space to detect columns
|
|
# sort use -k2 to identify last names
|
|
# sort by this 2nd column (last names)
|
|
}
|
|
|
|
#███████████████████████████████████████████████ Function - Get new entry ███
|
|
function getNewEntry() {
|
|
echo
|
|
# Ask the user the full name
|
|
printo "██████████████████████████████████████████████████ First Last Name ███" H1
|
|
printo "Please enter full name." Q
|
|
printo "NAME FORMAT ==> [first last middle]" Q
|
|
echo -n " [ENTER]: "
|
|
read -r full_name
|
|
# force name to be given
|
|
while [[ ${#full_name} = 0 ]]; do
|
|
printo "Empty Name not allowed" F
|
|
printo "NAME FORMAT ==> [first last middle]" Q
|
|
echo -n " [ENTER]: "
|
|
read -r full_name
|
|
done
|
|
echo
|
|
# move name global for later
|
|
export FULL_NAME=${full_name}
|
|
# check if entry exist in file
|
|
if grep -Eq "^${full_name}:" datafile; then
|
|
# return failure added
|
|
return 2
|
|
fi
|
|
# Ask the user the Phone Number
|
|
printo "█████████████████████████████████████████████████████ Phone Number ███" H1
|
|
printo "Please enter phone number." Q
|
|
printo "NUMBER FORMAT ==> [000-000-0000]" Q
|
|
echo -n " [ENTER]: "
|
|
read -r phone_number
|
|
# pad empty phone_number
|
|
if [[ ${#phone_number} = 0 ]]; then
|
|
phone_number="__"
|
|
fi
|
|
echo
|
|
# Ask the user the Address
|
|
printo "██████████████████████████████████████████████████████████ Address ███" H1
|
|
printo "Please enter address." Q
|
|
printo "ADDRESS FORMAT ==> [23 Edgecliff Place, Lincoln, NB 92743]" Q
|
|
echo -n " [ENTER]: "
|
|
read -r address
|
|
# pad empty phone_number
|
|
if [[ ${#address} = 0 ]]; then
|
|
address="__"
|
|
fi
|
|
echo
|
|
# Ask the user the Birth date
|
|
printo "███████████████████████████████████████████████████████ Birth date ███" H1
|
|
printo "Please enter birth date." Q
|
|
printo "DATE FORMAT ==> [7/25/1953]" Q
|
|
echo -n " [ENTER]: "
|
|
read -r birth_date
|
|
# pad empty phone_number
|
|
if [[ ${#birth_date} = 0 ]]; then
|
|
birth_date="__"
|
|
fi
|
|
echo
|
|
# Ask the user the Salary
|
|
printo "███████████████████████████████████████████████████████████ Salary ███" H1
|
|
printo "Please enter salary." Q
|
|
printo "SALARY FORMAT ==> [85100]" Q
|
|
echo -n " [ENTER]: "
|
|
read -r salary
|
|
# pad empty phone_number
|
|
if [[ ${#salary} = 0 ]]; then
|
|
salary="__"
|
|
fi
|
|
# now add entry to file
|
|
echo "${full_name}:${phone_number}:${address}:${birth_date}:${salary}" >> datafile
|
|
# return successfully added
|
|
return 0
|
|
}
|
|
|
|
#█████████████████████████████████████████████████████████████ RUN MAIN ;) ███
|
|
main
|