0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Archon
  • 59
  • 11
  • Please reduce your question to something more concise, it will help us help you. – Fred Apr 06 '17 at 19:57
  • Okay, but I just added the whole code for context. The actual question is just in the beginning. I am gonna change it now. – Archon Apr 06 '17 at 19:59
  • 3
    `(standard_in) 1: syntax error` is an error emitted by `bc`. Clearly, `$sums` and `$maxs` are not what you think they are. (ie, they are not simple numbers) – William Pursell Apr 06 '17 at 20:01
  • They are actually floats. Should I add some more of the code here? Because I don't understand that if there is a syntax error, why is there not an error for most of the iterations? – Archon Apr 06 '17 at 20:02
  • Well, awk is an option, but can't bc be used at all? Just asking. – Archon Apr 06 '17 at 20:05
  • 1
    Running `bc` over 2,000,000 times just to compare two floats is grossly inefficient. Consider writing your script in a language that can handle floating-point arithmetic natively. – chepner Apr 06 '17 at 20:06
  • I actually am doing this in matlab. But this was kind of an experiment – Archon Apr 06 '17 at 20:06
  • Run the script with `bash -x yourscript`, and reproduce the bug in that mode. You'll see the **actual** echo, with values substituted. I'd be very surprised if that didn't make the bug obvious (or, at least, the behavior wherein `bc` doesn't emit any output reproducible). – Charles Duffy Apr 06 '17 at 20:22
  • The canonical may be *["unary operator expected" error in Bash if condition](https://stackoverflow.com/questions/13617843/)* – Peter Mortensen Oct 05 '21 at 17:29

1 Answers1

1

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.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441