문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top