1

I have to pipe integers comparisons (and more complex arithmetic and boolean operations) to bc, and I have to keep track of syntax errors when some data is missing.

It seems I found some inconsistent behaviour with bc.

Of course :

echo "1==7" | bc gives boolean 0

echo "==7" | bc gives (standard_in) 1: syntax error

But :

echo -e "==1\n==7" | bc
(standard_in) 1: syntax error`
7

instead of 7, one would expect (standard_in) 2: syntax error on the second line.

For the sake of these explanations, I simplified the input of bc but I have to resolve this strange behaviour with much more complex arithmetic and boolean operations.

WORKAROUND As @KamilCok noticed below, a workaround consists in inserting newlines

echo -e "==1\n==7" | awk 'ORS="\n\n"' | bc

Mgrd
  • 13
  • 3

2 Answers2

1

No this is absolutely consistent with keyboard input.

I suspect that bc has a strange parsing error behavior that's all.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
1

I believe this is a bug in yacc parser within GNU bc. I see that inserting a valid statement between comparisons seems to reset the state.

echo -e "==1\n1\n==7" | bc
(standard_in) 1: syntax error
1
(standard_in) 3: syntax error

So a workaround would be to input an empty valid statement (like just 1) between comparisons. A real solution would be to write a patch to GNU bc and notify the developers.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanx. `echo -e "==1\n==7" | awk 'ORS="\n\n"' | bc` is a workaround. Don't know how to write a patch, but I notify the developpers. – Mgrd Nov 15 '20 at 14:39