• Welcome to Autism Forums, a friendly forum to discuss Aspergers Syndrome, Autism, High Functioning Autism and related conditions.

    Your voice is missing! You will need to register to get access to the following site features:
    • Reply to discussions and create your own threads.
    • Our modern chat room. No add-ons or extensions required, just login and start chatting!
    • Private Member only forums for more serious discussions that you may wish to not have guests or search engines access to.
    • Your very own blog. Write about anything you like on your own individual blog.

    We hope to see you as a part of our community soon! Please also check us out @ https://www.twitter.com/aspiescentral

Need some help on a Scripting project.

tkcartoonist

Tunes and Toons
So I'm working on a project that involves writing a PowerShell script for some kind of useful, everyday function. I remembered a bash script I typed up a while back that helps calculate interest to be paid back on a loan, and I wanted to convert it into a PowerShell-usable script. However, I'm having a bit of a go when it comes to reworking function and variable syntax to make the PowerShell program 1-to-1 with the original bash version. I was wondering if anyone could help me out here. I'll post the original bash code below to review.

Bash:
#!/bin/bash

# loan-calc: script to calculate monthly loan payments

PROGNAME="${0##*/}" # Use parameter expansion to get basename

usage () {
    cat <<- EOF
    Usage: $PROGNAME PRINCIPAL INTEREST MONTHS

    Where:

    PRINCIPAL is the amount of the loan.
    INTEREST is the APR as a number (7% = 0.07).
    MONTHS is the length of the loan's term.

    EOF
}

if (($# != 3)); then
    usage
    exit 1
fi

principal=$1
interest=$2
months=$3

bc <<- EOF
    scale = 10
    i = $interest / 12
    p = $principal
    n = $months
    a = p * ((i * ((1 + i) ^ n)) / (((1 + i) ^ n) - 1))
    print a, "\n"
EOF
 
and I wanted to convert it into a PowerShell-usable script.
Give this a go, even if it ends up with a bug or two it should give you a head start:


This cheat sheet might be handy too:

 
Have you considered using something a little OS-agnostic like Python, JS, etc?

I'd probably have a read a few docs on Powershell and Bash to port it over (I don't use either of them regularly enough) but you could always punch it into ChatGPT and just debug whatever parts it hallucinates. This could also be a great time to become fluent with the languages if that's your thing though, as the best way to learn a new language (IMO) is to just port your own code over to it.
 
Have you considered using something a little OS-agnostic like Python, JS, etc?

I'd probably have a read a few docs on Powershell and Bash to port it over (I don't use either of them regularly enough) but you could always punch it into ChatGPT and just debug whatever parts it hallucinates. This could also be a great time to become fluent with the languages if that's your thing though, as the best way to learn a new language (IMO) is to just port your own code over to it.
The project is specifically for a PowerShell script. Believe me, I'd rather just turn in the bash script and call it a day. I'm doing this for a class, and the unit we're currently working on relates to PowerShell (we were doing Python before).

Anyway, this is what I was able to cobble together for a conversion so far. I know this code doesn't work, but I'm still trying to go at it to get it to work.

Code:
#!/usr/bin/env pwsh

# loan-calc: script to calculate monthly loan payments

$usage = {
Usage: loan-calc PRINCIPAL INTEREST MONTHS

Where:

PRINCIPAL is the amount of the loan.
INTEREST is the APR as a number (7`% = 0.07).
MONTHS is the length of the loan`'s term.


}

if ($args.Length -lt 2) {
    $usage
    return
}

$principal=$args[0]
$interest=$args[1]
$months=$args[2]

scale = 10
$i = $interest / 12
$p = $principal
$n = $months
$a = $p * ($i * [Math]::Pow((1 + $i), $n) / [Math]::Pow((1 + $i), $n) - 1)
write-output $a
 

New Threads

Top Bottom