bash - calculate balance on certain day from monthly projection -
i have list of monthly outgoings looks this:
1st - car - 100
3rd - rent - 400
7th - gas , electricity - 51
8th - phone - 12
17th - internet - 30.74
21st - insurance - 45.21
27th - patreon - 2
28th - water - 12.9
the above example similar in structure monthly outgoings.
let's paid on 23rd of every month. write script (preferably in bash) input current bank balance , subtract that's coming out of bank between $current_date , 22nd of month?
so run script time in month , quick glance @ how surplus money have.
here's annotated bash solution, not optimal solution:
#! /bin/bash if [[ -z ${1} ]]; echo "missing argument: current balance" exit 2 fi # current balance comes in first argument, multiplied 100 cent-value (bc decimal) current_balance=$(scale=0; echo "${1} * 100" | bc -l) # current day without leading 0 current_day=$(date +"%-d") # last day of current month last_day_of_month=$(date -d "-$(date +%d) days +1 month" +"%d") # default target date 22. target_day=22 offset=0 # if we're past cut-off point, we'll check until 22. of next month if (( ${current_day} > 22 )); target_day=$(( ${last_day_of_month} + 22 )) offset=$(( $last_day_of_month )) fi # set outgoings on index of date cent-values outgoings=() outgoings[1]=10000 outgoings[3]=40000 outgoings[7]=5100 outgoings[8]=1200 outgoings[17]=3074 outgoings[21]=4521 outgoings[27]=200 outgoings[28]=1290 # iterate on days until hit target (( i=${current_day}; < ${target_day}; += 1 )); # decrement offset (either 0 or amount of days in month) if (( $i > ${offset} )); index=$(( $i - $offset )) else index=$(( $i )) fi # if no outgoings day, continue if [[ -z "${outgoings[index]}" ]]; continue; fi # calculate current balance after days outgoings current_balance=$(( $current_balance - ${outgoings[index]} )) done # divide current left on balance 100 euro/dollar/whatever amount left_over=$(echo "scale=2; ${current_balance} / 100" | bc -l) echo "extra money: ${left_over}"
Comments
Post a Comment