Last active
December 16, 2015 10:49
-
-
Save bcreasy/5422807 to your computer and use it in GitHub Desktop.
bash script to switch the symlink that PATH and JAVA_HOME use for the JDK currently in use
This file contains hidden or 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
if [[ -d /opt/build/resources/jdk ]]; then | |
servertype="build" | |
else | |
servertype="code" | |
fi | |
jdkpath=/opt/$servertype/resources/jdk/jdk | |
export PATH=$jdkpath/bin:$PATH | |
export JAVA_HOME=$jdkpath |
This file contains hidden or 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 | |
if [[ -d /opt/build/resources/jdk ]]; then | |
servertype="build" | |
else | |
servertype="code" | |
fi | |
jdks=/opt/$servertype/resources/jdk | |
if [[ ! -d /opt/$servertype/resources/jdk ]]; then | |
echo "no JDKs found" | |
exit 1 | |
fi | |
if [[ -L $jdks/jdk ]]; then | |
current=$(file $jdks/jdk | sed "s/\`/\'/" | cut -f2 -d\' | sed 's/jdk//') | |
if [[ "$current" == "latest" ]]; then | |
currentptr=$(file $jdks/jdk | sed "s/\`/\'/" | cut -f2 -d\' | sed 's/jdk//') | |
current=$(file $jdks/latest | sed "s/\`/\'/" | cut -f2 -d\' | sed 's/jdk//') | |
fi | |
fi | |
if [[ -L $jdks/latest ]]; then | |
latest=$(file $jdks/latest | sed "s/\`/\'/" | cut -f2 -d\' | sed 's/jdk//') | |
fi | |
versions=$(find $jdks -type d "jdk[1-9]" | xargs | sed 's/jdk//g') | |
count=0 | |
for version in $versions; do | |
((count++)) | |
promptstring="$count) $version" | |
if [[ $version == $current ]]; then | |
currentnum=$count | |
promptstring="$promptstring (active)" | |
fi | |
if [[ $version == $latest ]]; then | |
latestnum=$count | |
promptstring="$promptstring (latest)" | |
fi | |
echo $promptstring | |
versionsarray[$count]="$version" | |
done | |
until [[ $choice -ge 1 && $choice -le $count ]]; do | |
read -p "Choose a JDK version [1-$count]: " choice | |
done | |
jdk=${versionsarray[$choice]} | |
if [[ "${versionsarray[$latestnum]}" == "$jdk" ]]; then | |
jdkname=latest | |
else | |
jdkname=jdk$jdk | |
fi | |
if [[ $current == $jdk ]]; then | |
echo "JDK already set to $current" | |
exit | |
fi | |
cd $jdks | |
rm jdk && ln -s $jdkname jdk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment