This is the second, in a series, called "Fun with Shells".
This contribution is a simple bash shell that I wrote for calculating your car payments,
home mortgage or whatever you get on a loan.
If you look into the nuts and bolts of this program there are two fundamental things to be
learned here about shells.
First: You can call in variables, and have it output a simple help if you forget them.
And second: You can code up a simple mathematical equation into a shell to have it perform
a quick calculation, such as amortization.
#!/bin/bash
# program myamort
# version 1.0
# written by Andrew V. Newman
# last modified Wed Feb 25 17:37:04 MST 2004
# program to calculate monthly payment on a loan
# This is a default response if you do not give any input variables
if [ ${#} -lt "2" ]; then
echo "Usage: myamort 'LOAN' 'APR' [TRM] [CMP] [PPY]"
echo " where LOAN is amount in dollars"
echo " APR is in fractions of 100 (i.e. 6.5% is 6.5)"
echo " (following are optional:)"
echo " TRM is term in years [30 is default]"
echo " CMP is compounds per year [12 is default--monthly]"
echo " PPY is payments per year [12 is default]"
echo " Example:"
echo " myamort 150000 6.5 30 12 12"
exit 1
fi
# These need to be input every time
LOAN=312000 # Amount of loan
APR=0.06 # Annual Percentange Rate
LOAN=$1 # Amount of loan
APR=`echo $2 | awk '{print $1/100}' ` # Annual Percentange Rate
# These may or may not be input
TRM=30 # Term of loan in years
if [ $3 ]; then TRM=$3 ; fi
CMP=12 # Number of comounds/year
if [ $4 ]; then CMP=$4 ; fi
PPY=12 # Number of payments/year
if [ $5 ]; then PPY=$5 ; fi
# Calculate Monthly Payment
MPMT=` echo $APR | awk '{
R=(1+$1/CMP)^(CMP/PPY)}
END {printf "%8.2f", LOAN*(R^(TRM*PPY)*(R-1))/(R^(TRM*PPY)-1)
}' LOAN=$LOAN NPMTS=$NPMTS CMP=$CMP PPY=$PPY TRM=$TRM `
LOANK=`echo $LOAN | awk '{print "\$" $1/1000 "K"}'`
APR100=`echo $APR | awk '{print $1*100}'`
printf "For:\n LOAN: $LOANK\n APR $APR100 over $TRM years\n $PPY pmnt/yr at: \$%7.2f\n" $MPMT
# exit cleanly
exit 0
It may be easier to just download a copy (right click).
Once you have a copy on your system, all you need to do is follow the instructions at the bottom of
myforecast to get it up and running on your machine.
In the program, you will notice that there are several variables that are taken as a default value.
These are the default values that I found necessary when calculating my mortgage and do not
necessarily represent the values that you need (note: that amount in the example is not what I paid
for my own house!).
Ok, now that you have the program in the right spot and permission set, give it a shot.
Try running the following commands:
(Do not type in comments in blue)
% myamort # will give you the usage
% myamort 150000 6.125 # $150K at 6.125 APR
% myamort 150000 6.125 15 # $150K at 6.125 APR over 15 years
% myamort 150000 6.125 15 1e10 # $150K at 6.125 APR over 15 years compounded continuously
% myamort 150000 6.125 15 12 26.125 # $150K at 6.125 APR over 15 years compounded monthly, but payments made biweekly
Please email me if you have any comments or suggestions!