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:
parent
f77bc126ac
commit
98eb53a718
1 changed files with 13 additions and 9 deletions
22
histd.sh
22
histd.sh
|
@ -3,6 +3,8 @@
|
||||||
# Histd: how I spent this day.
|
# Histd: how I spent this day.
|
||||||
# A simple but useful personal diary CLI utility.
|
# A simple but useful personal diary CLI utility.
|
||||||
|
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
# Check if a text editor is specified
|
# Check if a text editor is specified
|
||||||
function _check_editor {
|
function _check_editor {
|
||||||
if [[ $EDITOR == "" ]]; then
|
if [[ $EDITOR == "" ]]; then
|
||||||
|
@ -16,26 +18,28 @@ function _check_editor {
|
||||||
function edit_note {
|
function edit_note {
|
||||||
# Create dirs (base_dir/year/month)
|
# Create dirs (base_dir/year/month)
|
||||||
WORKDIR="$BASE_DIR/$(date +%Y)/$(date +%m)"
|
WORKDIR="$BASE_DIR/$(date +%Y)/$(date +%m)"
|
||||||
mkdir -p $WORKDIR
|
mkdir -p "$WORKDIR"
|
||||||
|
|
||||||
# Generate file name
|
# Generate file name
|
||||||
PATH_TO_FILE="$WORKDIR/$(date +%d).md"
|
PATH_TO_FILE="$WORKDIR/$(date +%d).md"
|
||||||
|
|
||||||
# Open editor
|
# Open editor
|
||||||
_check_editor
|
_check_editor
|
||||||
cd $BASE_DIR
|
(
|
||||||
$EDITOR $PATH_TO_FILE
|
cd $BASE_DIR
|
||||||
cd - > /dev/null
|
$EDITOR "$PATH_TO_FILE"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create an archive with all notes
|
# Create an archive with all notes
|
||||||
function backup {
|
function backup {
|
||||||
ARCHIVE_PATH=~/histd-$(date +%F).tar.xz
|
ARCHIVE_PATH=~/histd-$(date +%F).tar.xz
|
||||||
cd $BASE_DIR
|
(
|
||||||
tar cfJ $ARCHIVE_PATH ../histd
|
cd $BASE_DIR
|
||||||
cd - > /dev/null
|
tar cfJ "$ARCHIVE_PATH" ../histd
|
||||||
|
)
|
||||||
|
|
||||||
echo Saved to $ARCHIVE_PATH
|
echo "Saved to $ARCHIVE_PATH"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Concatenate all files and prefix each with the filename.
|
# Concatenate all files and prefix each with the filename.
|
||||||
|
@ -54,7 +58,7 @@ function last {
|
||||||
_check_editor
|
_check_editor
|
||||||
LAST_FILE=$(find $BASE_DIR -type f -name "*.md" | tail -1)
|
LAST_FILE=$(find $BASE_DIR -type f -name "*.md" | tail -1)
|
||||||
cd $BASE_DIR
|
cd $BASE_DIR
|
||||||
$EDITOR $LAST_FILE
|
$EDITOR "$LAST_FILE"
|
||||||
cd - > /dev/null
|
cd - > /dev/null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue