-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddCust
More file actions
executable file
·97 lines (83 loc) · 2.94 KB
/
AddCust
File metadata and controls
executable file
·97 lines (83 loc) · 2.94 KB
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
#!/bin/bash
# Kevin Fan (26/4/17)
# Description: This script creates a local file named "CustomerDetails".
# Parameters are taken in from the user and stored/updates the CustomerDetails
# file with customer details taken from the user
# Clears the screen content
clear
# If CustomerDetails file does not exist, create it
if [ ! -f CustomerDetails ]; then
touch CustomerDetails
fi
# This function asks user for customer email. The valid email function is called
# after to check is whether email is valid
function readEmail
{
printf "Please enter the customer email address: "
read email
validEmail
}
# This function validates whether email entered is valid or not.
# If email is not valid the readEmail function is called again to ask user to
# re-enter email address
function validEmail
{
# Local variable to control while loop
validEmail=false
while [[ $validEmail = false ]]; do
# Prints out error message if the email it not in the format *@*.*
if [[ $email != *@*.* ]]; then
printf "\n\tNot a valid email. Must be in the format *@*.*\n"
readEmail
# Prints out error message if the email if already contained in the CustomerDetails file
elif [[ $(grep -i -c $email CustomerDetails) -ne 0 ]]; then
printf "\n\tEmail already used\n"
readEmail
else
# In all other cases, email is valid and exits the loop
validEmail=valid
fi
done
}
# This function asks user for phone number. The validPhoneNumber
# function is then called to check is phone number is integars only
function readPhoneNumber
{
printf "\nPlease enter the customer telephone Number: "
read phoneNumber
validNumber
}
# This function checks does phone number contain positive integars only.
# If not, the readPhoneNumber function to called again to ask user to re-enter
# phone number
function validNumber
{
if ! [[ $phoneNumber =~ ^[0-9]+$ ]]; then
printf "\n\tInvalid character detected, only positive integers are allowed. Please re-renter phone number\n"
readPhoneNumber
fi
}
# Call readEmail function
readEmail
# Asks user for name and take in as parameter, loops until name in not blank
while [[ -z $name ]]; do
printf "\nPlease enter the customer name: "
read name
done
# Asks user for alias and take in as parameter, loops until name in not blank
while [[ -z $alias ]]; do
printf "\nPlease enter the customer alias: "
read alias
done
# Asks user for address and take in as parameter, loops until name in not blank
while [[ -z $address ]]; do
printf "\nPlease enter the customer address: "
read address
done
# Calls the readPhoneNumber function to ask user for number and take in as parameter
readPhoneNumber
# Takes the entered parameters, converts them to lowercase and pass them in a file called CustomerDetails
echo "${email,,} ${name,,} ${alias,,} ${address,,} $phoneNumber" >> CustomerDetails
# Sort the customer details in alphabetical order according to their email address
sort -o CustomerDetails CustomerDetails
printf "\n\tCustomer successfully added.\n"