Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zachbrowne/d5020f5400e48daf80306059209700c8 to your computer and use it in GitHub Desktop.
Save zachbrowne/d5020f5400e48daf80306059209700c8 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo 'Checking for a new Java Version...'
kern_arch=$(uname -r | sed 's/.*-\(.*\)/\1/')
if [ "$kern_arch" = amd64 ]; then
java_ver=' x64'
fi
current_version=$(java -version 2>&1 >/dev/null | sed -n '1s/.*"\([^"]*\)"/\1/p')
latest_version_url=$(curl https://www.java.com/en/download/manual.jsp 2>/dev/null | grep -Po -m 1 "href=\"\K[^\"]*(?=.*Linux${java_ver} en JRE)"
)
latest_version=$(curl $latest_version_url 2>/dev/null | sed -n 's/.*File=jre-\([0-9]\)u\([0-9]\{2\}\).*/1.\1.0_\2/p')
if [ "$current_version" = "$latest_version" ]; then
echo 'No new Java version available. Aborting.'
exit 1
fi
read -n 1 -p "A new Java version is available (${latest_version})! Would you like to download it (y)? " download
echo
if [ "$download" = y ]; then
filename="$(curl "$latest_version_url" 2> /dev/null | sed -n 's/.*File=\([^&]*\).*/\1/p')"
wget -q -O "$filename" --show-progress "$latest_version_url"
echo 'Download completed.'
exit 0
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment