3 # bash-notes © 2023 by danix is licensed under CC BY-NC 4.0.
4 # To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/
6 # to debug the script run it like:
7 # DEBUG=true notes.sh ...
8 # and check /tmp/debug_bash-notes.log
9 if [[ $DEBUG == true
]]; then
10 exec 5> /tmp
/debug_bash-notes.log
17 BASENAME
=$
( basename "$0" )
21 DBVERSION
=${VERSION}_
${NOW}
26 EDITOR
=${EDITOR:-/usr/bin/vim}
27 TERMINAL
=${TERMINAL:-/usr/bin/alacritty}
28 # add options for your terminal. Remember to add the last option to execute
29 # your editor program, otherwise the script will fail.
30 # see example in the addnote function
31 TERM_OPTS
="--class notes --title notes -e "
32 # Setting PAGER here overrides whatever is set in your default shell
33 # comment this option to use your default pager if set in your shell.
34 PAGER
=${PAGER:-/usr/bin/more}
36 # set this to true to have output in plain text
37 # or use the -p option on the command line before every other option
39 # base directory for program files
40 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
41 # notes database in json format
43 # directory containing the actual notes
44 NOTESDIR
=${BASEDIR}/notes
46 } # end set_defaults, do not change this line.
50 # Do not edit below this point
51 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
54 if [ ! -x "$JQ" ]; then
55 echo "jq not found in your PATH"
56 echo "install jq to continue"
60 # IMPORT USER DEFINED OPTIONS IF ANY
61 if [[ -f $RCFILE ]]; then
62 # shellcheck disable=SC1090
66 # We prevent the program from running more than one instance:
67 PIDFILE
=/var
/tmp
/$
(basename "$0" .sh
).pid
69 # Make sure the PID file is removed when we kill the process
70 trap 'rm -f $PIDFILE; exit 1' TERM INT
72 if [[ -r $PIDFILE ]]; then
73 # PIDFILE exists, so I guess there's already an instance running
74 # let's kill it and run again
75 # shellcheck disable=SC2046,SC2086
76 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
77 # should already be deleted by trap, but just to be sure
82 echo $PID > "$PIDFILE"
84 # Export config to file
85 function export_config
() {
86 if [ -r ${RCFILE} ]; then
87 echo "Backing up current '${RCFILE}'...."
88 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
90 echo "Writing '${RCFILE}'...."
91 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
92 |
grep -v set_defaults \
93 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
95 if [ -r ${RCFILE} ]; then
96 echo "Taking no further action."
99 echo "Could not write '${RCFILE}'...!"
104 # we should expand on this function to add a sample note and explain a little bit
105 # how the program works.
106 function firstrun
() {
107 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
110 echo "${BASENAME} configuration:
112 base directory: ${BASEDIR}/
113 notes archive: ${NOTESDIR}/
114 notes database: ${DB}
116 text editor: ${EDITOR}
117 terminal: ${TERMINAL}
121 echo "Now I'll create the needed files and directories."
122 read -r -p "Do you wish to continue? (y/N) " ANSWER
126 cat << __EOL__ > ${DB}
129 "version": "${VERSION}",
130 "dbversion": "${DBVERSION}"
135 echo; echo "All done, you can now write your first note."
138 echo "No changes made. Exiting"
144 # check for notes dir existance and create it in case it doesn't exists
145 if [[ ! -d $NOTESDIR ]]; then
146 # we don't have a directory. FIRST RUN?
149 # check if input is a number, returns false or the number itself
150 function check_noteID
() {
162 function helptext
() {
164 echo " $0 [PARAMS] ..."
167 ${BASENAME} configuration is:
169 base directory: ${BASEDIR}/
170 notes archive: ${NOTESDIR}/
171 notes database: ${DB}
173 debug file: /tmp/debug_bash-note.log
175 text editor: ${EDITOR}
176 terminal: ${TERMINAL}
182 echo "${BASENAME} parameters are:"
183 echo -e " -h | --help\t\t\t: This help text"
184 echo -e " -p | --plain\t\t\t: Output is in plain text"
185 echo -e "\t\t\t\t (without this option the output is formatted)"
186 echo -e "\t\t\t\t (this option must precede all others)"
187 echo -e " -l | --list\t\t\t: List existing notes"
188 echo -e " -a | --add [\"<title>\"]\t: Add new note"
189 echo -e " -e | --edit [<note>]\t\t: Edit note"
190 echo -e " -d | --delete [<note> | all] : Delete single note or all notes at once"
191 echo -e " -s | --show [<note>]\t\t: Display note using your favourite PAGER"
192 echo -e " -v | --version\t\t: Print version"
193 echo -e " --userconf\t\t\t: Export User config file"
194 echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder"
199 # remove eventually existing temp DB file
200 if [[ -f $TMPDB ]]; then
205 echo "adding new note - \"$NOTETITLE\""
206 # shellcheck disable=SC2086
207 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
208 # [ "" == $LASTID ] && LASTID=0
209 NOTEID
=$
(( LASTID
+ 1 ))
210 # shellcheck disable=SC2086
211 touch ${NOTESDIR}/${NOW}
212 # shellcheck disable=SC2016
213 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
214 # shellcheck disable=SC2086
216 # example for alacritty:
217 # alacritty --class notes --title notes -e /usr/bin/vim ...
218 # shellcheck disable=SC2086,SC2091
219 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
221 function backup_data
() {
223 echo "backing up data in $BACKUPDIR"
226 if [ -d $BACKUPDIR ]; then
227 if [ $
(/bin
/ls -A $BACKUPDIR) ]; then
228 echo "$BACKUPDIR is not empty. Cannot continue"
231 echo "$BACKUPDIR is ok. Continuing!"
234 # BACKUPDIR doesn't exists
235 echo "$BACKUPDIR doesn't exists"
236 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
242 echo "No changes made. Exiting"
247 # ok, we have a backup directory
248 if [ -r $RCFILE ]; then
249 BCKUP_COMM
=$
(rsync
-avz --progress ${RCFILE} ${BASEDIR}/* ${BACKUPDIR})
251 BCKUP_COMM
=$
(rsync
-avz --progress ${BASEDIR}/* ${BACKUPDIR})
254 if [ "$BCKUP_COMM" ]; then
255 echo -e "BASE directory:\t\t$BASEDIR"
256 echo -e "BACKUP directory:\t$BACKUPDIR"
257 echo; echo "BACKUP COMPLETED"
261 function editnote
() {
263 # shellcheck disable=SC2155
264 local OK
=$
(check_noteID
"$NOTE")
266 echo "invalid note \"$NOTE\""
267 echo "Use the note ID that you can fetch after listing your notes"
271 # shellcheck disable=SC2016,SC2086
272 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
273 # shellcheck disable=SC2016,SC2086
274 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
275 if [ "$TITLE" ]; then
276 echo "editing note $TITLE"
277 # shellcheck disable=SC2086,SC2091
278 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
280 echo "note not found"
284 function listnotes
() {
285 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
286 if [[ $
(ls -A "$NOTESDIR") ]]; then
287 if [ $PLAIN == false
]; then
288 echo "listing all notes"
291 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
292 for i
in "${NOTESDIR}"/*; do
293 # shellcheck disable=SC2155
294 local fname
=$
(basename $i)
295 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
296 # shellcheck disable=SC2016,SC2086
297 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
298 # shellcheck disable=SC2016,SC2086
299 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
300 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
303 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
307 # remove eventually existing temp DB file
308 if [[ -f $TMPDB ]]; then
313 if [ "all" == "$NOTE" ]; then
314 echo "You're going to delete all notes."
315 read -r -p "Do you wish to continue? (y/N) " ANSWER
318 # shellcheck disable=SC2086
319 $JQ 'del(.notes[])' $DB > $TMPDB
320 # shellcheck disable=SC2086
322 # shellcheck disable=SC2086
324 echo "Deleted all notes"
327 echo "Aborting, no notes were deleted."
332 # shellcheck disable=SC2155
333 local OK
=$
(check_noteID
"$NOTE")
335 echo "invalid note \"$NOTE\""
336 echo "Use the note ID that you can fetch after listing your notes"
340 # shellcheck disable=SC2016,SC2086
341 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
342 # shellcheck disable=SC2016,SC2086
343 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
344 if [ "$TITLE" ]; then
345 # shellcheck disable=SC2016,SC2086
346 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
347 # shellcheck disable=SC2086
350 echo "Deleted note $TITLE"
352 echo "note not found"
357 function shownote
() {
360 # shellcheck disable=SC2155
361 local OK
=$
(check_noteID
"$NOTE")
363 echo "invalid note \"$NOTE\""
364 echo "Use the note ID that you can fetch after listing your notes"
368 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
371 $PAGER ${NOTESDIR}/${FILE}
374 # shellcheck disable=SC2006
375 GOPT
=$
(getopt
-o hvpla
::e
::d
::s
:: --long help,version
,list
,plain
,userconf
,backup
::,add
::,edit
::,delete
::,show
:: -n 'bash-notes' -- "$@")
377 # shellcheck disable=SC2181
378 if [ $?
!= 0 ] ; then helptext
>&2 ; exit 1 ; fi
380 # Note the quotes around `$GOPT': they are essential!
391 echo $BASENAME v
${VERSION}
405 read -r -p "Title: " TITLE
418 read -r -p "Note ID: " NOTE
431 read -r -p "Note ID: " NOTE
444 read -r -p "Note ID: " NOTE
456 # shellcheck disable=SC2317
457 echo "config exported to \"$RCFILE\""
458 # shellcheck disable=SC2317
464 read -r -p "Backup Dir: " BDIR