我对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
有人可以帮忙吗?
看看我的整个脚本 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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。