如何使用Bash Shell检查Java版本?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

如何使用Bash Shell检查Java版本?

祖安文状元 2020-01-06 15:23:19 101

我对Shell脚本非常陌生。我要检查以下内容:

无论用户是su不是。 如果su那么systemctl命令存在或不存在 如果已安装,则是否安装了Java 如果已安装,则版本是否为1.8或更高 如果更多,那就不是Oracle Java 如果使用Oracle Java,则运行jar 我已经完成以下工作:

#!/bin/bash
if ! [ $(id -u) = 0 ]; then
   echo "User not logged in as Root!"
   exit 1
fi

if command -v systemctl; then
    echo Found systemctl
else
    sudo apt-get install systemd 
fi   

if type java | grep -q 'java' ; then
    echo Found java executable in PATH
    _java=java


elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    echo Found java executable in JAVA_HOME     
    _java="$JAVA_HOME/bin/java"

else
    echo "Java is not installed. Please install JAVA 1.8 or upper version"
    exit 1
fi

if [ ! -z "$_java" ]; then

    version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    echo version "$version"
    if [ "$version" > "1.8" ]; then
        echo Java version is more than 1.8

        if [[ $("$_java" -version 2>&1) != *"Java(TM)"* ]]; then  #Problem is here
      #  if [ $("$_java" -version 2>&1) | grep -q 'Java(TM)' ]; then 
           echo "Oracle JRE is installed" 
           java -jar myJar-0.0.1-SNAPSHOT.jar
        else
           echo "Oracle JRE is not installed" ;           
        fi


    else         
        echo Java version is less than 1.8        
        echo "Please install JAVA 1.8 or upper version"
        exit 1
    fi
else
    exit 1

fi 当我执行它时,以下错误来自该行 if [[ $("$_java" -version 2>&1) != "Java(TM)" ]]; then

installNew.sh: 34: installNew.sh: [[: not found

有人可以帮忙吗?

Oracle Java 关系型数据库 Shell Perl
分享到
取消 提交回答
全部回答(1)
  • 祖安文状元
    2020-01-06 15:23:38

    看看我的整个脚本 https://github.com/darshanpatidar1/Environment-Setup/blob/master/version.sh, 用于检查Java,Python和其他版本

    #!/bin/bash
    #echo "Checking java JDK version"
    #command -v javac >/dev/null 2>&1 || { echo >&2 "Java JDK is not installed"; }
    #variables
            purge_='sudo apt-get purge '
            update_='sudo apt update '
            install_='sudo apt install ' 
    
    #checking for java files and exe
    echo Java JRE -
    if type -p java; then
        echo Found Java executable in PATH
        _java=java  
    elif [[ -n $JAVA_HOME ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
        echo Found Java executable in JAVA_HOME
        _java="$JAVA_HOME/bin/java"
    else
        echo "no JRE found"
    fi
    
    if [[ "$_java" ]]; then
        version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
        echo Version "$version"
    fi
    
    echo
    echo Java JDK -
    if type -p javac; then
        echo Found Javac executable in PATH
        _javac=javac  
    elif [[ -n $JAVA_HOME ]] && [[ -x "$JAVA_HOME/bin/javac" ]];  then
        echo Found Javac executable in JAVA_HOME
        _javac="$JAVA_HOME/bin/javac"
    else
        echo "no JDK found"
        echo "Previous Java JDK files will also be removed in installation."
        read -r -p "Do you want to install it now? [y/N] " response
        if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
        then
                echo "Installing Java JDK "
            $purge_ 'java openjdk-*'
            $update_
            $install_ java -y
        else
               echo "Installation aborted !"
               exit
        fi
    fi
    
    if [[ "$_javac" ]]; then
        version=$("$_javac" -version 2>&1   | awk '{print $2}')
        echo Version "$version"
    fi
    #javac -version 2>&1 | head -n 1
    #end of java
    
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程