-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu
executable file
·80 lines (70 loc) · 2.44 KB
/
Menu
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
#!/bin/bash
# Kevin Fan (26/4/17)
# Description: This Menu script present the user with a command line
# menu and allow the user to select the functionaly based on
# their selection which in turns executes the relevent script
# Clear the console
clear
# Function that displays available options to user, reads in user option and calls validation
# function to check is user input valid
function mainMenu
{
printf "\nWelcome. Please choose one of the following:\n"
printf "\n\t1. Add a new Customer\n"
printf "\n\t2. Remove an existing Customer\n"
printf "\n\t3. Search for a Customer\n"
printf "\n\t4. E-mail a customer\n"
printf "\n\t==================================\n"
printf "\n\t0. Exit\n"
printf "\nEnter a number: "
read userChoice
validInput
}
# This function performs validation on userChoice from mainMenu function.
# If validation fails, a relevent error is displayed and returns to mainMenu to re-enter user Choice
# If validation passes, runMenu function is called, which in turn executes the relevant script call
function validInput
{
# If the userChoice is not an integar but a string, error message is displayed for 1 second and returns to main menu
if ! [[ $userChoice =~ ^-?[0-9]+$ ]]; then
printf "\nInvalid (String) entry, only integers shown are allowed. Please re-renter option\n"
printf "\nReturning to Main menu ...."
sleep 1
clear
mainMenu
# Checks if userChoice inputs an integar outside the available options, error message is displayed for 1 second and returns to main menu
elif [ $userChoice -le -1 ] || [ $userChoice -ge 5 ]; then
printf "\nInvalid number entered\n"
printf "\nReturning to Main menu ...."
sleep 1
clear
mainMenu
else
runMenu
fi
}
# From user choice, execute relevant script for function. Function is a while loop until 0 in
# the main menu
function runMenu
{
# While loop to stay in script until user enters 0 from the main menu function to exit script
while [ $userChoice -ne 0 ]
do
# Case statement that executes relevant scripts depending on userChoice from main menu
case $userChoice in
1) ./AddCust ;;
2) ./RemCust ;;
3) ./FindCust ;;
4) ./EmailCust ;;
esac
# After returning from the relevant script, display message for 1 second, clear the console and return to main menu
printf "\n\tReturning to main menu..."
sleep 1
clear
mainMenu
done
printf "\nExiting... bye\n"
exit 1
}
#Run main Menu function, which begins the loop
mainMenu