-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJava Plugin Version.sh
executable file
·60 lines (52 loc) · 1.95 KB
/
Java Plugin Version.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# https://gist.github.com/6033293
function die () {
local st=$?
printf '%b\n' "$*" >&2
exit $st
}
function plist_read () {
/usr/bin/defaults read "$1" "$2" 2>/dev/null
}
if [[ -d "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" ]]; then
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin"
elif [[ -d "/Library/Internet Plug-Ins/JavaPluginCocoa.bundle" ]]; then
plugin="/Library/Internet Plug-Ins/JavaPluginCocoa.bundle"
else
die "Not installed"
fi
if [[ -e "$plugin/Contents/Info.plist" ]]; then
bundle_id=$(plist_read "$plugin/Contents/Info" CFBundleIdentifier) || die "Error: Could not read bundle identifier"
else
die "Error: Could not find $plugin/Contents/Info.plist"
fi
case $bundle_id in
com.oracle.* )
version=$(plist_read "$plugin/Contents/Info" CFBundleVersion) || die "Error: Could not read bundle version"
;;
com.apple.* )
# Normalize the Java runtime version so that it looks something like x.y.z.b instead of x.y.0_z-b-foo
output=$(/usr/libexec/java_home -v 1.6 -exec java -version 2>&1 | /usr/bin/awk '/Runtime Environment/ { sub(/.$/, "", $NF); print $NF }') || die "Error: Could not get runtime version"
[[ -n $output ]] || die "Not installed"
# The output looks something like '1.6.0_51-b11-457-11M4509' so we trim off what is useful to identify what isn't
waste=${output#*.*.0_*-b*-}
# From right-to-left, trim off the waste plus an extra '-' character
version=${output%-$waste}
# Substitute an empty string for the '0_'
version=${version/0_/}
# Finally, substitute a '.' for the '-b'
version=${version/-b/.}
;;
* )
die "Unknown bundle identifier\n$bundle_id"
;;
esac
# Regex pattern to match a version string
# Matches any of the following where N is any n-digit number: N, N.N, N.N.N, or N.N.N.N
pattern='^[[:digit:]]+(\.[[:digit:]]+){0,3}$'
if [[ $version =~ $pattern ]]; then
printf '%s\n' "$version"
else
die "Unknown version\n$version"
fi
exit 0