开发者社区> 问答> 正文

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

我对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

有人可以帮忙吗?

展开
收起
祖安文状元 2020-01-06 15:23:19 860 0
1 条回答
写回答
取消 提交回答
  • 看看我的整个脚本 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
    
    2020-01-06 15:23:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载