Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up bash idioms, use modern interpolation style #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions alias
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#!/bin/bash
#!/usr/bin/env bash
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

# alias file for ledger from Getting Started With Ledger
# https://github.com/rolfschr/GSWL-ecosystem.git
#
# Source this to activate the variables and functions within:
# source ./alias

# Debug this with:
# DEBUG=true bash ./alias
# It will echo commands and line numbers.
DEBUG=${DEBUG:-false}

# When debugging use bash unofficial strict mode
# See http://redsymbol.net/articles/unofficial-bash-strict-mode/
$DEBUG && set -euo pipefail
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we always set euo pipefail? (i.e. not only when in debug mode?)


# Thanks https://stackoverflow.com/a/17805088
$DEBUG && export PS4='${LINENO}: ' && set -x

# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
LEDGER_ECOSYSTEM_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

LEDGER_ECOSYSTEM_DIR=$(dirname "$_") # needed for later
if [[ "$_" == "" && ! -z $BASH ]] ; then
LEDGER_ECOSYSTEM_DIR=$(dirname "$BASH_SOURCE")
fi

# This is the main command to invoke Ledger.
# --strict : Warn if account, tag or commodity is unknown.
@@ -28,19 +44,19 @@ alias ledconv="${LEDGER_ECOSYSTEM_DIR}/convert.py"
# When the latter are named consistently (ex:"apr2044_bank1.csv,
# may2044_bank1.csv") one can use simple aliases to always convert the latest
# CSV files. (Assuming monthly updates in this case.)
THIS_AMN=$(date +'%b' | tr '[:upper:]' '[:lower:]') # Abreviated Month Name, e.g. "apr"
export THIS_AMN=$(date +'%b' | tr '[:upper:]' '[:lower:]') # Abreviated Month Name, e.g. "apr"

GNU_DATE=$(date -d 'last year' 2>/dev/null)
GNU_DATE=$(date -d 'last year' 2>/dev/null) || true
if [[ "$GNU_DATE" == "" ]]; then
LAST_AMN=$(date -v-1m +'%b' | tr '[:upper:]' '[:lower:]') # "apr"
LAST_YEAR=$(date -v-1y +'%Y') # "2044"
FST_DOM_6MONTHS_AGO=`date -v-6m +"%Y/%m/01"`
FST_DOM_12MONTHS_AGO=`date -v-12m +"%Y/%m/01"`
FST_DOM_6MONTHS_AGO=$(date -v-6m +"%Y/%m/01")
FST_DOM_12MONTHS_AGO=$(date -v-12m +"%Y/%m/01")
else
LAST_AMN=$(date +'%b' -d 'last month' | tr '[:upper:]' '[:lower:]') # "apr"
LAST_YEAR=$(date +'%Y' -d 'last year') # "2044"
FST_DOM_6MONTHS_AGO=`date +"%Y/%m/01" -d "6 months ago"`
FST_DOM_12MONTHS_AGO=`date +"%Y/%m/01" -d "12 months ago"`
FST_DOM_6MONTHS_AGO=$(date +"%Y/%m/01" -d "6 months ago")
FST_DOM_12MONTHS_AGO=$(date +"%Y/%m/01" -d "12 months ago")
fi


@@ -50,6 +66,7 @@ if [[ "$LAST_AMN" == "dec" ]]; then
LAST_AMN_AND_YEAR="${LAST_AMN}${LAST_YEAR}"
fi
# First Day Of Month x Months Ago (may be used for specific Ledger reports)
export THIS_YEAR LAST_AMN LAST_YEAR LAST_AMN_AND_YEAR FST_DOM_6MONTHS_AGO FST_DOM_12MONTHS_AGO

function ledxact () {
FILE=misc.tmp.txt
@@ -140,4 +157,4 @@ alias lmmake="cat \$(listTmpFiles) > $ATMPFILE && ledger $LEDGERARGS -f $ATMPFIL
alias lmclean="rm ./tmp/*"

# Source this file in the current working directory
source alias.local 2>/dev/null
[[ -f alias.local ]] && source alias.local || true