Linux 替代方案 --config java bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5954075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:01:14  来源:igfitidea点击:

alternatives --config java bash script

linuxbashssh

提问by prolink007

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.

我正在编写一个在远程机器上安装 java 的脚本。在我为 JRE 运行 .bin 文件后,如何设置替代 --config java 而无需用户输入任何内容。

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.

例如,当您输入“alternatives --config java”时,系统会提示您选择所需的 Java 版本。由于我安装 java ("/usr/sbin/alternatives --install /usr/bin/java java /location/of/jdk1.6/bin/java 2") 的方式,#"2" 选项应该始终是我想要选择的java。

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.

因此,使用 ssh 命令执行,我如何选择 java 替代方案的第二个选项,而无需用户选择该选项。我希望它完全自动化。

This is in a bash script.

这是在 bash 脚本中。

Thanks

谢谢



Below is the code (working correctly now):

下面是代码(现在工作正常):

#install the jre
sshRetValue=`ssh -p "22" -i $HOME/sshids/idrsa-1.old  " /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  " /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  " alternatives --config java ";
else
        echo "java jre installation failed";
fi

采纳答案by Blagovest Buyukliev

Generally, you can feed any program that expects something on the standard input like this:

通常,您可以提供任何需要标准输入的程序,如下所示:

echo -e "line 1\nline 2\nline 3" | program

回答by Lynch

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 $tmpfile will be passed to the input of the alternatives command.

<意味着$tmp文件的内容将被传递到替代命令的输入。

Edit:You could simply use a single pipe as other suggested:

编辑:您可以简单地按照其他建议使用单个管道:

echo 2 | sudo alternatives --config java

echo 2 | sudo alternatives --config java

回答by Thomas Bratt

This worked for me with Java 8:

这对我来说适用于 Java 8:

 alternatives --install /usr/bin/java java /usr/lib/jvm/jre1.8.0_60/bin/java 3
 alternatives --config java <<< '3'

回答by Alastair Harrison

You can run the alternativescommand non-interactively too. Instead of --config, use the --setoption to specify the path of the alternative directly.

您也可以以alternatives非交互方式运行该命令。而不是--config,使用--set选项直接指定替代的路径。

sudo alternatives --set java /location/of/jdk1.6/bin/java

回答by vevon

I had couple java versions in my /usr/lib/jvm directory.

我的 /usr/lib/jvm 目录中有几个 java 版本。

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:

我的脚本必须首先删除旧的符号链接,创建新的符号链接,然后直接使用 update-alternatives 指定替代的路径(我没有设法在没有更新前缀的情况下使替代工作)我的系统 - Debian 8.11 我的脚本:

#!/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