Question

I'm trying to write a script that switches between the JDK and the JRE and also x86 and x64 architectures.

However, when I do: echo $PATH or echo $JAVA_HOME or echo $JRE_HOME, nothing prints! Path prints but does not have java added to it.

My script is as follows and it exports the paths at the very end.

#!/bin/bash

#sudo apt-get install oracle-java8-installer

x64_JDK_PATH="/opt/java/jdk1.7.0_51"
x64_JRE_PATH="$x64_JDK_PATH/jre"

x86_JDK_PATH="/opt/java/jdk1.7.0_51_x86"
x86_JRE_PATH="$x86_JDK_PATH/jre"
xJAVA_PATH=""

echo
echo -e "What would you like to switch to?"
echo -e "    1. JDK (Java Development Kit)\n"
echo -e "    2. JRE (Java Runtime Environment)\n"
echo -e "Choice: \c"
read ENV_ANSWER
echo

case $ENV_ANSWER in
    1)
        echo -e "Switching to the JDK (Java Development Kit)\n"
    ;;

    2)
        echo -e "Switching to the JRE (Java Runtime Environment)\n"
    ;;
    *)
        echo -e "Invalid option.\n"
        echo -e "Script terminating.\n"
        exit
    ;;
esac

echo -e "What version would you like to switch to? (x32\\\x64): \c"
read ARCH_ANSWER

if [ $ARCH_ANSWER != "x32" ] && [ $ARCH_ANSWER != "x64" ]
then
    echo -e "Invalid version. Versions are 'x32' and 'x64'."
    echo -e "Script terminating.\n"
    exit
fi

echo "Switching to the $ARCH_ANSWER version."
echo

if [ $ARCH_ANSWER = "x32" ] && [ $ENV_ANSWER = 1 ]
then
    xJAVA_PATH=$x86_JDK_PATH
elif [ $ARCH_ANSWER = "x32" ]
then
    xJAVA_PATH=$x86_JRE_PATH
fi

if [ $ARCH_ANSWER = "x64" ] && [ $ENV_ANSWER = 1 ]
then
    xJAVA_PATH=$x64_JDK_PATH
elif [ $ARCH_ANSWER = "x64" ]
then
    xJAVA_PATH=$x64_JRE_PATH
fi

sudo update-alternatives --install "/usr/bin/java" "java" "$xJAVA_PATH/bin/java" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "$xJAVA_PATH/bin/javaws" 1
if [ $ENV_ANSWER = 1 ]
then
    sudo update-alternatives --install "/usr/bin/javac" "javac" "$xJAVA_PATH/bin/javac" 1
    sudo update-alternatives --install "/usr/bin/javadoc" "javadoc" "$xJAVA_PATH/bin/javadoc" 1
fi
sudo update-alternatives --set java "$xJAVA_PATH/bin/java"


echo
echo -e "Would you like to update your Mozilla Plugins as well? (y\\\n): \c"
read PLUGINS_ANSWER

if [ $PLUGINS_ANSWER = "Y" ] || [ $PLUGINS_ANSWER = "y" ]
then
    rm "$HOME/.mozilla/plugins/libnpjp2.so"
    mkdir -p "$HOME/.mozilla/plugins"
    ln -s "$xJAVA_PATH/jre/lib/amd64/libnpjp2.so" "$HOME/.mozilla/plugins/"
    echo -e "Mozilla Plugins updated.\n"
else
    echo -e "Mozilla Plugins left untouched.\n"
fi

JAVA_HOME=$xJAVA_PATH
JRE_HOME=$xJAVA_PATH
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

java -version
echo

Any ideas what is wrong with it? java -version prints fine. Everything works except for the exporting of the paths..

Was it helpful?

Solution

The "exports" are "locals" to the script, try with source command:

. myscript.sh

Or

source myscript.sh
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top