java -version In Bash Script. Giving integer error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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









share|improve this question



















  • 3




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














up vote
0
down vote

favorite












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









share|improve this question



















  • 3




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












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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









share|improve this question















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






linux scripting java test






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 at 21:28

























asked Nov 28 at 16:00









Bek

63




63







  • 3




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












  • 3




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







3




3




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




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










2 Answers
2






active

oldest

votes

















up vote
3
down vote













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





share|improve this answer






















  • 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 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 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 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 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 at 19:44

















up vote
2
down vote













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.






share|improve this answer






















  • 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 at 16:50











  • @Bek I suggest removing the redirects. So just: java -version | grep 'java version'
    – Jesse_b
    Nov 28 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 at 16:58










  • I have tested with - java -version | grep 'java version' - And still giving the same Integer error
    – Bek
    Nov 28 at 17:03






  • 2




    java -version outputs the text to stderr
    – Jeff Schaller
    Nov 28 at 17:33










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484702%2fjava-version-in-bash-script-giving-integer-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













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





share|improve this answer






















  • 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 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 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 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 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 at 19:44














up vote
3
down vote













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





share|improve this answer






















  • 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 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 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 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 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 at 19:44












up vote
3
down vote










up vote
3
down vote









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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 at 22:44

























answered Nov 28 at 16:26









Jeff Schaller

37.2k1052121




37.2k1052121











  • 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 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 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 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 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 at 19:44
















  • 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 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 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 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 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 at 19:44















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 at 16:56




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 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 at 16:58




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 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 at 17:10




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 at 17:10




1




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 at 17:33




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 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 at 19:44




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 at 19:44












up vote
2
down vote













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.






share|improve this answer






















  • 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 at 16:50











  • @Bek I suggest removing the redirects. So just: java -version | grep 'java version'
    – Jesse_b
    Nov 28 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 at 16:58










  • I have tested with - java -version | grep 'java version' - And still giving the same Integer error
    – Bek
    Nov 28 at 17:03






  • 2




    java -version outputs the text to stderr
    – Jeff Schaller
    Nov 28 at 17:33














up vote
2
down vote













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.






share|improve this answer






















  • 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 at 16:50











  • @Bek I suggest removing the redirects. So just: java -version | grep 'java version'
    – Jesse_b
    Nov 28 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 at 16:58










  • I have tested with - java -version | grep 'java version' - And still giving the same Integer error
    – Bek
    Nov 28 at 17:03






  • 2




    java -version outputs the text to stderr
    – Jeff Schaller
    Nov 28 at 17:33












up vote
2
down vote










up vote
2
down vote









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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 at 16:28

























answered Nov 28 at 16:20









Jesse_b

11.5k23063




11.5k23063











  • 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 at 16:50











  • @Bek I suggest removing the redirects. So just: java -version | grep 'java version'
    – Jesse_b
    Nov 28 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 at 16:58










  • I have tested with - java -version | grep 'java version' - And still giving the same Integer error
    – Bek
    Nov 28 at 17:03






  • 2




    java -version outputs the text to stderr
    – Jeff Schaller
    Nov 28 at 17:33
















  • 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 at 16:50











  • @Bek I suggest removing the redirects. So just: java -version | grep 'java version'
    – Jesse_b
    Nov 28 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 at 16:58










  • I have tested with - java -version | grep 'java version' - And still giving the same Integer error
    – Bek
    Nov 28 at 17:03






  • 2




    java -version outputs the text to stderr
    – Jeff Schaller
    Nov 28 at 17:33















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 at 16:50





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 at 16:50













@Bek I suggest removing the redirects. So just: java -version | grep 'java version'
– Jesse_b
Nov 28 at 16:52





@Bek I suggest removing the redirects. So just: java -version | grep 'java version'
– Jesse_b
Nov 28 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 at 16:58




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 at 16:58












I have tested with - java -version | grep 'java version' - And still giving the same Integer error
– Bek
Nov 28 at 17:03




I have tested with - java -version | grep 'java version' - And still giving the same Integer error
– Bek
Nov 28 at 17:03




2




2




java -version outputs the text to stderr
– Jeff Schaller
Nov 28 at 17:33




java -version outputs the text to stderr
– Jeff Schaller
Nov 28 at 17:33

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484702%2fjava-version-in-bash-script-giving-integer-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?