我正在编写 RHEL kickstart 脚本,并且在我的 %post 中,我需要安装 JRE。
基本上,当前的设置要求我在首次启动后手动进入并使用命令将新安装的 JRE 设置为默认alternatives --config
。有没有办法让我传递参数,alternatives
这样我就不必手动选择正确的 JRE?
答案1
你的版本有吗--set
?
--set name path
将程序路径设置为 name 的替代路径。这相当于 --config,但它是非交互式的,因此可以编写脚本。
答案2
使用grep
:
update-alternatives --set java $(update-alternatives --list java | grep java-8)
或者
update-alternatives --set java $(update-alternatives --list java | grep java-11)
答案3
您可以使用alternatives --auto <name>
它来自动选择最高优先级的选项。
一个例子:
alternatives --install /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/javac javac /usr/java/latest/bin/javac 10
alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 20
alternatives --auto javac
会选择优先级较高的版本 (20)/usr/java/latest/bin/javac
答案4
您可以使用下面的脚本。
您应该根据您的系统配置java“/bin”目录的路径和每个版本的编号。
#!/bin/bash
# update-java script
# Supported versions from the script. You may add more version numbers according to your needs.
supported_versions=( 8 11 )
# Validate that an argument has been given
if [ -z "$1" ]; then
echo -e "No argument given. You should set as first argument the preferred java version.\nAvailable values: ${supported_versions[@]}"
exit
fi
# Java "/bin" of each version. Be careful, each path should have the same index with the supported_versions array.
version_path=( "/usr/lib/jvm/jdk1.8.0_171/bin" "/usr/lib/jvm/java-11-openjdk-amd64/bin")
# Configure the alternatives
found=0
for i in "${!supported_versions[@]}"; do
if [[ ${supported_versions[$i]} -eq $1 ]]; then
update-alternatives --set java ${version_path[$i]}/java
update-alternatives --set javac ${version_path[$i]}/javac
found=1
fi
done
# If a wrong version number has been given
if [ $found -ne 1 ]; then
echo "You didn't provide a version of: ${supported_versions[@]}"
fi
如果您将脚本保存为名为“update-java”,然后设置可执行权限,您将能够像下面这样运行。
$sudo update-java 12
You didn't provide a version of: 8 11
$sudo update-java
No argument given. You should set as first argument the preferred java version.
Available values: 8 11
$sudo update-java 11