• Feeling isolated? You're not alone.

    Join 20,000+ people who understand exactly how your day went. Whether you're newly diagnosed, self-identified, or supporting someone you love – this is a space where you don't have to explain yourself.

    Join the Conversation → It's free, anonymous, and supportive.

    As a member, you'll get:

    • A community that actually gets it – no judgment, no explanations needed
    • Private forums for sensitive topics (hidden from search engines)
    • Real-time chat with others who share your experiences
    • Your own blog to document your journey

    You've found your people. Create your free account

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
 
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