0

Can anyone see what I have done wrong here?

local VERSION=$(java -version 2>&1 | grep "java version")
if [[ ! "$VERSION" =~ *"1.8.0_33"* ]]; then
  ERROR (not code, just place holder)
else
  NO ERROR (not code, just place holder)
fi

Thanks for the quick help!

* UPDATE *

Here is what else I have tried:

local VERSION=$(java -version 2>&1 | grep "java version")
if [[ "$VERSION" != *"1.8.0_33"* ]];  then
  Error blah blah not using 1.8.0_33
else
  Good to go
fi

This is in a function all by itself. No other nested anything.

GNU bash, version 4.2.37(1)-release (arm-unknown-linux-gnueabihf)

12
  • 1
    Just use: [[ "$VERSION" != *"1.8.0_33"* ]] Commented Feb 11, 2015 at 17:10
  • Yes, I tried that as well wth no luck it seems Commented Feb 11, 2015 at 17:12
  • Have you actually looked to see what $VERSION contains? Commented Feb 11, 2015 at 17:13
  • What does echo "[$VERSION]" show to you? Commented Feb 11, 2015 at 17:13
  • 1
    Is java on $PATH when you execute the script? Commented Feb 11, 2015 at 17:21

2 Answers 2

1

The value of VERSION (you shouldn't use ALL_CAPS variables by the way, those are "reserved" for shell/etc. usage) is the entire line that matches 'java version'.

That isn't going to ever match a simple version string.

You need to pull out just the version from the matching line if you want to do something like this.

The RHS of =~ is a regex not a glob. That is you need .* not * to match anything.

Or, as anubhava correctly points out, you can use [[ globbing with != directly.

[[ "$VERSION" != *"\"1.8.0_33\""* ]]

I added escaped quotes to the match since the output from java -version appears to have them and otherwise this would also match 1.8.0_333, etc.

Sign up to request clarification or add additional context in comments.

Comments

0

Use grep:

VERSION=$(java -version 2>&1 | grep "java version")
if echo "$VERSION" | grep "1.8.0_33";  then
  NO ERROR (not code, just place holder)
else
  ERROR (not code, just place holder)
fi

grep will exit with 0 if outputs any lines, so will execute that in the if, otherwise the code under the else will run.

6 Comments

Nope, this seems to always be true.
...? What version of bash / grep are you using?
This should work fine (though you might want to use \. to avoid matching non-periods there).
Everything should be latest
Also technically grep exits with zero if it outputs any lines (not it any matched) this is the same for grep but inverted for grep -v.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.