1

I am having a problem with the bc command. The output generated is:

(standard_in) 2: syntax error

Here I post my code, any suggestions will be appreciated.

value="100%"          # the threshold to cause a break out
kb="kB/s"
mb="MB/s"             # strings to mask out
conversor=1024        # kb->mb conversion
contador=0            # initalize our total in megabytes
for ((x=3; x<=${#array[@]}; x+=5)); do
    paraula=${array[$x]};

    if [[ $paraula =~ .*kB.* ]]; then
        paraula=${paraula%$kb}
        paraula=$(echo "scale=4; $paraula/$conversor" | bc) # convert to $mb
        contador=$(echo "scale=4; $contador+$paraula" | bc)
        echo "Counting: "$contador
    else
        paraula=${paraula%$mb}
        contador=$(echo "scale=4; $contador+$paraula" | bc)
        echo "Counting: "$contador
    fi

    if [[ " ${array[$x]} " =~ " $value " ]]; then
        break;
    fi
    echo "Value : ${array[$x]} ";

done 

echo "final value: $contador"

then, the error on the terminal after execute the script is

error

6
  • 1
    Can you simplify this to a minimal reproducible example? You can probably get the same error from a single line of code that does nothing but invoke bc. Commented Jun 1, 2017 at 18:06
  • Consider using bash -x yourscript to log everything the script does, to find the place where it first goes wrong. If you want the tracking to be a bit easier, you can assign a value to PS4 that includes line number; for example, all on one line you could run: PS4=':${LINENO}+' bash -x yourscript. (Note that for security reasons, PS4 has to be set inside your script itself in very new versions of bash if that script is running as root). Commented Jun 1, 2017 at 18:07
  • BTW, please add the above-requested logs as text to the question, not screenshots. See the accepted answer to Why not to upload images of code on SO when asking a question? Commented Jun 1, 2017 at 18:10
  • Can you echo the values of paraula and contador before you feed it to bc? Commented Jun 1, 2017 at 18:13
  • Jesuuuuuuus, that was the problem. On my last row, I added to the counting a %. Thank you so much! Commented Jun 1, 2017 at 18:17

1 Answer 1

2

The issue is your last line doesn't have a newline in it, when piped to bc.

This works fine.

$ echo "scale=4; 2046/1024" | bc
1.9980
$ echo -e "scale=4; 2046/1024\n" | bc
1.9980

This is broke

$ echo -n "scale=4; 2046/1024" | bc
(standard_in) 1: syntax error

I think you can change to

contador=$(echo -e "scale=4; $contador+$paraulao\n" | bc)

That should fix it. Also, indent and add comments.

Sign up to request clarification or add additional context in comments.

7 Comments

The -e option is unnecessary (I hope there aren't any escaped characters in what should be numbers).
@chepner the -e is needed for the \n.
Why use the non-standard -e option to suppress the implicit newline only to add it back explicitly?
There is no missing newline; echo produces one. Failing to write a newline to bc is a problem, but it's not one the OP is suffering from.
To avoid potential issues like this and because I think it looks nicer, I tend to just use here strings: bc <<< "scale=4; $contador+$paraula"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.