How to create a Shell Script to calculate Loan payment

Posted on 6:53 PM by Bharathvn

The Code

# loancalc - Given a principal loan amount, interest rate, and
# duration of loan (years), calculates the per-payment amount.

# Formula is: M = P * ( J / (1 - (1 + J) ** -N))
# where P = principal, J = monthly interest rate, N = duration (months)
# Users typically enter P, I (annual interest rate), and L (length, years)


if [ $# -ne 3 ] ; then
echo "Usage: $0 principal interest loan-duration-years" >&2
exit 1

P=$1 I=$2 L=$3
J="$(scriptbc -p 8 $I / \(12 \* 100 \) )"
N="$(( $L * 12 ))"
M="$(scriptbc -p 8 $P \* \($J / \(1 - \(1 + $J\) \^ -$N\) \) )"

# Now a little prettying up of the value:

dollars="$(echo $M | cut -d. -f1)"
cents="$(echo $M | cut -d. -f2 | cut -c1-2)"

cat << EOF
A $L year loan at $I% interest with a principal amount of $(nicenumber $P 1 )
results in a payment of \$$dollars.$cents each month for the duration of
the loan ($N payments).

exit 0

Running the Script
This minimalist script expects three parameters to be specified: the amount of the loan, the interest rate, and the duration of the loan (in years).

The Results
I've been eyeing a lovely new Volvo XC90, and I'm curious how much my payments would be if I bought the car. The Volvo is about $40,000 out the door, and the latest interest rates are running at 6.75 percent for an auto loan. I'd like to see how much difference there is in total payments between a four-year and five-year car loan. Easily done:

$ loancalc 40000 6.75 4
A 4 year loan at 6.75% interest with a principal amount of 40,000
results in a payment of $953.21 each month for the duration of
the loan (48 payments).
$ loancalc 40000 6.75 5
A 5 year loan at 6.75% interest with a principal amount of 40,000
results in a payment of $787.33 each month for the duration of
the loan (60 payments).

If I can afford the slightly higher payments on the four-year loan, the car will be paid off and the overall amount of the loan (payment * number of payments) will be significantly cheaper. To calculate the exact savings, I can use Script #24, the interactive calculator:

$ calc '(787.33 * 60) - (953.21 * 48)'

This seems like a worthwhile savings. $1,485.72 would buy a nice little laptop!

Hacking the Script
Exploring the formula itself is beyond the scope of this book, but it's worth noting how even a complex mathematical formula can be implemented directly in a shell script.

The entire calculation could be solved using a single input stream to bc, because that program also supports variables. However, being able to manipulate the intermediate values within the script itself proves beyond the capabilities of the bc command alone. For an example of just such a manipulation, here is the code that splits the resultant monthly payment value and ensures that it's presented as a properly formatted monetary value:

dollars="$(echo $M | cut -d. -f1)"
cents="$(echo $M | cut -d. -f2 | cut -c1-2)"

As it does in so many scripts in this book, the cut command proves tremendously useful here. The second line of this code grabs the portion of the monthly payment value that follows the decimal point and then chops off anything after the second character. Ideally, this modification would round up or down according to the value of the third cents character, rather than doing what is considered a floor function. And this change is surprisingly easy to accomplish: Just add 0.005 cents to the value before truncating the cents amount at two digits.

This script could also really do with a way to prompt for each field if no parameters are specified. And a more sophisticated and useful version of this script would let a user specify any three parameters of the four (principal, interest rate, number of payments, and monthly payment amount) and have the script solve for the fourth value. That way, if you knew you could afford only $500 per month in payments, and that the maximum duration of a 6 percent auto loan was five years, you could ascertain the largest amount of principal that you could borrow.