Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Loan Payment Calculator in Bash

Submitted by on October 13, 2016 – 5:53 pm

Just a little bit of fun with Bash: a loan payment calculator. Nothing fancy here. Maybe I’ll add the amortization schedule to it later…

#!/bin/bash
principal=$(echo  | sed -r 's/[,]//g')
apr=
life=

d() {
	sed ':a;s/\B[0-9]\{3\}\>/,&/;ta'
}

if [ -z "${principal}" ] || [ -z "${apr}" ] || [ -z "${life}" ]
then
	exit 1
fi

apr="$(echo "scale=6; ${apr} / (100*12)" | bc -q)"
payments=$(( ${life} * 12 )) 
c="${principal} * ( ${apr} * ((1 + ${apr}) ^ ${payments} )) / ( ((1 + ${apr}) ^ ${payments}) - 1)"
payment="$(echo "scale=2; $${c}" | bc -q)"
interest="$(echo "scale=2; ${payment} * ${payments} - ${principal}" | bc -q)"
echo "You'll be making ${payments} monthly payments of $`echo ${payment} | d` to pay off $`echo ${principal} | d` at %, paying $`echo ${interest} | d` in interest"

Sample run:
(root:brocksamson)-(bg:0)-(~)# loancalc 30,000.00 4.75 5
You'll be making 60 monthly payments of $562.63 to pay off $30,000.00 at 4.75%, paying $3,757.80 in interest

 

Print Friendly, PDF & Email

Leave a Reply