計算貸款的shell指令碼 目的就是在溫度轉換上再進一步的瞭解數學計算。 代碼: 01#!/bin/sh02 03# loancalc.sh -- 指定貸款的本金、稅率、年限04 05# 公式: M = P * (J / (1- (1 + J)** - N))06# 其中, P = 本金、J = 每月稅率、N = 年限(月表示)07# 使用者輸入P、I(年利率)、L(長度,年份)08 09# 注意,公式中後面的 (1 + J)** - N 是一個整體,表示指數10# 比如,(1 + 1)** - 2 就是 2 ^ -2,相當於 1/2*2 = 0.2511 12#source library.sh13 14if [ $# -ne 3 ]; then15 echo "Usage: `basename $0` principal interest loan-duration-years" >&216 exit 117fi18 19# 這兒用到的scriptbc.sh是第九個指令碼的程式20# 傳給它的參數是一個公式,部分運算子號需要轉義21# 轉義的有: ( ) * ^22P=$1 I=$2 L=$323J="$(scriptbc.sh -p 8 $I/\(12 \* 100\))"24N="$(($L*12))"25M="$(scriptbc.sh -p 8 $P\*\($J/\(1-\(1+$J\)\^-$N\)\))"26 27dollars="$(echo $M | cut -d. -f1)"28cents="$(echo $M | cut -d. -f2 | cut -c1-2)"29 30# newnicenum.sh是第4個指令碼中的程式31cat << EOF32A $L year loan at $I% interest with a principal amount of $(newnicenum.sh $P 1)33results in a payment of \$$dollars.$cents each month for the duration of34the loan($N payments).35EOF36 37exit 0運行結果:1$ loancalc 40000 6.75 42A 4 year loan at 6.75% interest with a principal amount of 40,0003results in a payment of $953.21 each month for the duration of4the loan (48 payments).5 6$ loancalc 40000 6.75 57A 5 year loan at 6.75% interest with a principal amount of 40,0008results in a payment of $787.33 each month for the duration of9the loan (60 payments).分析指令碼: 這個指令碼比較完整的展示了一個數學公式的運算。