bashshbc

Bash - 'unary operator expected'


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?


Solution

  • The two errors are directly linked: