I am writing a script that installs java on a remote machine. After i run the .bin file for the JRE, how can i s开发者_如何学Goet 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
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
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'
Generally, you can feed any program that expects something on the standard input like this:
echo -e "line 1\nline 2\nline 3" | program
I did it using this script:
tmp=`mktemp`
echo 2 > $tmp
alternatives --config java < $tmp
rm -f $tmp
The <
means that the content of the $tmp
file will be passed to the input of the alternatives command.
Edit: You could simply use a single pipe as other suggested:
echo 2 | sudo alternatives --config java
I had couple java versions in my /usr/lib/jvm directory.
My script had to first remove old symlink, create new one and then specify the path of the alternative directly with update-alternatives (i did not manage to make alternatives work without update- prefix) My system - Debian 8.11 My script:
#!/bin/bash
echo "Removing old java 8 symlink.."
sudo unlink /usr/lib/jvm/java
echo "Linking new java.."
sudo ln -s /usr/lib/jvm/new_java /usr/lib/jvm/java
echo "Updating alternatives for java and java compiler"
sudo update-alternatives --set java /usr/lib/jvm/new_java/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/new_java/bin/javac
The documentation says:
If you want to configure non-interactively you can use the --set option instead (see below).
--set name path
Set the program path as alternative for name. This is
equivalent to --config but is non-interactive and thus
scriptable.
So, to set alacritty as the default terminal, use the following command:
sudo update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator $(which alacritty) 50
sudo update-alternatives --set x-terminal-emulator $(which alacritty)
In your case, use the following command:
sudo alternatives --install /usr/bin/java java $(which java) 70
sudo alternatives --set java $(which java)
精彩评论