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...


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

This gives the expected output of 1.7.0_09



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. – Fiximan Aug 14 '15 at 8:19
  • just get OS type and use an if, easier than bashing head over something trivial – gwillie Aug 14 '15 at 8:26
  • @gwillie I thought of that, but I wanted a more elegant way. – Pieter van Niekerk Aug 14 '15 at 8:29

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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