I've written this if, that is obviously not working, and I still can't manage to get over it:#LASTEFFECTIVEHASHif (( $(echo $LASTEFFECTIVEHASHMINVAL < $LASTEFFECTIVEHASH < $LASTEFFECTIVEHASHMAXVAL | $BC -l) )); then                echo $DATESTAMP - LASTEFFECTIVEHASH=$LASTEFFECTIVEHASH is between $LASTEFFECTIVEHASHMINVAL and $LASTEFFECTIVEHASHMAXVAL|tee -a $LOGFILEelse                echo $DATESTAMP - LASTEFFECTIVEHASH=$LASTEFFECTIVEHASH is not between $LASTEFFECTIVEHASHMINVAL and $LASTEFFECTIVEHASHMAXVAL|tee -a $MSGFILE $LOGFILEfiBut, when value are out of ranges, it results in this:20170810003646 - LASTEFFECTIVEHASH=139.2 is between 104.9 and 136.9I'm following the maths syntax: if x > 104.9 and < 136.9, in maths you write it 104.9 < x < 136.9. But I suspect that bash/bc are behaving differently from my math's teacher.It would be great if bc won't fail to count up to 137 ;)
Checking that a decimal number is in a range in bc
shell script;bc;floating point;math
Unlike for example python, bc does not support chained comparisons:a < b < cTo perform both comparisons and require both to be true, use logical-and (requires GNU bc):(a < b) && (b < c)For example:$ a=104.9; b=136; c=136.9; if echo ($a < $b) && ($b < $c) | bc -l | grep -q 1; then echo True; else echo False; fiTrue$ a=104.9; b=137; c=136.9; if echo ($a < $b) && ($b < $c) | bc -l | grep -q 1; then echo True; else echo False; fiFalsePOSIX bcIf you don't have GNU bc, you can replace logical-and with multiplication:$ a=104.9; b=136; c=136.9; if echo ($a < $b)*($b < $c) | bc -l | grep -q 1; then echo True; else echo False; fiTrue$ a=104.9; b=137; c=136.9; if echo ($a < $b)*($b < $c) | bc -l | grep -q 1; then echo True; else echo False; fiFalse