Last active
January 6, 2021 14:08
-
-
Save buckett/5832311 to your computer and use it in GitHub Desktop.
set-java(1) for OS X
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 | |
# Set the current version of Java being used. | |
# Doesn't at the moment set the path as the mac just pulls them from JAVA_HOME | |
# Get the current | |
current=$JAVA_HOME | |
# Look for the default | |
default=$(/usr/libexec/java_home) | |
# Get a list of all the options | |
# Output is path:name:version | |
options=$(/usr/libexec/java_home -V 2>&1 | grep '^ ' | while read line; do path=$(echo ${line##* }); name=$(echo ${line% *}); echo -e "${name}:${path}"; done | sort -n) | |
echo "$options" | while read line | |
do | |
name=$(echo $line| cut -d: -f1) | |
path=$(echo $line| cut -d: -f2| tr -d '"') | |
if [ "$path" == "$current" ]; then | |
name="${name} (*)" | |
fi | |
echo -e "$name\t( $path )" | |
done | cat -n 1>&2 | |
echo -n "Select your JDK: " 1>&2 | |
read line | |
if [ -n "$line" ]; then | |
# Return the selected path on stdout | |
echo "$options" | cut -d: -f2| sed -n "${line}p" | |
else | |
echo "$current" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this with bash set the alias: