0

I have a script, and I am getting an error as syntax error in expression on line 4. I am guessing it is because of trailing spaces after 0" and befoe ]. If I remove that I am getting different error. How else I can modify this line to make it work?

Surprisingly I do not see any error on line1 . This is snippet of bigger script, so log is a variable defined already.

 1  if [ "$Total" -ge 1024 ]
 2  then
 3      echo "Good to go" >> "$log"
 4      if [ "$((a-(b + c))) -ge 0" ] ; then echo "YES"  | tee -a "$log"; else echo "NO"  | tee -a "$log"; fi
 5  else
 6      echo "can't proceed" >> "$log"
 7      echo "NO"  | tee -a "$log"
 8      exit 0
 9  fi
  • What are the exact values of `a`, `b`, and `c`? You can check them with `declare -p a b c`. Also, what's the *full* error message (it'll usually include more info than that). – Gordon Davisson Mar 23 '21 at 19:30
  • I can't reproduce the error. – Barmar Mar 23 '21 at 19:50
  • Spaces are definitely required before `]`. See https://stackoverflow.com/questions/9581064/why-should-there-be-a-space-after-and-before-in-bash – Barmar Mar 23 '21 at 19:51
  • 1
    You will want to read this shellcheck wili page: [Argument to implicit -n is always true due to literal strings](https://github.com/koalaman/shellcheck/wiki/SC2157) – glenn jackman Mar 23 '21 at 20:25

1 Answers1

1

I can't actually reproduce the error, but there's an obvious mistake in the way you've written the if statement.

-ge needs to be its own argument, you can't put the whole comparison in a single string.

if [ "$((a-(b + c)))" -ge 0 ]
Barmar
  • 669,327
  • 51
  • 454
  • 560
  • This is correct, but doesn't explain the error message. Having double-quotes around the whole expression would make the test something like: "is '17 -ge 0' a non-null string?" And since it's always non-null, it'd just execute the `then` branch and not give any error. – Gordon Davisson Mar 23 '21 at 19:29
  • lemme try this mentioned recommended by @Barmar and confirm. – Erroruploading Mar 23 '21 at 20:16