Solved

Unix Shell Script To Verify Java Version

Posted on 2012-03-12
4
2,512 Views
Last Modified: 2012-06-27
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
Comment
Question by:cgray1223
4 Comments
 
LVL 7

Expert Comment

by:micropc1
ID: 37713093
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 37713973
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
 
LVL 3

Assisted Solution

by:jcgd
jcgd earned 100 total points
ID: 37715207
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
 
LVL 7

Accepted Solution

by:
gudii9 earned 300 total points
ID: 37715272
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which non-HTML GUI front end to use with Java? 3 51
Glassfish admin console not working 1 55
AWK How do I reorder the columns in a csv 7 58
Shell script issue 4 53
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question