-
Notifications
You must be signed in to change notification settings - Fork 1
/
fee-breakup
executable file
·59 lines (39 loc) · 1.45 KB
/
fee-breakup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# path to the file to read fee breakups from
breakupfile='/usr/share/deltaT1/feeBreakup.txt'
echo $(id -nu)
# Read the total amount from user
[[ -n $1 ]] && total_amount=$1 || { echo "Usage: feeBreakup total_amount"; exit 1; }
# Read feeBreakup.txt and generate arrays of
# category names and corresponding percentages
categories=()
percentages=()
n=0
readarray lines < "$breakupfile"
for entry in "${lines[@]}"; do
IFS=' ' read -r -a words <<<"$entry"
categories=(${categories[@]} ${words[0]})
percentages=(${percentages[@]} ${words[1]%%'%'})
((n=n+1))
done
# Write to user's fees.txt
feesfile=$HOME/fees/fees.txt
echo $'\n'"Fee payment on $(date -Is)" >> "$feesfile"
# Iterate over categories, updating the cumulative
# amount and writing the transaction details at the
# same time
for ((i=0;i<n;i++)); do
category=${categories[i]}
# compute amount for i'th category
# Bash does not support floating-point arithmetic.
# Rather than require yet another utility to be
# installed, we will simply disregard the decimal
# point.
amount=$(( $total_amount * ${percentages[i]} / 100 ))
# Update cumulative amount
oldamount=$(grep -m 1 "$category" "$feesfile" | grep -E -o '[0-9]+')
newamount=$(($oldamount+$amount))
sed -i "0,/${category}/ s/${category} ${oldamount}/${category} ${newamount}/" "$feesfile"
# Write transaction details
echo "${categories[i]} ${amount}" | tee -a "$feesfile"
done