-1

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,

  1. Is there a way to make printf to print in the above format?

  2. I've noticed that print cmon, \", \", diff results in

    6, 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 for print.

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
tankris
  • 1
  • 5
  • 1
    looks to me, from your own evidence, that `print cmon, \" \", diff` would put a space there. I think that's what you want? your version was close, you just put a comma between the (escaped) quotes – erik258 Jul 28 '20 at 15:30
  • Oh man! That worked. I just did not understand what \", \" was. I noticed its use in different program and just used it here. Thank you so very much. Could you help me with what exactly is \" \"? Also, is there a man page for print? Thank you in advance – tankris Jul 28 '20 at 15:35
  • 1
    Include a [mre]. – oguz ismail Jul 28 '20 at 15:42
  • 3
    `print` is a command within `bc`, and the fact that `\"` does something is due to shell quoting rules and not anything to do with `bc`. And, to @oguzismail's point, you can replace the whole `bc` command with a one-liner that prints two numbers to show the problem. – Benjamin W. Jul 28 '20 at 15:46
  • @tankris see my answer for how a minimal example of this might have been provided. By pulling most of the logic out of the function for the purposes of this question, it keeps the conversation focused and makes it easy for folks to test out their recommendations without thinking too much about irrelevant stuff. unfortunately coming up with minimal reproducible examples is sometimes a tricky change of perspective , especially for newer programmers, but it's a great exercise – erik258 Jul 28 '20 at 15:56

2 Answers2

0

you can print a space in a bc print command by enquoting the space in double strings.

Since your bc input is already enquoted, you'll need to escape the quotes with backslashes so it doesn't end the here-string (data following <<<).

Here's a simple example:

$  bc <<<"print 1, \" \", 3, \" \", 4"; echo
1 3 4
erik258
  • 14,701
  • 2
  • 25
  • 31
0

While I would like it to display it as

Then:

  1. Remove printf

  2. Read bc documentation and note that strings start and end with double quotes.

  3. Learn about shell quoting. If you are inside " quotes, then to pass " you have to escape it.

    $ cat <<<"inside double quotes: literal quote: \" "
    inside double quotes: literal quote: "
    
  4. Print the space between numbers.

#!/bin/bash
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
"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 2
    If you are writing a 100% `bc` script program, then use the shebang `#!/usr/bin/env -S bc -l` and have the whole script handled by `bc` – Léa Gris Jul 28 '20 at 17:04