I'm trying to write a Bash script that validate if Java is installed with the -version output (this is mandatory). This is my actual code, I get the else result, but when I try to echo the variable, it contain the "openjdk" for the then to execute. Thanks for the help.
#!/bin/bash
sudo apt install default-jre
java_version=$(java -version)
if [[ $java_version == *"openjdk version"* ]]
then
echo "Java is successfully installed!"
else
echo "Java is not installed, please retry."
fi
Current output:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
default-jre is already the newest version (2:1.11-72build2).
0 upgraded, 0 newly installed, 0 to remove and 38 not upgraded.
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)
Java is not installed, please retry.