Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2905
  • Last Modified:

Unix Shell Script To Verify Java Version

Hello,

I need to write a Unix Shell Script (preferably shell agnostic, but bash is ok) to verify that the system executing the .sh script has java 1.6 or greater installed and on the system path.  

Thanks for the help...
0
cgray1223
Asked:
cgray1223
3 Solutions
 
micropc1Commented:
This is probably what you want for the version part...

http://notepad2.blogspot.com/2011/05/bash-script-to-check-java-version.html

#!/bin/bash
VER=`java -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \" | awk '{split($0, array, ".")} END{print array[2]}'`
if [[ $VER ge 6 ]]; then
    echo "Java version is greater than 1.6."
else
    echo "Java version is lower than 1.6."
fi
0
 
CEHJCommented:
Or this could make it more future proof (pass the script 16, 17, 18 etc)

#!/bin/bash
min_version=${1}
test $(java -version 2>&1 | grep 'java version' | egrep -o '[1-2]\.[6-9]' | tr -d '.') -ge ${min_version}

Open in new window

0
 
jcgdCommented:
to verify java in the path:

#!/bin/bash
JAVA=$(which java)
if [ ! -x "${JAVA}" ]; then
   echo java not found
else
   echo java in directory  $JAVA
fi

(here add micropc1 or CEHJ comments)
0
 
gudii9Commented:
Here is interesting explanation, link

if type -p java; then
    echo found java executable in PATH
    _java=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    echo found java executable in JAVA_HOME    
    _java="$JAVA_HOME/bin/java"
else
    echo "no java"
fi

if [[ "$_java" ]]; then
    version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    echo version "$version"
    if [[ "$version" > "1.6" ]]; then
        echo version is more than 1.6
    else        
        echo version is less than 1.6
    fi
fi



http://stackoverflow.com/questions/7334754/correct-way-to-check-java-version-from-bash-script
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now