2

I have declared two numeric variables but am unable to compare them

  remote_file_size=$(curl -sI $URL | grep -i content-length | awk '{print $2}')
  local_file_size=$(ls -l $file_location | awk '{print $5}')

  if [ "$local_file_size" -eq "$remote_file_size" ]; then
      echo "Database up to date. Update not required"
  else
      echo "Database needs to be updated! Downloading newer version"
      wget --continue -O $file_location $URL
  fi

I've also tried,

 if [[ "$local_file_size"="$remote_file_size" ]];
 if [[ "$local_file_size"=="$remote_file_size" ]];
 if [[ $local_file_size==$remote_file_size ]];
 if [[ $local_file_size == $remote_file_size ]];
Ayushya
  • 9,599
  • 6
  • 41
  • 57
  • 1
    Btw.: bash can only deal with [integers](https://en.wikipedia.org/wiki/Integer). – Cyrus Jul 21 '17 at 01:46
  • 1
    Btw.: [Why *not* parse `ls`?](https://unix.stackexchange.com/q/128985/74329) – Cyrus Jul 21 '17 at 02:04
  • have you done `echo $local_file_size .... $remote_file_size` to confirm that both values are integers? Good luck. – shellter Jul 21 '17 at 02:11
  • @Cyrus That was very helpful. I found that using `stat -c %s -- $file_location` to obtain file size was much better option than using `ls` – Ayushya Jul 21 '17 at 02:12
  • yes @shellter I have echoed after every step to see if they are getting declared properly and if they are integers. Since I am using file size, can there be problems of number being out of range? – Ayushya Jul 21 '17 at 02:14

2 Answers2

2

curl is notorious for outputting invisible but harmful carriage returns directly from HTTP responses. This is why you're getting this weird, wrapped message:

")syntax error: invalid arithmetic operator (error token is "

You can strip them with tr:

#                                  v-- Here
remote_file_size=$(curl -sI $URL | tr -d '\r' | grep -i content-length | awk '{print $2}')
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • That works! Stripping `curl` of carriage returns now works. Probably curl was giving the problems in the first place. – Ayushya Jul 21 '17 at 02:24
  • 1
    @Ayushya Yay! This is easily diagnosed from the error message, so make sure to include that in future questions – that other guy Jul 21 '17 at 02:29
  • 1
    Funny thing is, there were no errors when I was not using typeset. Since `if` evaluated the condition whatsoever the variables were, whether integers or not. – Ayushya Jul 21 '17 at 02:33
1

You may find typesetting the variables as integers to be helpful:

$ typeset -i a="123"
$ typeset -i b="242"
$ [ $a -lt $b ] && echo 'a < b' || echo 'a >= b'
a < b
$ a=545
$ [ $a -lt $b ] && echo 'a < b' || echo 'a >= b'
a >= b
bishop
  • 37,830
  • 11
  • 104
  • 139
  • typesetting is giving error: tried `typeset -i local_file_size="$(ls -l $file_location | awk '{print $5}')"` and `typeset -i local_file_size=$(ls -l $file_location | awk '{print $5}')`. With and without quotes, both giving error. – Ayushya Jul 21 '17 at 01:52
  • 1
    The error was given by `typeset -i remote_file_size=$(curl -sI $URL | grep -i content-length | awk '{print $2}')`. Other one works fine. Error goes as: `")syntax error: invalid arithmetic operator (error token is "` – Ayushya Jul 21 '17 at 01:57
  • 1
    The integer attribute isn't really that useful. It doesn't affect comparisons, since you need to use the correct set of operators anyway. Aside from its use as documentation, the only effect it has is to treat the value as an arithmetic expression when you *assign* to the variable. – chepner Jul 21 '17 at 02:03
  • 1
    I don't see how `typeset` will solve OP's problem. It's not required (in bash, anyway) in order to use `-eq`, etc. The only thing it buys you is it will spit out an error if you try to do something like `a=2.5`. And it doesn't seem to complain at all if you do `a="foobar"`. Instead it just quietly sets `a` to `0`. In any case, OP's failure to use `-eq` is neither caused by not using `typeset -i`, nor will it be fixed by using it. Except that it *might* point out to him where he's assigning a non-integer value. – Mike Holt Jul 21 '17 at 02:03
  • 1
    Thanks @bishop It did point out that the problem was related to carriage returns in `curl`. – Ayushya Jul 21 '17 at 02:26