The correct syntax for comparing floating point numbers in a bash script

I had a problem with the command from this condition in a bash script, since the bash script does not support floating point numbers.

if ([ $first_util -gt 0.10 ] || [ $first_util -le 1.00 ]) && [ $first_sig -ge $first_sig_min ]; then
  #state1   
else
  #state2   
fi 

I want the first and second condition to be processed first using "or", and then with the 3rd condition with "and", but in this case all numbers are float, not integers.

How to solve this problem? thank you

+4
source share
2 answers

You can use awk:

first_util=0.2
first_sig=0.8
first_sig_min=0.9
awk_input="$first_util $first_sig $first_sig_min"
if awk '{ exit !(($1 > 0.1 || $1 <= 1.0) && ($2 >= $3)) }' <<< "$awk_input"; then
      echo "OK"
fi

The above example uses the return value awk.

+4
source

Source: https://habr.com/ru/post/1535462/


All Articles