Skip to content

Instantly share code, notes, and snippets.

@akhilputhiry
Last active December 7, 2017 18:40
Show Gist options
  • Save akhilputhiry/ea08bc9798613444b7dd06bcce9993d1 to your computer and use it in GitHub Desktop.
Save akhilputhiry/ea08bc9798613444b7dd06bcce9993d1 to your computer and use it in GitHub Desktop.
Check installed java version and upgrade if new release is available
#! /bin/bash
# function to install latest java
function download() {
echo "Downloading java $1"
# here you have to write curl or wget command to download java
# version to be downloaded will be available in $1
}
# get version of JAVA installed
INSTALLED_JAVA_VERSION=`java -version 2>&1 | awk 'NR==1{ gsub(/"/,""); print $3 }'`
echo "Installed version $INSTALLED_JAVA_VERSION"
# check the latest version of JAVA available
LATEST_JAVA_VERSION=`curl -s http://javadl-esd-secure.oracle.com/update/baseline.version | head -n 1`
echo "Latest version $LATEST_JAVA_VERSION"
if [ "$(printf "$LATEST_JAVA_VERSION\n$INSTALLED_JAVA_VERSION" | sort -V | head -n1)" == "$INSTALLED_JAVA_VERSION" ] && [ "$LATEST_JAVA_VERSION" != "$INSTALLED_JAVA_VERSION" ]; then
echo "Upgrading to $LATEST_JAVA_VERSION"
download $LATEST_JAVA_VERSION
else
echo "Everything up-todate"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment