1

I am trying to compare two numbers stored in variables in a shell script. The objective is to find if the incoming value is inside the specified ranges: 500-600 or 700-800.

My current code looks like:

inc=0
srange[0]=500 
erange[0]=600
srange[1]=700 
erange[1]=800

for i in "${startrange[@]}"; do
    if [[ " ${startrange[$inc]} " -lt " $actual " ]]; then
        echo " in range "
    fi
    inc = $((inc+1))
done

The code works with integer values such as 530 or 540 but fails with decimal values like 530.2 or 540.3, resulting in a syntax error on this line of the code:

 if (( " ${startrange[$inc]} " -lt " $actual " )); then

How can I fix this to handle the decimal values?

njachowski
  • 927
  • 5
  • 14
vigneshhegde
  • 67
  • 1
  • 8

2 Answers2

1

The shell can only do integer calculations. If you want to compare values that are non-integers, you must use a tool that can understand decimal math. bc is one such option:

if bc <<< "${startrange[inc]} < $actual"; then …
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • Won't accept decimals just like OP's won't – 123 Jan 13 '16 at 14:33
  • 1
    i assumed that was their attempt to stop the syntax error as the if in their actual code would work the same as your answer. – 123 Jan 13 '16 at 14:36
  • Also i can't check atm but I'm pretty sure you can use quotes inside `(())` – 123 Jan 13 '16 at 14:40
  • @kojiro Are you sure i'm almost 100% that you can use quotes however you like in `(())`. Also yes the title is slightly misleading! – 123 Jan 13 '16 at 14:44
  • @123 I'm sure of what I'm saying, but I'm not sure of what meaning you're taking from it. Compare `x=123; echo $(( "$x" ))` and `x=123; echo $(( " $x " ))`. – kojiro Jan 13 '16 at 14:45
  • 1
    That's shell arithmetic, not `let` or `(())`, spaces matter in shell, not in let. – 123 Jan 13 '16 at 14:46
  • 1
    @123 huh, you're right! Good eye. – kojiro Jan 13 '16 at 14:49
  • It's covered in the [Bash Reference Manual](https://www.gnu.org/software/bash/manual/bashref.html#Concept-Index_cp_letter-A). Arithmetic Expansion says "a double quote inside the parentheses is not treated specially." _Shell Arithmetic_ is ever so slightly different. – kojiro Jan 13 '16 at 15:03
  • Arithmetic expansion uses shell arithmetic, and "a double quote inside the parentheses is not treated specially." just means that quotes are treated normally as in they allow spaces to be part of the string, which is incidentally incorrect syntax for shell arithmetic. The `(())` is actually the `let` command and handles spaces( i assume) by just removing/ignoring them after the fact. – 123 Jan 13 '16 at 15:22
1

It seems like you're saying that your code works for integer numbers but not for decimal numbers.

In that case you could simply turn all your numbers into integers before you do the comparison to avoid that error.

This is one way to turn decimals to integers in bash:

DECIMAL=3.14
INTEGER=${DECIMAL%.*}

Note that if you used this code to try to turn an integer into an integer it would return the same number so you can apply this to any number you get regardless of whether or not it is a decimal.

njachowski
  • 927
  • 5
  • 14