从每月预测计算某一天的余额

时间:2017-04-07 05:29:27

标签: bash calculator

我有一份每月支出的清单,看起来像这样:

1st - Car - 100

第3名 - 租金 - 400

7日 - 天然气和电力 - 51

8th - Phone - 12

17th - Internet - 30.74

21 - 保险 - 45.21

27th - Patreon - 2

第28页 - 水 - 12.9

以上是一个例子,但结构与我的每月支出相似。

假设我每个月23日都得到报酬。我可以写一个脚本(最好是在bash中),我可以输入我当前的银行余额,它会在$ CURRENT_DATE和当月22日之间减去我银行发出的所有内容吗?

所以我可以在这个月的任何时候运行这个脚本,快速浏览一下我有多少钱。

3 个答案:

答案 0 :(得分:0)

    public static DataTable GetFileByteArrayByID(int aID)
    {
        conn.Open();

        dbCommand = new OleDbCommand("SELECT Datei FROM Belege WHERE (ID = @ID)", conn);

        dbCommand.Parameters.Add("@ID", OleDbType.Integer).Value = aID;
        dbDataAdapter = new OleDbDataAdapter(dbCommand);
        DataTable resultDataTable = new DataTable();
        dbDataAdapter.Fill(resultDataTable);

        conn.Close();
        return resultDataTable;
    }

如果余额为1000

#!/bin/bash

PAYDAY=$2
B=$1

sed '/^$/d; s#\([0-9][0-9]*\)st#\1#g; s#\([0-9][0-9]*\)nd#\1#g; s#\([0-9][0-9]*\)th#\1#g; s#\([0-9][0-9]*\)rd#\1#g' | while read line
do 
   lday=$(echo $line | awk -F'-' '{print $1}')
   amount=$(echo $line | awk -F'-' '{print $3}')
   if [[ $lday -gt `date '+%d'` && $lday -lt $PAYDAY ]]; then
      B=`echo "$B - $amount" | bc`
      echo $B
   fi
done | tail -1

答案 1 :(得分:0)

这是一个带注释的bash解决方案,可能不是最优解决方案:

#! /bin/bash

if [[ -z ${1} ]]; then
    echo "Missing argument: current balance"
    exit 2
fi

# current balance comes in as the first argument, multiplied by 100 for the cent-value (bc for decimal)
current_balance=$(scale=0; echo "${1} * 100" | bc -l)
# current day without leading zero
current_day=$(date +"%-d")
# the last day of the current month
last_day_of_month=$(date -d "-$(date +%d) days +1 month" +"%d")
# the default target date is the 22.
target_day=22
offset=0

# if we're past the cut-off point, then we'll check until the 22. of next month
if (( ${current_day} > 22 )); then
    target_day=$(( ${last_day_of_month} + 22 ))
    offset=$(( $last_day_of_month ))
fi

# set the outgoings to be on the index of their date as their 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 over the days until we hit the target
for (( i=${current_day}; i < ${target_day}; i += 1 )); do
    # decrement by the offset (either by 0 or the amount of days in month)
    if (( $i > ${offset} )); then 
        index=$(( $i - $offset ))
    else
        index=$(( $i ))
    fi
    # if no outgoings for this day, continue
    if [[ -z "${outgoings[index]}" ]]; then continue; fi
    # calculate the current balance after this days outgoings
    current_balance=$(( $current_balance - ${outgoings[index]} ))
done

# divide the current left over balance by 100 to get the euro/dollar/whatever amount
left_over=$(echo "scale=2; ${current_balance} / 100" | bc -l)

echo "Extra money: ${left_over}"

答案 2 :(得分:0)

完整的awk解决方案是:

awk -v sal="1000" -v datnow=$(date +%d) -v dat=23 -F\- '{ dat=dat--;if ( $0 != "" && +$1 >= datnow && +$i <= dat ) { vals[+$1]=+$3 } } END { for ( i in vals ) { sal=sal-vals[i] } print sal  }' balances

其中balance是包含数据的文件。

我们将工资作为sal(示例1000)传递,然后将支付日作为dat传递。我们还将当前日期作为datnow传递。

我们将工资日减少一行,检查该行是否为空白,检查文件中的日期是否大于或等于现在且小于或等于支付日。如果是这种情况,则创建一个数组,并通过从salary中减去值来最终循环此数组以获得剩余余额。