1

This question already has an answer here:

I'm trying to write my own bash script that detects if Debian's release version is less than 9.0

code:

VERSION=$(cat /etc/debian_version)
echo $VERSION

if [ "$VERSION" -lt "9.0" ]; then
    echo "Debian version is less than 9"
else
    echo "Debian version is greater than 9"
fi

but the error i'm receiving is:

./test.sh: line 4: [: 8.2: integer expression expected

basically, $VERSION is a string type, but it's 8.2. I'm trying to make it become 8.2, but as a float. so I can use <= on it.

marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • By the way, Bash doesn't support float numbers, it only works with integers. – Giovanni Nunes Dec 24 '18 at 16:03
  • ty @GiovanniNunes is it possible that bash is going to add support for float in the future? – girng rodriguez Dec 24 '18 at 20:25
  • why you remove my question? that's not very nice. the people here have been extremely helpful. – girng rodriguez Jan 6 at 20:19
2

Try this, I use to compare the float with bc.

VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "`echo "${VERSION} < 9.0" | bc`" -eq 1 ]; then
    echo "Debian version is less than 9"
else
    echo "Debian version is greater than 9"
fi

one-liner using awk

awk  '{if ($1 < 9.0) {print "Debian version is less than 9";} else if ($1 > 9.0){ print "Debian version is greater than 9";}}' /etc/debian_version
  • hi msp9011, i'm getting ./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected – girng rodriguez Dec 24 '18 at 7:14
  • 1
    oh. i just did apt-get install bc. i think it's working now. thank you so much. – girng rodriguez Dec 24 '18 at 7:16
1

Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort utility, that knows how to handle versions numbers:

check_versions(){
    # check_version <reference> <version to check>
    if [[ $1 == $2 ]]; then
        echo 'same'
    else
        low=$(echo -e "$1\n$2" | sort --version-sort | head --lines=1)
        if [[ $low == $1 ]]; then
            echo 'greater'
        else
            echo 'lower'
        fi
    fi
}

ref='8.5'      # reference version for check

check_versions $ref '8.2' # current version is lower!
check_versions $ref '8.5' # current version is equal!
check_versions $ref '8.12' # current version is greater!

I think that it should a better solution.

Not the answer you're looking for? Browse other questions tagged or ask your own question.