谁能看到我在这里做错了什么?
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
感谢您的快速帮助!
*更新*
以下是我尝试的其他内容:
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
这本身就是一个功能。没有其他嵌套的东西。
GNU bash,版本4.2.37(1)-release(arm-unknown-linux-gnueabihf)
答案 0 :(得分:1)
VERSION
的值(顺便说一下,你不应该使用ALL_CAPS变量,对于shell / etc。用法是“保留”)是匹配'java的整行版'。
这不会匹配简单的版本字符串。
如果你想做这样的事情,你需要从匹配行中提取出版本。
=~
的RHS是正则表达式而不是全局。那就是你需要.*
而不是*
来匹配任何东西。
或者,正如anubhava正确指出的那样,您可以直接使用[[
globbing与!=
。
[[ "$VERSION" != *"\"1.8.0_33\""* ]]
我在竞赛中添加了转义引号,因为java -version
的输出似乎包含它们,否则它也会匹配1.8.0_333
等。
答案 1 :(得分:0)
使用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
将退出0
,因此会在if
中执行该行,否则else
下的代码将会运行。