-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappt.sh
executable file
·134 lines (125 loc) · 3.37 KB
/
appt.sh
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
####################################
# CIS 1910 - Spring 2023
# Final Project: TodoCal
# Jason Hom and Audrey Yang
####################################
# assuming we have a "database" called appts.csv
# formatted (date, appt, start, end)
function addAppt {
printf "\n*** Add appointment ***\n"
read -p "Enter appointment name: " -r appt
read -p "Enter date (mm/dd/yy): " -r date
read -p "Enter start time (hh:mm): " -r start
read -p "Enter end time (hh:mm): " -r end
echo "${date},${appt},${start},${end}" >> appts.csv
printf "Thanks, added your appointment!\n"
}
function showAppt {
printf "\n*** My appointments ***\n"
read -p "What day would you like to view? (mm/dd/yy) " -r day
count=0
grep -E "${day}" appts.csv | while read -r line; do
printf '[%d] ' "$count"
echo $line | sed -r 's/^(.*),(.*),(.*),(.*)$/\3-\4: \2/'
(( count++ ))
done
}
function showApptsToday {
printf "\n*** Today's appointments ***\n"
today=$(date +'%m/%d/%y')
count=0
grep -E "${today}" ~/todocal/appts.csv | while read -r line; do
printf '[%d] ' "$count"
echo $line | sed -r 's/^(.*),(.*),(.*),(.*)$/\3-\4: \2/'
(( count++ ))
done
}
function editAppt {
printf "\n*** My appointments ***\n"
cat appts.csv | while read -r line; do
printf '[%d] ' "$count"
echo $line | sed -r 's/^(.*),(.*),(.*),(.*)$/\3-\4: \2/'
(( count++ ))
done
read -p "Which appointment would you like to edit? Please enter the number: " -r num
count=0
while read -r line; do
if [ $count -ge $((num)) ]; then
break
fi
(( count++ ))
done < <(cat appts.csv)
if [ $count -eq $((num)) ]; then
read -p "What would you like to replace this appointment with? " -r newAppt
date="$(cut -d ',' -f 1 <<<$line)"
startTime="$(cut -d ',' -f 3 <<<$line)"
endTime="$(cut -d ',' -f 4 <<<$line)"
sed -ri "s~$line~$date,$newAppt,$startTime,$endTime~" appts.csv
printf "Appointment updated!\n"
else
printf "Error: appointment does not exist\n"
fi
}
function deleteAppt {
printf "\n*** My appointments ***\n"
cat appts.csv | while read -r line; do
printf '[%d] ' "$count"
echo $line | sed -r 's/^(.*),(.*),(.*),(.*)$/\3-\4: \2/'
(( count++ ))
done
read -p "Which appointment would you like to delete? Please enter the number: " -r apptNum
count=0
while read -r line; do
if [ $count -eq $((apptNum)) ]; then
break
fi
(( count++ ))
done < <(cat appts.csv)
if [ $count -eq $((apptNum)) ]; then
sed -ri "\~$line~d" appts.csv
printf "Appointment deleted!\n"
else
printf "Error: appointment does not exist\n"
fi
}
function main() {
printf 'Welcome! Today is %(%m/%d/%y)T.\n' -1
printf "\tadd\n\tshow\n\tedit\n\tdelete\n\thelp\n\texit\n"
printf "Enter a command: "
read opt
while [ ! -z "$opt" ]; do
case $opt in
add)
addAppt
;;
show)
showAppt
;;
edit)
editAppt
;;
delete)
deleteAppt
;;
today)
showApptsToday
;;
help)
printf "\n*** HELP ***\n"
printf "Available commands:\nadd\nshow\nhelp\nexit\n"
;;
exit)
break
;;
*)
printf "usage: ./appt.sh [add | show | edit | delete | help | exit]\n"
;;
esac
echo
read -p "Enter a command: " -r opt
done
printf "Bye!\n"
exit 0
}
main