I'm writing a program for CodeWars that requires me to return 2 specific variables with a space between them.
The following is the script that I am trying to execute
!/bin/bash
nbMonths()
{
printf "%.0f\n" $(
bc <<< "
scale=5
osum=$1
nsum=$2
sav=0
lper=($4 / 100)
cmon=0
diff = osum + sav - nsum
while( diff < 0 ) {
osum -= ( osum * lper )
nsum -= ( nsum * lper )
sav += $3
cmon += 1
scale=0
if( (cmon%2) == 1) {
scale=5
lper += 0.005
}
scale=5
diff = osum + sav - nsum
}
print cmon, diff
")
}
nbMonths $1 $2 $3 $4
For the following input to the bash script
bash nameOfProg.sh 2000 8000 1000 1.5
The result it shows is
6766
While I would like it to display it as
6 766
There are 2 questions I have regarding this,
Is there a way to make printf to print in the above format?
I've noticed that
print cmon, \", \", diff
results in6, 766
Are there are escape sequences(like
\",\"
) that I could use? If so, where could I find it since there isn't a man page forprint
.
Please note that the reason I've used %0.f
in the printf
statement is that I need the last value (from diff) to be rounded off.