0

I am trying to make this if..fi case work for the Java Version from wrapper.conf configuration file./ and from the system, If the output from wrapper.conf is java

I would have two options;
1. wrapper.java.command=/apps/jdk1.8.0_191/bin/java
2. wrapper.java.command=java

If it's long one with Java Version it should just print it.

If it's just java, which means it's using default java, and should check the java -version.

With Below script, I am failing to get it. I am getting below error.

integer expression expected
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

Script Snippet:

for file in $(echo $folder/conf/wrapper.conf)

do

JavaVersion=$(grep "command" $file | awk -F "=" '{print $NF}')

if [ "$JavaVersion" -eq "java" ]; then

java -version 2>&1 >/dev/null | grep 'java version'

else

$JavaVersion

fi
  • 3
    Welcome on U&L! It looks like a final done is missing in your snippet. – fra-san Nov 28 '18 at 16:21
3

Besides the missing done for your loop that fra-san commented on and the -eq integer comparison complaining about the strings you asked it to compare, there's also the error where you called some particular java binary with no parameters, generating the second half of the error you saw.

Since you're likely wanting the java version in either case, simply execute $JavaVersion regardless.

JavaVersion=$(grep wrapper.java.command= "$file" | awk -F "=" '{print $NF}')
"$JavaVersion" -version

Appears I misunderstood the end goal of printing the wrapper.java.command value if it's not exactly java, otherwise executing java -version.

if grep -Fxq wrapper.java.command=java "$file" 
then
  java -version 2>&1 | grep 'java version'
else
  grep ^wrapper.java.command= "$file" | cut -d= -f2-
fi
  • Hey Jeff Schaller, Thank you for the Answer. I have the other "done" at the end of the script. Since it's big script, I just took one snipped from it. Can you please explain what this will do here? "$JavaVersion" -version – Bek Nov 28 '18 at 16:56
  • It'll run whatever you found in the file as the wrapper.java.command and add the -version option.I put quotes around it in case someone decided to package a "/usr/bin/java funny version with spaces" – Jeff Schaller Nov 28 '18 at 16:58
  • I need to run java -version and get the Default Java Version from the system. What would be best way to get it? – Bek Nov 28 '18 at 17:10
  • 1
    run java -version? I'm not sure what "Default Java Version" is in this situation. Perhaps it's worth a separate question? – Jeff Schaller Nov 28 '18 at 17:33
  • Actually this is my Main Concern here :) I need to get java -version from the system, if the output from wrapper.conf is "java" if long text, then just print. – Bek Nov 28 '18 at 19:44
2

The -eq operator is for integer comparison. Since you are trying to compare strings you will need to use =, specifically:

if [ "$JavaVersion" = "java" ]; then

Additionally, the following line seems flawed:

java -version 2>&1 >/dev/null | grep 'java version'

You are redirecting both stdout and stderr to /dev/null (all output) so there will be nothing left to grep.

  • Thank you for your input. I have modified "-eq" to "=" operator. Regarding the Java Command, I can get the output on the command line. What do you suggest me to replace it with? – Bek Nov 28 '18 at 16:50
  • @Bek I suggest removing the redirects. So just: java -version | grep 'java version' – jesse_b Nov 28 '18 at 16:52
  • java -version |grep 'java version' is giving me every single text it has. I can't even filter it with awk. Have you tested it? Not working for me thou. – Bek Nov 28 '18 at 16:58
  • I have tested with - java -version | grep 'java version' - And still giving the same Integer error – Bek Nov 28 '18 at 17:03
  • 2
    java -version outputs the text to stderr – Jeff Schaller Nov 28 '18 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.