0

I'm getting a "bc: integer expression expected" in this line:

numV=$numVtest

while running the script. How can I solve this error?

firstv=1200         #first velocity value
lastv=5000          #last velocity value
increment=200       #velocity increment
numVtest=100        #use to limit number of velocity panels
                #otherwise, use very large value (100)
#================================================
# Compute number of velocity panels

 numV='bc -l << -END
 ( ( $lastv - $firstv ) / $increment ) + 1
 END'
 if [ $numVtest -lt $numV ] ; then
 numV=$numVtest
 fi

3 Answers 3

2

numV is not a number; it's a string that looks like a call to bc, so it cannot be used as an argument to -lt. I suspect you meant to use backquotes, not single quotes, in its definition.

numV=`bc -l << -END
...
`

However, it is better practice to use $(...) for command substitution instead of backquotes.

numV=$(bc -l << -END
( ( $lastv - $firstv ) / $increment ) + 1
END
)
if [ "$numVtest" -lt "$numV" ] ; then
    numV=$numVtest
fi

The next problem is that -lt only works with integers, though, while the result of bc could be a floating-point value. You need to do the comparison inside bc as well. bc will output a 1 if the comparison is true, and a 0 if it is false.

result=$(bc -l << -END
( ( $lastv - $firstv ) / $increment ) + 1 > $numVtest
END
)
if [ "$result" = 1 ] ; then
    numV=$numVtest
fi
Sign up to request clarification or add additional context in comments.

4 Comments

note that the test command will still fail as $numV is a float, and bash can only handle integers. You'll have to cast $numV as an int using either string manipulation (i.e. ${numV%.*}) or printf
Ugh, you're right. I missed the forest for the trees.
... or just use the bc without -l and you will get integers... ;) e.g. bc -l <<<"10/3" vs bc <<<"10/3"
I'm assuming the OP actually wants floating-point values, or else there is no need for bc. (If integers are fine, I recommend SLePort's answer.)
1

If the result is expected to be an integer, you don't need bc. You can use the $(()) syntax instead:

numV=$(( ($lastv - $firstv) / $increment + 1 ))
if [ "$numVtest" -lt "$numV" ] ; then
    numV=$numVtest
fi

1 Comment

That depends on whether the result of the division is expected to be an integer (which does, however, seem likely, or at least sufficient) or a floating-point value.
1

Your problem is, as pointed out in chepner's answer, that you just assign a string to numV.

You can alternatively use one less operation: if bc evaluates a boolean statement, it prints 0 or 1, and to map that to a Bash conditional, we can wrap the bc command substitution into (( )):

if (( $(bc -l <<< "$numVtest < ($lastv - $firstv) / $increment + 1") )); then
    numV=$numVtest
fi

Comments

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.