Fix small issues reported by shellcheck

- Use subshells instead of "cd -".
- Use quotes around variables.
- Add "set -eo pipefail" to catch errors.
This commit is contained in:
Ivan R. 2024-12-15 22:11:53 +05:00
parent f77bc126ac
commit 98eb53a718
Signed by: lumin
GPG key ID: E0937DC7CD6D3817

View file

@ -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
}