bc error

Whenever I use bc to compare two numbers, I get a "syntax error on line 1, teletype" error. I've tried simpler expressions like
echo '1 > 2' | bc
...but the error still occurs. Is it a problem with versions? If so, how should I fix it?

What is the exact command that you use: These are two different quotes.

I guess I didn't change it in time, sorry. Should be:

echo '1 > 2' | bc

I've tried both " " and ' ', though, and neither work

I get

$ echo '1 > 2' | bc
0

So you get "syntax error on line 1, teletype", when issuing this simple command, is that correct? What is your OS and version?

Sun 5.10

edit:
and yes, I do get the error instead of 0. Same if I try to use it interactively:

bc
1 > 2
syntax error on line 1, teletype

See if

/usr/xpg6/bin/bc 

works

It won't. '1 > 2' is an incorrect statement as far as the bc standard is concerned.

Unlike all other operators, the relational operators
( '<', '>', "<=", ">=", "==", "!=" ) shall be only valid as the
object of an if, while, or inside a for statement.
2 Likes

no :frowning:

/usr/xpg6/bin/bc
1 > 2
syntax error on line 1, teletype

Some more relevant info:
I'm writing my script on an instructional account, so apparently, the version of bc in the system is not GNU bc. However, GNU bc is installed in path ~/xyzRandomPath/bc, and the error is occuring while i'm working in xyzRandomPath (and my home directory). Am I doing it wrong?

I suggest you rewrite you script so that it conforms to the POSIX specification. Then it will work on both your own system and your instructional system.

1 Like

hmm it still doesn't work as expected:

if [ $(echo "1.1 == 1.1" | bc) -eq 0 ]; then echo "great"; else echo "no go"; fi
syntax error on line 1, teletype
-bash: [: -eq: unary operator expected
no go

Here is a way to do it:

if [ $(echo "if 1.1 == 1.1 0 " | bc) -eq 0 ]; then echo "great"; else echo "no go"; fi

@jlliagre, we would need some parentheses, no?

if [ $(echo "if (1.1 == 1.10) 0 " | bc) -eq 0 ]; then echo "great"; else echo "no go"; fi

But still, this would leave us with an error message in test if the results do not match

$ if [ "$(echo "if (1.1 == 1.0) 0 " | bc)" -eq 0 ]; then echo "great"; else echo "no go"; fi
-bash: [: : integer expression expected
no go

But I guess we could redirect stderr:

$ if [ "$(echo "if (1.1 == 1.10) 0 " | bc)" -eq 0 ] 2>/dev/null; then echo "great"; else echo "no go"; fi
no go

----------edit-----------

I guess we could also do something like this:

if [ $(printf "%.8f" 1.1) = $(printf "%.8f" 1.10) ]; then echo "great"; else echo "no go"; fi

or use ksh93, where we can just do:

if [ 1.1 -eq 1.10 ]; then echo "great"; else echo "no go"; fi
2 Likes

@Scrutinizer: I indeed forgot the parenthesis.

Here is a portable solution:

a=1.1
b=1.0

if [ $(echo "if ( $a == $b ) 0; if ( $a != $b ) 1 " | bc) -eq 0 ]; then echo "great"; else echo "no go"; fi

Note that it isn't still completely portable as it might fail in some locales.

bc doesn't looks like the best tool for the job. ksh93 or awk would be more appropriate.

1 Like

awk could be used like this maybe:

a=1.1 b=1.11
if awk "BEGIN{exit! ($a == $b)}" ; then echo "great"; else echo "no go"; fi

if awk "BEGIN{exit! ($a < $b)}" ; then echo "great"; else echo "no go"; fi

I got it to work with Scrutinizer's

$ if [ "$(echo "if (1.1 == 1.10) 0 " | bc)" -eq 0 ] 2>/dev/null; then echo "great"; else echo "no go"; fi
no go

Thank you so much for the help, guys!