Solved

Unix Shell Script To Verify Java Version

Posted on 2012-03-12
4
2,291 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now