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 # Git binary only used if $USEGIT is true - See below
29 GIT
=${GIT:-/usr/bin/git}
30 # add options for your terminal. Remember to add the last option to execute
31 # your editor program, otherwise the script will fail.
32 # see example in the addnote function
33 TERM_OPTS
="--class notes --title notes -e "
34 # Setting PAGER here overrides whatever is set in your default shell
35 # comment this option to use your default pager if set in your shell.
36 PAGER
=${PAGER:-/usr/bin/more}
38 # set this to true to have output in plain text
39 # or use the -p option on the command line before every other option
41 # base directory for program files
42 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
43 # notes database in json format
45 # directory containing the actual notes
46 NOTESDIR
=${BASEDIR}/notes
50 # If you want to store your notes in a git repository set this to true
52 # Address of your remote repository
53 GITREMOTE
=${GITREMOTE:-""}
55 } # end set_defaults, do not change this line.
59 # Do not edit below this point
60 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
63 if [ ! -x "$JQ" ]; then
64 echo "jq not found in your PATH"
65 echo "install jq to continue"
69 # IMPORT USER DEFINED OPTIONS IF ANY
70 if [[ -f $RCFILE ]]; then
71 # shellcheck disable=SC1090
75 # We prevent the program from running more than one instance:
76 PIDFILE
=/var
/tmp
/$
(basename "$0" .sh
).pid
78 # Make sure the PID file is removed when we kill the process
79 trap 'rm -f $PIDFILE; exit 1' TERM INT
81 if [[ -r $PIDFILE ]]; then
82 # PIDFILE exists, so I guess there's already an instance running
83 # let's kill it and run again
84 # shellcheck disable=SC2046,SC2086
85 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
86 # should already be deleted by trap, but just to be sure
91 echo $PID > "$PIDFILE"
93 # Export config to file
94 function export_config
() {
95 if [ -r ${RCFILE} ]; then
96 echo "Backing up current '${RCFILE}'...."
97 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
99 echo "Writing '${RCFILE}'...."
100 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
101 |
grep -v set_defaults \
102 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
104 if [ -r ${RCFILE} ]; then
105 echo "Taking no further action."
108 echo "Could not write '${RCFILE}'...!"
113 # we should expand on this function to add a sample note and explain a little bit
114 # how the program works.
115 function firstrun
() {
116 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
119 echo "${BASENAME} configuration:
121 base directory: ${BASEDIR}/
122 notes archive: ${NOTESDIR}/
123 notes database: ${DB}
125 text editor: ${EDITOR}
126 terminal: ${TERMINAL}
130 echo "Now I'll create the needed files and directories."
131 read -r -p "Do you wish to continue? (y/N) " ANSWER
135 cat << __EOL__ > ${DB}
138 "version": "${VERSION}",
139 "dbversion": "${DBVERSION}"
147 echo; echo "All done, you can now write your first note."
150 echo "No changes made. Exiting"
156 # check for notes dir existance and create it in case it doesn't exists
157 if [[ ! -d $NOTESDIR ]]; then
158 # we don't have a directory. FIRST RUN?
161 # check if input is a number, returns false or the number itself
162 function check_noteID
() {
174 function helptext
() {
176 echo " $0 [PARAMS] [note ID]..."
178 echo "${BASENAME} parameters are:"
179 echo -e " -h | --help\t\t\t: This help text"
180 echo -e " -p | --plain\t\t\t: Output is in plain text"
181 echo -e "\t\t\t\t (without this option the output is formatted)"
182 echo -e "\t\t\t\t (this option must precede all others)"
183 echo -e " -l | --list\t\t\t: List existing notes"
184 echo -e " -a | --add=[\"<title>\"]\t: Add new note"
185 echo -e " -e | --edit=[<note>]\t\t: Edit note"
186 echo -e " -d | --delete=[<note> | all] : Delete single note or all notes at once"
187 echo -e " -s | --show=[<note>]\t\t: Display note using your favourite PAGER"
188 echo -e " -r | --restore=[<dir>]\t: Restore a previous backup from dir"
189 echo -e " -v | --version\t\t: Print version"
190 echo -e " --userconf\t\t\t: Export User config file"
191 echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder"
193 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
196 function configtext
() {
198 ${BASENAME} configuration is:
200 base directory: ${BASEDIR}/
201 notes archive: ${NOTESDIR}/
202 notes database: ${DB}
204 debug file: /tmp/debug_bash-note.log
206 text editor: ${EDITOR}
207 terminal: ${TERMINAL}
214 # this function returns a random 2 words title
215 function random_title
() {
218 DICT
=/usr
/share
/dict
/words
221 # total number of non-random words available
222 COUNT
=$
(cat $DICT |
wc -l)
224 # while loop to generate random words
227 RAND
=$
(od -N3 -An -i /dev
/urandom |
awk -v f
=0 -v r
="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}')
228 OUTPUT
+="$(sed `echo $RAND`"q
;d
" $DICT)"
230 [[ $X -eq 1 ]] && OUTPUT
+=" "
236 # returns true if the argument provided directory is a git repository
239 if [[ -d $DIR ]]; then
241 if git rev-parse
2>/dev
/null
; then
249 # sync local repository to remote
251 echo "Syncing notes with git on remote \"$GITREMOTE\""
256 # check for USEGIT and subsequent variables
257 if [[ $USEGIT && -n $GITREMOTE ]]; then
259 if ! is_git_repo
$BASEDIR; then
260 # initializing git repository
263 echo "adding all files to git"
265 $GIT commit
-m "$(basename $0) - initial commit"
266 $GIT remote add origin
$GITREMOTE
267 $GIT push
-u origin master
269 elif [[ $USEGIT && -z $GITREMOTE ]]; then
270 echo "GITREMOTE variable not set. reverting USEGIT to false"
275 # remove eventually existing temp DB file
276 if [[ -f $TMPDB ]]; then
280 RTITLE
=$
(random_title
)
281 [[ -z "$1" ]] && NOTETITLE
="$RTITLE" || NOTETITLE
="$1"
282 echo "adding new note - \"$NOTETITLE\""
283 # shellcheck disable=SC2086
284 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
285 # [ "" == $LASTID ] && LASTID=0
286 NOTEID
=$
(( LASTID
+ 1 ))
287 # shellcheck disable=SC2086
288 touch ${NOTESDIR}/${NOW}
289 # shellcheck disable=SC2016
290 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
291 # shellcheck disable=SC2086
293 # example for alacritty:
294 # alacritty --class notes --title notes -e /usr/bin/vim ...
295 # shellcheck disable=SC2086,SC2091
296 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
298 function backup_data
() {
300 echo "backing up data in $BACKUPDIR"
303 if [ -d $BACKUPDIR ]; then
304 if [ $
(/bin
/ls -A $BACKUPDIR) ]; then
305 echo "$BACKUPDIR is not empty. Cannot continue"
308 echo "$BACKUPDIR is ok. Continuing!"
311 # BACKUPDIR doesn't exists
312 echo "$BACKUPDIR doesn't exists"
313 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
319 echo "No changes made. Exiting"
324 # ok, we have a backup directory
325 if [ -r $RCFILE ]; then
326 BCKUP_COMM
=$
(rsync
-avz --progress ${RCFILE}* ${BASEDIR}/ ${BACKUPDIR})
328 BCKUP_COMM
=$
(rsync
-avz --progress ${BASEDIR}/ ${BACKUPDIR})
331 if [ "$BCKUP_COMM" ]; then
332 echo -e "All files backed up."
333 echo -e "BACKUP directory:\t$BACKUPDIR"
334 tree
$BACKUPDIR |
$PAGER
335 echo; echo "BACKUP COMPLETED"
339 function backup_restore
() {
341 echo "restoring backup from $BACKUPDIR"
342 echo "This will overwrite all your notes and configurations with the backup."
343 read -r -p "Do you want to continue? (y/N) " ANSWER
347 BACKUPRC
=$
(basename $RCFILE)
348 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
349 if [ -r ${RCFILE} ]; then
350 echo "Backing up current '${RCFILE}'...."
351 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
353 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
355 # restoring notes directory
356 if [ -d $BACKUPDIR/notes
]; then
357 if [ $
(/bin
/ls -A $NOTESDIR) ]; then
358 rm --verbose $NOTESDIR/*
360 cp -r --verbose $BACKUPDIR/notes
$BASEDIR
363 BACKUPDB
=$
(basename $DB)
364 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
365 if [ -r ${DB} ]; then
366 echo "Backing up current '${DB}'...."
367 mv -f ${DB} ${DB}.$
(date +%Y
%m
%d_
%H
%M
)
369 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
371 # restoring git repo subdirectory
372 if [ -d $BACKUPDIR/.git
]; then
373 if [ /bin
/ls -A ${BASEDIR}/.git
]; then
374 rm -rf ${BASEDIR}/.git
376 cp -r --verbose ${BACKUPDIR}/.git
${BASEDIR}/
380 echo "No changes made. Exiting"
386 function editnote
() {
388 # shellcheck disable=SC2155
389 local OK
=$
(check_noteID
"$NOTE")
391 echo "invalid note \"$NOTE\""
392 echo "Use the note ID that you can fetch after listing your notes"
396 # shellcheck disable=SC2016,SC2086
397 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
398 # shellcheck disable=SC2016,SC2086
399 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
400 if [ "$TITLE" ]; then
401 echo "editing note $TITLE"
402 # shellcheck disable=SC2086,SC2091
403 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
405 echo "note not found"
409 function listnotes
() {
410 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
411 if [[ $
(ls -A "$NOTESDIR") ]]; then
412 if [ $PLAIN == false
]; then
413 echo "listing all notes"
416 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
417 for i
in "${NOTESDIR}"/*; do
418 # shellcheck disable=SC2155
419 local fname
=$
(basename $i)
420 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
421 # shellcheck disable=SC2016,SC2086
422 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
423 # shellcheck disable=SC2016,SC2086
424 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
425 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
428 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
432 # remove eventually existing temp DB file
433 if [[ -f $TMPDB ]]; then
438 if [ "all" == "$NOTE" ]; then
439 echo "You're going to delete all notes."
440 read -r -p "Do you wish to continue? (y/N) " ANSWER
443 # shellcheck disable=SC2086
444 $JQ 'del(.notes[])' $DB > $TMPDB
445 # shellcheck disable=SC2086
447 # shellcheck disable=SC2086
449 echo "Deleted all notes"
452 echo "Aborting, no notes were deleted."
457 # shellcheck disable=SC2155
458 local OK
=$
(check_noteID
"$NOTE")
460 echo "invalid note \"$NOTE\""
461 echo "Use the note ID that you can fetch after listing your notes"
466 # shellcheck disable=SC2016,SC2086
467 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
468 # shellcheck disable=SC2016,SC2086
469 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
470 if [ "$TITLE" ]; then
471 # shellcheck disable=SC2016,SC2086
472 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
473 # shellcheck disable=SC2086
476 echo "Deleted note $TITLE"
480 echo "note not found"
486 function shownote
() {
489 # shellcheck disable=SC2155
490 local OK
=$
(check_noteID
"$NOTE")
492 echo "invalid note \"$NOTE\""
493 echo "Use the note ID that you can fetch after listing your notes"
497 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
500 $PAGER ${NOTESDIR}/${FILE}
503 # shellcheck disable=SC2006
504 GOPT
=$
(getopt
-o hvplr
::a
::e
::d
::s
:: --long help,version
,list
,plain
,userconf
,sync
,restore
::,backup
::,add
::,edit
::,delete
::,show
:: -n 'bash-notes' -- "$@")
506 # shellcheck disable=SC2181
507 if [ $?
!= 0 ] ; then helptext
>&2 ; exit 1 ; fi
509 # Note the quotes around `$GOPT': they are essential!
520 echo $BASENAME v
${VERSION}
534 read -r -p "Title: " TITLE
547 read -r -p "Note ID: " NOTE
560 read -r -p "Note ID: " NOTE
573 read -r -p "Note ID: " NOTE
586 read -r -p "Backup Dir: " RDIR
602 # shellcheck disable=SC2317
603 echo "config exported to \"$RCFILE\""
604 # shellcheck disable=SC2317
610 read -r -p "Backup Dir: " BDIR
631 if [ $
(check_noteID
$arg) ]; then