AWK POSIX – AWK/NAWK Alternative on SunOS and Linux

awkportabilityposix

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

Best Answer

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.

Related Question