Bash - Integer Expression Expected
Answer :
The test
command, also named [
, has separate operators for string comparisons and integer comparisons:
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
vs
STRING1 = STRING2
the strings are equal
and
STRING1 != STRING2
the strings are not equal
Since your data is not strictly an integer, your test needs to use the string comparison operator. The last realization in the comments was that the "-eq" logic did not match the sense of the if/else echo
statements, so the new snippet should be:
... if [ "$x" != "$y" ] then echo There is version $y update else echo Version $x is the latest version fi
BTW, if you have two version strings (e.g. in $x
and $y
) you can use printf
and GNU sort
to find which is newer.
$ x=4.1.1 $ y=4.2.2 $ printf "%s\n" "$x" "$y" | sort -V -r 4.2.2 4.1.1 $ if [ $(printf "%s\n" "$x" "$y" | sort -V -r | head -1) = "$x" ] ; then if [ "$x" = "$y" ] ; then echo "$x is equal to $y" else echo "$x is newer than $y" fi else echo "$x is older than $y" fi 4.1.1 is older than 4.2.2
Comments
Post a Comment