0

In the script I want to check if the java is installed and if is it newer than 1.8.

java -version 2>$1 >/dev/null | egrep "\S+\s+version" | awk '{print substr($3,0,5)}' | tr -d '"'

I guess the results can be like this:

1.8.0_225
11.0.2
1.6.1_223

so to make it comparable it will be good to map this values to \d+\.\d+:

1.80225
11.02
1.61223

It can be done by replacing everything which is not a digit to nothing except first dot.

wBacz
  • 99
  • 9
  • Are you sure you want to do that instead of using GNU sort's version comparison algorithm? – Charles Duffy Apr 23 '20 at 14:17
  • That is to say, does the existing answer to [How two compare two strings in dot-separated version format in bash](https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash) not work for the use case? – Charles Duffy Apr 23 '20 at 14:17
  • (BTW, you want `2>&1`, not `2>$1`, and `\S` and `\s` aren't guaranteed to work in `egrep`; you only get code that's guaranteed to be compatible with all POSIX-compliant `grep -E`s/`egrep`s if you use `[![:space:]]` for `\S`, and `[[:space:]]` for `\s`). – Charles Duffy Apr 23 '20 at 14:19
  • Anyhow, part of the problem here is that the results of the proposed algorithm are just not reliable. Version `1.0.9.2` is much older than `1.0.10.3`, but get rid of your dots after the first you get `1.092` being a smaller number than `1.0103` when you try to compare them as floating-point values. Use a sort that compares each number in the list as a distinct value and you don't have this issue. – Charles Duffy Apr 23 '20 at 14:23

1 Answers1

0

You can use awk like this below :

java -version 2>&1 | awk  -F[\"._] '/version/{print $2"."$3$4$5$6$7}'

Using sed:

java -version 2>&1 | sed -Ee 's/.*"(.*)"$/\1/g;s/^([0-9]+\.[0-9]+\.)([0-9]+)[^0-9]/\1\2/g;q'