centos 使用shellscript安装Java

axr492tv  于 2022-11-07  发布在  Shell
关注(0)|答案(1)|浏览(135)

我是新的shell脚本,我正在寻找一种方法来安装java 1.8的centos机器,这样如果java 1.8已经存在,那么脚本将跳过安装,如果任何其他版本的Java存在,那么它将安装1.8,并将只使用1.8。这里的帮助将非常感谢。


# !/bin/sh

JAVA_VER=$(java -version 2>&1 >/dev/null | egrep "\S+\s+version" | awk '{print $3}' | tr -d '"')
        if [ "${JAVA_VER}" -ne 0 ]; then
        echo "java $JAVA_VER already installed. Skipping installation..."
  else
      echo "Installing java..."
      yum install java-1.8.0-openjdk -y
      echo "java installed!"
        java -version
        echo "----------------------------------------------------"
  fi
e5nqia27

e5nqia271#

您可以提取版本major.minor并对其进行检查。


# !/bin/bash

# checking by java -version

JAVA_VERSION=$(java -version);

# or set it manually for test purposes

# JAVA_VERSION=1.7

# extract major.minor

JAVA_VERSION_INSTALLED=$(perl -lne  '/\d+.\d+/ && print $&' <<< $JAVA_VERSION);

case $JAVA_VERSION_INSTALLED in
    1.8 )
        echo "$JAVA_VERSION_INSTALLED already installed";
        echo "nothing do do";
        exit 0;
    ;;
    1.* )
        echo "you Java is old, trying to install new one";
        # you can do the business here with yum, app-get, etc
    ;;
    * )
        echo "unknown version";
        exit 1;

    ;;
esac

备注

  • 我还没有安装java,模式匹配是基于你的评论
  • 相应地更新案例

相关问题