Last active
October 19, 2023 22:36
-
-
Save hanggrian/7bade4563ee7b5a97e96b39de4090daf to your computer and use it in GitHub Desktop.
Check versions and update JDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Compare installed JDK versions with Liberica API, with options to download. | |
readonly END=[0m | |
readonly BOLD=[1m | |
readonly RED=[91m | |
readonly GREEN=[92m | |
readonly YELLOW=[93m | |
warn() { echo "$YELLOW$*$END"; } >&2 | |
die() { echo; echo "$RED$*$END"; echo; exit 1; } >&2 | |
if [[ "$(uname)" != "Darwin" ]]; then die "${RED}Unsupported OS.$END"; fi | |
if ! command -v jq &> /dev/null; then die "${RED}jq is not installed.$END"; fi | |
if [[ "$(uname -m)" == "arm"* ]]; then | |
arch="arm" | |
else | |
arch="x86" | |
fi | |
found="false" | |
for jvm in "/Library/Java/JavaVirtualMachines"/*; do | |
found="true" | |
echo | |
filename="$(basename -- "$jvm")" | |
echo "$BOLD$filename$END" | |
# get current version | |
feature_version="${filename#*liberica-jdk-}" # remove prefix | |
feature_version="${feature_version%-*}" # remove suffix dash | |
feature_version="${feature_version%.*}" # remove suffix dot | |
version="$("$jvm/Contents/Home/bin/java" -version 2>&1)" | |
version="$(echo "$version" | head -n 2)" # take 2nd line | |
version="${version#*(}" # remove prefix | |
version="${version#*build }" | |
version="${version%)*}" # remove suffix | |
version="${version%-LTS*}" | |
echo "Version: $version" | |
# check bundle type | |
if [[ "$filename" == *"full"* ]]; then | |
bundle_type="jdk-full" | |
echo "Type: Full" | |
elif [[ "$filename" == *"lite"* ]]; then | |
bundle_type="jdk-lite" | |
echo "Type: Lite" | |
else | |
bundle_type="jdk" | |
echo "Type: Standard" | |
fi | |
# pull API | |
api="$(curl "https://api.bell-sw.com/v1/liberica/releases?version-feature=$feature_version&version-modifier=latest&bitness=64&os=macos&arch=$arch&package-type=dmg&bundle-type=$bundle_type" -s)" | |
api_version="$(echo "$api" | jq ".[0].version" | tr -d '"')" | |
api_download_url="$(echo "$api" | jq ".[0].downloadUrl" | tr -d '"')" | |
api_filename="$(echo "$api" | jq ".[0].filename" | tr -d '"')" | |
if [[ "$version" == "$api_version" ]]; then | |
echo "${GREEN}No update available$END" | |
else | |
echo "Latest version $YELLOW$api_version$END is available, download? [Y/N]" | |
read input | |
# ask to download | |
case "$input" in | |
Y | y) | |
echo -n "Downloading..." | |
curl -s -L "$api_download_url" -o "$api_filename" | |
echo " Done." | |
;; | |
N | n) ;; | |
*) die "Unable to recognize input." ;; | |
esac | |
fi | |
done | |
if [[ "$found" = false ]]; then | |
echo "${RED}Not found.$END" | |
fi | |
echo | |
echo "Goodbye!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment