5

I have a script that basically checks the Java version on the box it's running on and does whatever based on the version number.

My problem is that I want to use the same command on both SunOs and Linux boxes.

The closest I have come to this is...

SunOs:

java -version 2>&1 | nawk -F '"' '/version/ {print $2}'

This gives the expected output of 1.7.0_09

and...

Linux:

java -version 2>&1 | awk -F '"' '/version/ {print $2}'

This gives the expected output of 1.8.0_05

I want to know if I can get the same result, with the same command on the different OS's

  • can you simply alias one of the commands or create a simbolic link, or should this be universal? If so, you could check for the existence of either program and specify the respective command in an if loop. – FelixJN Aug 14 '15 at 08:19
  • just get OS type and use an if, easier than bashing head over something trivial – gwillie Aug 14 '15 at 08:26
  • @gwillie I thought of that, but I wanted a more elegant way. – Pieter van Niekerk Aug 14 '15 at 08:29

1 Answers1

10

Here is a portable way :

java -version 2>&1 | PATH=`getconf PATH` awk -F '"' '/version/ {print $2}'

Unlike the usual suggestions that try to guess the correct location depending on the Unix implementation, it uses the getconf PATH command that returns the path to POSIX compliant commands.

jlliagre
  • 61,204