From 61c919904c5484b6207d264b351d31b627f25de4 Mon Sep 17 00:00:00 2001 From: danix Date: Sat, 25 Mar 2023 08:29:00 +0100 Subject: [PATCH] Added check to verify if note argument is an integer modified: notes.sh --- notes.sh | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/notes.sh b/notes.sh index 91a3867..c3390b6 100644 --- a/notes.sh +++ b/notes.sh @@ -56,6 +56,19 @@ fi # create PIDFILE echo $PID > $PIDFILE +# check if input is a number, returns false or the number itself +function check_noteID() { + IN=$1 + case $IN in + ''|*[!0-9]*) + return 1 + ;; + *) + echo $IN + ;; + esac +} + function helptext() { echo "Usage:" echo " $0 [PARAMS] ..." @@ -113,8 +126,15 @@ function listnotes() { } function editnote() { - TITLE=$($JQ --arg i $1 '.notes[] | select(.id == $i) | .title' $DB) - FILE=$($JQ -r --arg i $1 '.notes[] | select(.id == $i) | .file' $DB) + NOTE=$1 + local OK=$(check_noteID $NOTE) + if [ ! $OK ]; then + echo "invalid note \"$NOTE\"" + exit 1 + fi + + TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) + FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) if [ "$TITLE" ]; then echo "editing note $TITLE" $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${FILE}) @@ -125,14 +145,17 @@ function editnote() { } function datenote() { - echo "edit date for note \"${1}\"" + NOTE=$1 + local OK=$(check_noteID $NOTE) + [ $OK ] && echo "editing date for note $OK" || echo "invalid note \"$NOTE\"" # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T) } function rmnote() { NOTE=$1 - echo "removing note $NOTE" + local OK=$(check_noteID $NOTE) + [ $OK ] && echo "removing note $OK" || echo "invalid note \"$NOTE\"" } function export_config() { -- 2.20.1