Domanda

I have a problem to make command from this condition in bash script since bash script doesn't support float number.

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

I want 1st and 2nd condition will be processed first using 'or' then with 3rd condition with 'and' but in this case, all numbers are float, not integer.

How to solve this problem? Thank you

È stato utile?

Soluzione

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 uses the return value of awk in the if test.

Altri suggerimenti

You can create a couple of functions to grant bc the capabilities to work with floating numbers, as described in this article by Mitch Frazier in Linux Journal:

I believe you can use that as the base to create your own functions to do floating point numbers comparisons. I hope this helps!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top