2

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..

3
  • possible duplicate of Changing the value of a export variable from a bash script Commented Apr 15, 2014 at 21:47
  • @thatotherguy; The above comment doesn't work if the terminal is closed.. I tried that and closed the terminal. I echo $JAVA_HOME and it is blank. Commented Apr 15, 2014 at 21:53
  • Correct. There is no such thing as a global environment variable. You can consider creating a wrapper script for java that invokes the real thing with the parameters you need, read from some config file you can change. Commented Apr 15, 2014 at 22:16

1 Answer 1

3

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

. myscript.sh

Or

source myscript.sh
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.