Hi all!
inspired by the great @rtrouton , I adjusted his superb script on installing Java.
I added a check to see if java has been installed or if a newer version is available, a bit like my Flash Update script, for which I borrowed heavily on other guys work.
Hope you guys can use this, and if you have any suggestions, please let me know!
1#!/bin/bash -e23# This script downloads and installs the latest Oracle Java 8 for compatible Macs45# Determine OS version6osvers=$(sw_vers -productVersion | awk -F. '{print $2}')78# Specify the "OracleUpdateXML" variable by adding the "SUFeedURL" value included in the9# /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist file. 10#11# Note: The "OracleUpdateXML" variable is currently specified in the script as that for 12# Java 8 Update 20, but the XML address embedded with Java 8 Update 20 is not special in 13# this regard. I have verified that using the address for Java 8 Update 5 will also work 14# to pull the address of the latest Oracle Java 8 installer disk image. To get the "SUFeedURL"15# value embedded with your currently installed version of Java 8 on Mac OS X, please run16# the following command in Terminal:17#18# defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info" SUFeedURL19#20# As of Java 8 Update 20, that produces the following return:21#22# https://javadl-esd-secure.oracle.com/update/mac/au-1.8.0_20.xml23#2425OracleUpdateXML="https://javadl-esd-secure.oracle.com/update/mac/au-1.8.0_20.xml"2627# check if newer version exists28plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"29if [ -f "$plugin" ]30then31 currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`32 echo " Current version is $currentver"33 currentvermain=${currentver:5:1}34 echo "Installed main version is $currentvermain"35 currentvermin=${currentver:14:2}36 echo "Installed minor version is $currentvermin"37 /usr/bin/curl --retry 3 -Lo /tmp/au-1.8.0_20.xml $OracleUpdateXML38 onlineversion1=`cat /tmp/au-1.8.0_20.xml | awk -F " /enclosure/'{print $(NF-1)}'`39 onlineversion2=${onlineversion1:50:8}40 onlineversionmain=${onlineversion2:2:1}41 echo "Online main: $onlineversionmain"42 onlineversionmin=${onlineversion2:6:2}43 echo "Online minor: $onlineversionmin"44 if [ "$onlineversionmain" -gt "$currentvermain" ]45 then46 echo "Let's install Java! Main online version is higher than installed version."47 installjava=148 fi49 if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" -gt "$currentvermin" ]50 then51 echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."52 installjava=153 fi54 if [ "$onlineversionmain" = "$currentvermain" ] && [ "$onlineversionmin" = "$currentvermin" ]55 then56 echo "Java is up-to-date!"57 fi58else59 echo "No java installed, let's install"60 installjava=161fi626364# Use the XML address defined in the OracleUpdateXML variable to query Oracle via curl 65# for the complete address of the latest Oracle Java 8 installer disk image.666768fileURL=`/usr/bin/curl --silent $OracleUpdateXML | awk -F " /enclosure/'{print $(NF-1)}'`6970# Specify name of downloaded disk image7172java_eight_dmg="/tmp/java_eight.dmg"7374if [[ ${osvers} -lt 7 ]]; then75 echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."76 exit 077fi78if [ "$installjava" = 1 ]79then80 echo "Start installing Java"81 if [[ ${osvers} -ge 7 ]]; then8283 # Download the latest Oracle Java 8 software disk image84 # The curl -L option is needed because there is a redirect 85 # that the requested page has moved to a different location.8687 /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"8889 # Specify a /tmp/java_eight.XXXX mountpoint for the disk image9091 TMPMOUNT=`/usr/bin/mktemp -d /tmp/java_eight.XXXX`9293 # Mount the latest Oracle Java 8 disk image to /tmp/java_eight.XXXX mountpoint9495 hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen9697 # Install Oracle Java 8 from the installer package. This installer may98 # be stored inside an install application on the disk image, or there99 # may be an installer package available at the root of the mounted disk100 # image.101102 if [[ -e "$(/usr/bin/find $TMPMOUNT -maxdepth 1 ( -iname *.app ))/Contents/Resources/*Java*.pkg" ]]; then 103 pkg_path="$(/usr/bin/find $TMPMOUNT -maxdepth 1 ( -iname *.app ))/Contents/Resources/*Java*.pkg"104 elif [[ -e "$(/usr/bin/find $TMPMOUNT -maxdepth 1 ( -iname *Java*.pkg -o -iname *Java*.mpkg ))" ]]; then 105 pkg_path="$(/usr/bin/find $TMPMOUNT -maxdepth 1 ( -iname *Java*.pkg -o -iname *Java*.mpkg ))"106 fi107108 # Before installation, the installer's developer certificate is checked to109 # see if it has been signed by Oracle's developer certificate. Once the 110 # certificate check has been passed, the package is then installed.111112 if [[ "${pkg_path}" != "" ]]; then113 signature_check=`/usr/sbin/pkgutil --check-signature "$pkg_path" | awk /'Developer ID Installer/{ print $5 }'`114 if [[ ${signature_check} = "Oracle" ]]; then115 # Install Oracle Java 8 from the installer package stored inside the disk image116 /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1117 fi118 fi119120 # Clean-up121122 # Unmount the Oracle Java 8 disk image from /tmp/java_eight.XXXX123124 /usr/bin/hdiutil detach -force "$TMPMOUNT"125126 # Remove the /tmp/java_eight.XXXX mountpoint127128 /bin/rm -rf "$TMPMOUNT"129130 # Remove the downloaded disk image131132 /bin/rm -rf "$java_eight_dmg"133134 # Remove xml file135 /bin/rm -rf /tmp/au-1.8.0_20.xml136 fi137fi