Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternatives --config java bash script

Tags:

linux

bash

ssh

I am writing a script that installs java on a remote machine. After i run the .bin file for the JRE, how can i set the alternatives --config java without the user having to input anything.

For instance, when you type in "alternatives --config java" you are prompted to select which java version you would like. Due to the way i installed java ("/usr/sbin/alternatives --install /usr/bin/java java /location/of/jdk1.6/bin/java 2") the #"2" option should always be the java that i want selected.

So, using an ssh command execution, how can i select the second option for java alternatives without the user having to choose the option. I want it fully automated.

This is in a bash script.

Thanks


Below is the code (working correctly now):

#install the jre
sshRetValue=`ssh -p "22" -i $HOME/sshids/idrsa-1.old ${1} " /home/geiser/jms_adapter/jre-6u25-linux-i586.bin "`;
sshRetValue=`echo $?`;
if [ "$sshRetValue" -eq 0 ];then
        echo "java jre installed successfully";
        #set the alternative and stuff if needed
        ssh -p "22" -i $HOME/sshids/idrsa-1.old ${1} " /usr/sbin/alternatives --install /usr/bin/java java /root/jre1.6.0_25/bin/java 2 ";
        echo 2 | ssh -p "35903" -i $HOME/sshids/idrsa-1.old ${1} " alternatives --config java ";
else
        echo "java jre installation failed";
fi
like image 366
prolink007 Avatar asked May 10 '11 17:05

prolink007


2 Answers

You can run the alternatives command non-interactively too. Instead of --config, use the --set option to specify the path of the alternative directly.

sudo alternatives --set java /location/of/jdk1.6/bin/java
like image 108
Alastair Harrison Avatar answered Oct 28 '22 10:10

Alastair Harrison


This worked for me with Java 8:

 alternatives --install /usr/bin/java java /usr/lib/jvm/jre1.8.0_60/bin/java 3
 alternatives --config java <<< '3'
like image 41
Thomas Bratt Avatar answered Oct 28 '22 09:10

Thomas Bratt