0

I am trying to perform a simple integer comparison in bash, which I am new to using, and my methods are causing an error. Any advice would be appreciated.

My basic logic is that I am reading in hurricane track points. There may be multiple entries for the same track_id, with different pressure values. What I want to do is store only one entry per track_id into the array track_id_poly; the case with the lowest pressure. So I am looping through each line, and attempting to compare the current pressure (for $int), with the previous track pressure ($int - 1), and if it is lower, replace the previous array value with the new lower pressure. I hope that makes sense. My code is below.

int=0
    while read track_id ppres_inter 
    do
      printf -v pres_inter "%.0f" "$pres_inter"
      echo pressure $pres_inter
      case $int in
      0)
        Track_id_poly[$int]=$track_id
        Pres_inter_poly[$int]=$pres_inter
        ((int=int+1)) ;;
      *)  
       if [[ $track_id == ${Track_id_poly[$int-1]} ]]
       then
         if (( $pres_inter -lt ${Pres_inter_poly[$int-1]} ))
         then
           Track_id_poly[$int-1]=$track_id
           Pres_inter_poly[$int-1]=$pres_inter
         fi
       else  
         Track_id_poly[$int]=$track_id
         Pres_inter_poly[$int]=$pres_inter
         ((int=int+1))
       fi ;;
      esac
    done <$file_poly
    int_poly=$int
    echo Number of polygon crossings from set $i is $int_poly

The line that is causing me problems is the integer comparison for $pres_inter.

if (( $pres_inter -lt ${Pres_inter_poly[$int-1]} ))

I get the following error:

line 41:  96800 -lt 98759 : syntax error in expression (error token is "98759 ")

Any tips to fix this problem would be appreciated. Probably a simple fix!

Josiah
  • 37
  • 7
kimmyjo221
  • 685
  • 4
  • 10
  • 17
  • Using int as variable name might not be a good idea. Although that's not the cause of your problem. – Strubbl Jan 08 '13 at 16:16

3 Answers3

5

The ((...)) operator does not accept the same syntax as the test, [..], or [[...]] commands. To compare two numbers in ((...)), you would use actual > or < symbols:

$ (( 4 > 2 )) && echo '4 is bigger!'
4 is bigger!

See the ARITHMETIC EVALUATION section of the bash(1) man page for more information (or here).

larsks
  • 277,717
  • 41
  • 399
  • 399
0

My shell scripting is rusty for good reason but you may to review that line and either use "[[ ]]" instead of "(( ))", or use "<" instead of "-lt" . See bash: double or single bracket, parentheses, curly braces

However, the main tip I'd give to you is to stop using bash for things that involve anything beyond simple program invocation and switch to a scripting language (Perl, Python, ...) because it won't only be more robust, it'll be easier to get the job done and it'll also run faster.

Community
  • 1
  • 1
Luis
  • 1,235
  • 1
  • 12
  • 12
-2

In bash "((expression))" is differently evaluated. So you cannot use the operator "-lt", instead you can use the normal operator <.

For further info see the man page of bash:

((expression))

The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".

And the paragraph ARITHMETIC EVALUATION explains further possibilities.

Strubbl
  • 709
  • 4
  • 15
  • 31