From 98eb53a718abb8d4d75dff4d0fac0167fd107eeb Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Sun, 15 Dec 2024 22:11:53 +0500 Subject: [PATCH] Fix small issues reported by shellcheck - Use subshells instead of "cd -". - Use quotes around variables. - Add "set -eo pipefail" to catch errors. --- histd.sh | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/histd.sh b/histd.sh index 5a1da18..fbb6436 100755 --- a/histd.sh +++ b/histd.sh @@ -3,6 +3,8 @@ # Histd: how I spent this day. # A simple but useful personal diary CLI utility. +set -eo pipefail + # Check if a text editor is specified function _check_editor { if [[ $EDITOR == "" ]]; then @@ -16,26 +18,28 @@ function _check_editor { function edit_note { # Create dirs (base_dir/year/month) WORKDIR="$BASE_DIR/$(date +%Y)/$(date +%m)" - mkdir -p $WORKDIR + mkdir -p "$WORKDIR" # Generate file name PATH_TO_FILE="$WORKDIR/$(date +%d).md" # Open editor _check_editor - cd $BASE_DIR - $EDITOR $PATH_TO_FILE - cd - > /dev/null + ( + cd $BASE_DIR + $EDITOR "$PATH_TO_FILE" + ) } # Create an archive with all notes function backup { ARCHIVE_PATH=~/histd-$(date +%F).tar.xz - cd $BASE_DIR - tar cfJ $ARCHIVE_PATH ../histd - cd - > /dev/null + ( + cd $BASE_DIR + tar cfJ "$ARCHIVE_PATH" ../histd + ) - echo Saved to $ARCHIVE_PATH + echo "Saved to $ARCHIVE_PATH" } # Concatenate all files and prefix each with the filename. @@ -54,7 +58,7 @@ function last { _check_editor LAST_FILE=$(find $BASE_DIR -type f -name "*.md" | tail -1) cd $BASE_DIR - $EDITOR $LAST_FILE + $EDITOR "$LAST_FILE" cd - > /dev/null }