-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemCust
executable file
·113 lines (98 loc) · 4.45 KB
/
RemCust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
# Kevin Fan (26/4/17)
# Description: This script searches and removes all of a customer's information
# from the Address Book based on a particular customers name or alias
# Clear the terminal
clear
# This function reads in the search input from the user
function readInput
{
# Ask user for search name
printf "\nPlease enter name or alias of customer you wish to remove: "
# Reads in search name as parameter
read input
# Converts the entered input from user to all lowercase to perform a case insensitive search
# (all customers added by the AddCust script are converted to all lower case on addition to file)
lowerCaseInput=${input,,}
# Performs an awk search on the lowerCaseInput on the name and alias. Count the number of lines and assign to variable
searchCount=$(awk -v search="$lowerCaseInput" '$2 ~ search || $3 ~ search' CustomerDetails | wc -l)
# Calls the checkInput function to check is the input a valid one
checkInput
}
# This function checks the search input to see if its valid.
function checkInput
{
# If search input is blank, error message is displayed and calls the readInput function to ask user to re-enter search name/alias
if [ -z $lowerCaseInput ]; then
printf "\n\tNo entry detected. Please re-enter search term"
readInput
# If search count of input is not 0, display the number of matches found and call the actionAfterSearch function
elif [[ $searchCount -ne 0 ]]; then
printf "\n$searchCount match(s) were found:\n"
# Awk functions that search on the lowerCaseInput on the name and alias and displays the matching lines
awk -v search="$lowerCaseInput" '$2 ~ search || $3 ~ search' CustomerDetails
actionAfterSearch
# If search count of input is 0, display message and call the actionAfterSearch function
elif [[ $searchCount -eq 0 ]]; then
printf "\nNo matches were found\n"
actionAfterSearch
fi
}
# This function serves as a secondary menu that gives options to the user after the search
# Users can remove all the matching entries, perfom another seach or return to the main menu
function actionAfterSearch
{
# Local variable to control while loop
validChoice=false;
while [[ $validChoice = false ]]; do
printf "\n\t1) Remove all matching entries found\n"
printf "\n\t2) Perform another search\n"
printf "\n\t=========================================\n"
printf "\n\t0) Return to Main Menu\n"
printf "\nEnter choice: "
read choice
clear
# If entered choice is not a number, display error message and loop back
if ! [[ $choice =~ ^-?[0-9]+$ ]]; then
printf "\nInvalid (String) entry, only integers shown are allowed. Please re-renter option\n"
# If entered choice is a number greater than or less than the options available, display error message and loop back
elif [ $choice -le -1 ] || [ $choice -ge 3 ]; then
printf "\nInvalid number entered\n"
# If entered choice is 1, set the validChoice variable to valid and call the removeInput function
elif [[ $choice -eq 1 ]]; then
validChoice=valid
removeInput
# If the eneted choice is 2, set the validChoice variable to valid and call the readInput function to search again
elif [[ $choice -eq 2 ]]; then
validChoice=valid
readInput
# In all other cases, set the validChoice variable to value, and the script will end and return back to menu script
else
validChoice=valid
fi
done
}
#This function removes the searched customer(s), an additional confirmation is asked from the user to delete the Customers or not
function removeInput
{
# Until loop that read user choice until the upperCase of choice variable is either Y or N
until [[ ${choice^^} = 'Y' || ${choice^^} = 'N' ]]; do
printf "\nRemove customers (Y/N):"
read choice
done
# User choice to converted to upper case, and if its Y then remove the searched Customer(s) and return to menu script
if [[ ${choice^^} = 'Y' ]]; then
# Inverted awk search - moves all Customers whos names and alias that do not have the search term to another tempoary file
awk -v search="$lowerCaseInput" '!($2 ~ search) && !($3 ~ search)' CustomerDetails > /tmp/CustomerDetails
#Temp file is moved back to the same directory replacing the old file
mv /tmp/CustomerDetails CustomerDetails
printf "\nCustomer Details successfully removed\n"
sleep 1
# User choice to converted to upper case, and if its N and return to menu script
elif [[ ${choice^^} = 'N' ]]; then
printf "\nNo customer(s) removed"
sleep 1
fi
}
# Calls the readInput function that begins the script
readInput