-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo_cmd.sh
executable file
·91 lines (83 loc) · 1.91 KB
/
todo_cmd.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
#!/bin/bash
####################################
# CIS 1910 - Spring 2023
# Final Project: TodoCal
# Jason Hom and Audrey Yang
####################################
# assuming we have a "database" called todos.csv
# formatted (todo)
function addTodo {
printf "\n*** Add TODO ***\n"
read -p "Enter task: " -r todo
echo "${todo}" >> todos.csv
printf "Thanks, added your task!\n"
}
function showTodos {
printf "\n*** My TODOs ***\n"
count=0
cat todos.csv | while read -r line; do
printf '[%d] %s\n' "$count" "$line"
(( count++ ))
done
}
function editTodos {
printf "\n*** Edit TODOs ***\n"
read -p "Which TODO would you like to edit? Please enter the number: " -r todoNum
count=0
while read -r line; do
if [ $count -eq $((todoNum)) ]; then
break
fi
(( count++ ))
done < <(cat todos.csv)
if [ $count -eq $((todoNum)) ]; then
read -p "What would you like to replace this TODO with? " -r newTodo
sed -ri "s/$line/$newTodo/" todos.csv
printf "TODO updated!\n"
else
printf "TODO does not exist\n"
fi
}
function deleteTodos {
printf "\n*** Delete TODOs ***\n"
read -p "Which TODO would you like to delete? Please enter the number: " -r todoNum
count=0
while read -r line; do
if [ $count -eq $((todoNum)) ]; then
break
fi
(( count++ ))
done < <(cat todos.csv)
if [ $count -eq $((todoNum)) ]; then
sed -ri "/$line/d" todos.csv
printf "TODO deleted!\n"
else
printf "TODO does not exist\n"
fi
}
function main() {
case $1 in
add)
addTodo
;;
show)
showTodos
;;
edit)
showTodos
editTodos
;;
delete)
showTodos
deleteTodos
;;
help)
printf "\n*** HELP ***\n"
printf "Available commands:\nadd\nshow\nedit\ndelete\nhelp\n"
;;
*)
printf "usage: ./todo.sh [add | show | edit | delete | help]\n"
;;
esac
}
main $1