[英]How to check Java version using Bash Shell?
I am very new to shell scripting.我对 shell 脚本很陌生。 I want to check the following:
我想检查以下内容:
su
or not.su
。su
then systemctl
command is present or notsu
那么systemctl
命令是否存在 I have done the following:我做了以下工作:
#!/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
While I am executing it, the following error is coming from the line if [[ $("$_java" -version 2>&1) != *"Java(TM)"* ]]; then
当我执行它时,以下错误来自
if [[ $("$_java" -version 2>&1) != *"Java(TM)"* ]]; then
if [[ $("$_java" -version 2>&1) != *"Java(TM)"* ]]; then
installNew.sh: 34: installNew.sh: [[: not found
Can anyone help?任何人都可以帮忙吗?
Take a look at my whole script https://github.com/darshanpatidar1/Environment-Setup/blob/master/version.sh for checking versions of Java,Python and Others查看我的整个脚本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
After you've sorted out the issue of the wrong shell executing your script, as glenn jackman mentioned, take caution with the line在解决了执行脚本的错误 shell 的问题后,正如 glenn jackman 所提到的,请注意这一行
if [ "$version" > "1.8" ]; then
- the ordinary test
command [
doesn't interpret >
as the greater than operator, but rather as an output redirection, so you'll get an empty file "1.8" and, what's worse, the test yields true for any non-empty "$version". - 普通的
test
命令[
不会将>
解释为大于运算符,而是作为输出重定向,因此您将获得一个空文件“1.8”,更糟糕的是,测试对任何非空“都为真” $版本”。 Using [[
should work as expected.使用
[[
应该按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.