generated from Josee9988/project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqnote
executable file
·53 lines (47 loc) · 1.36 KB
/
qnote
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
#!/bin/bash
NOTES_FILENAME="/home/${USER}/.cli-quick-notes.txt"
# check if the file exists
if [[ ! -e "$NOTES_FILENAME" ]]; then
touch "$NOTES_FILENAME"
fi
check_occurrence() {
local note=$1
if grep -q -x "$note" "$NOTES_FILENAME"; then
echo "Error: the note '$note' already exists."
exit 1
fi
}
add_note() {
local note=$(echo "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
check_occurrence "$note"
if [[ -z "$note" ]]; then # shows all notes
nl -b a "$NOTES_FILENAME"
exit 0
fi
echo "$note" >>"$NOTES_FILENAME"
}
case "$1" in
-h | --help)
echo -e "\e[1mUsage:\e[0m"
echo " qnote Displays all notes."
echo " qnote [-a|--add] <note> Adds a note at the end."
echo " qnote <note> Adds a note at the end."
echo " qnote -h|--help Display this help text."
echo " qnote [-r|-d|--remove|--delete] <index> Removes a note by its index (position or line number)."
echo " qnote [-c|--clear] Removes all the notes."
;;
-a | --add)
shift
add_note "$1"
;;
-r | -d | --remove | --delete)
sed -i "${2}d" "$NOTES_FILENAME"
;;
-c | --clear)
echo -n "" >"$NOTES_FILENAME"
;;
*)
add_note "$1"
;;
esac
exit 0 # ok error