165 lines
7.0 KiB
Bash
Executable File
165 lines
7.0 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 to add an entry to the datafile and sort the file in place
|
|
# 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() {
|
|
# the basic program
|
|
while true;
|
|
do
|
|
# ask the user if they would like to add a entry to the datafile
|
|
printo "███████████████████████████████████████████████████ ADD ENTRY? ███" H1
|
|
printo "Would you like to add an entry to the datafile? [y]" Q
|
|
# give the quit notice
|
|
printo "To Quit! [q]" F
|
|
echo -n " [y/q]: "
|
|
read -r answer
|
|
if [ ${answer^^} == "Y" ] || [ ${answer^^} == "YES" ]; then
|
|
# 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
|
|
# show error on failure
|
|
printo "██████████████████████████████████████████████████████ FAILURE ███" F
|
|
printo "Entry failed!" F
|
|
fi
|
|
elif [ ${answer^^} == "Q" ]; then
|
|
exit 0
|
|
fi
|
|
done
|
|
# we should not get here
|
|
exit 1
|
|
}
|
|
|
|
#█████████████████████████████████████████████████████████████ 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
|
|
# 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
|
|
# move name global for search
|
|
export FULL_NAME=${full_name}
|
|
# return successfully added
|
|
return 0
|
|
}
|
|
|
|
#█████████████████████████████████████████████████████████████ RUN MAIN ;) ███
|
|
main
|
|
|
|
#█████████████████████████ Display the number of lines/entries in the file ███
|
|
echo
|
|
echo -n "Entries: "
|
|
cat datafile | wc -l
|