Reputation: 33
So I have a variable that I want to compare with another number in an if statement.
b=8.25
if [ $(echo "$b < 10" | bc) -ne 0 ]; then
echo "hey"
fi
I get the following error
(standard_in) 1: syntax error
I know the issue is having the b variable inside, how can I make it so that I can maintain it in there?
Please help
Upvotes: 1
Views: 542
Reputation: 246837
Your script file probably has DOS-style CRLF line endings:
$ b=8.25
$ if [ $(echo "$b < 10" | bc) -ne 0 ]; then
> echo "hey"
> fi
hey
$ b=$'8.25\r'
$ if [ $(echo "$b < 10" | bc) -ne 0 ]; then
> echo "hey"
> fi
(standard_in) 1: illegal character: ^M
bash: [: -ne: unary operator expected
Run dos2unix
on your script file.
Upvotes: 2
Reputation:
Store the comparison in a variable separateley
b=8.25
# Capture output outside the if
comparison=$(echo "$b < 10" | bc)
# Use the output in the if
if [ $comparison -ne 0 ]; then
echo "hey"
fi
Upvotes: 0