I have a shell script which has over 200,000 iterations. Here is the piece of code which is giving me problems:
if [ 1 -eq `echo "$sums>$maxs" | bc` ] ;
then
hmax=$h;
kmax=$k;
maxs=$sums;
fi
sums, maxs, h, and k are defined earlier. I am looping through different values of h and k and sums is calculated from them. But, only for specific values of h and k, I get this error:
(standard_in) 1: syntax error
./zhu-kanamori.sh: line 173: [: 1: unary operator expected
I don't understand why. I saw some similar questions, but I couldn't find a satisfactory answer. What might be the solution? I am clear on how to compare two floats. Why do I get this error only in some cases?
The two errors are directly linked:
(standard_in) 1: syntax error
means that bc
didn't understand the string it was passed. This means that $sums
and $maxs
weren't actually the floating-point values you think they were; run with bash -x yourscript
to see the actual values in use (and thereby to be able to amend your question to include actual values that make your error reproducible)../zhu-kanamori.sh: line 173: [: 1: unary operator expected
means that [
expects -eq
to have two operands, but it was only passed one. This happened because you didn't adequately quote the command substitution, so an empty string emitted by bc
when it failed was treated as zero arguments to test
, as opposed to a single empty argument.