i try to validate if a number with decimals is between in a specify range, i mean like the following example :
rangeA=58.5
rangeB=61.5
number=62.7
if [[ ( "$number" > "$rangeA" | bc ) || ( "$number" = "$rangeA" | bc ) ]] && [[ ( "$number" < "$rangeB" | bc ) || ( "number" = "rangeB" | bc ) ]]; then
but i'm stuck in this operation, I would appreciate your help thanks
Bash's <
and >
compare strings, -lt
and -gt
compare integers, the only way to compare floating point numbers is to shell out to bc(1)
(which you do, but you do it wrong):
rangeA=58.5
rangeB=61.5
number=62.7
if (( $(bc <<<"$number >= $rangeA && $number <= $rangeB") )); then
echo yes
else
echo no
fi
bc
prints 1 or 0 to the standard output, and bash's arithmetics context (((expression))
) tests it for zero and sets the status code accordingly.
You can use awk:
rangeA=58.5
rangeB=61.5
number=62.7
if awk -v number=$number -v rangeA=$rangeA -v rangeB=$rangeB '
BEGIN{exit !(number >= rangeA && number <= rangeB)}'
then
echo "condition matched"
else
echo "condition didn't match"
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With