compare - comparing two decimal numbers in bash -
I need to compare two numbers in a bash script. I get an integer error, are there any other methods available ?
The format for our construction is YYYY.M or YYYY.MM.
Therefore, I need to compare this 2014.7 (October 2014) is older than 2014.7 (July 2014).
#! / Bin / bash NEWVER = "2014.10"; # 2014.10 Crover = $ (Head-N1 / Release); # 2014.7 If [$ NEWVER & gt; $ CURVER]; Then the echo "This version is new"; The complexity is that July is formatted as 2014.7
Consequently, floating point arithmetic will not work, instead of 2014.07 . A solution is to separate the information of the year and month and compare it separately: NEWVER = "2014.10" crossover = 2014.7 IFS =. Head 1 Small 1 & lt; & Lt; & Lt; "$ NEWVER" IFS = Major 2 Short 2 < & Lt; & Lt; Read "$ CURVER" [[$ major1 -gt $ major2 || ($ Major 1-au $ major 2 and $ 1-1-GT $ small 2)]] & amp; Amp; Alternatively, we can fix the format of the month and then compare the floating point: fix () {new} < H3> Optional Echo "$ 1" | Sed 's / \. \ ([0- 9] \) $ /. 0 \ 1 \ '; } NEWVER = $ (fix "$ NEWVER") CURVER = $ (fix "$ CURVER") (($ (dumb "$ NEWVER" & gt; $ CURVER '| BC-L))) & amp; Amp; New echo
Comments
Post a Comment