gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:26 25 4
gpt4 key购买 nike

我是 shell 脚本的新手。我想检查以下内容:

  1. 用户是否是su
  2. 如果 susystemctl 命令是否存在
  3. 如果已安装,则是否安装了 java
  4. 如果已安装则版本为 1.8 或更高版本
  5. 如果大于则判断是不是Oracle Java
  6. 如果是 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)"* ]];然后

installNew.sh: 34: installNew.sh: [[: 未找到

有人能帮忙吗?

最佳答案

看看我的整个脚本 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

关于linux - 如何使用 Bash Shell 检查 Java 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com