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