-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdkenv
More file actions
101 lines (89 loc) · 2.86 KB
/
jdkenv
File metadata and controls
101 lines (89 loc) · 2.86 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh
# Maintained at: git@github.com:dareni/shellscripts.git
# For system installed jdks on debian use: update-java-alternatives.
outputDoc() {
DOC="\
Configure /usr/local/etc/jdktab containing java homes eg:
7:/opt/dev/jdk1.7.0_45
6:/opt/dev/jdk1.6.0_45
Then configure the env eg
JDK_NO=6
$ . jdkenv
or
. jdkenv
to get the java home selection prompt.
"
echo "$DOC" > /dev/stderr
}
JDKTAB=""
if [ -e /etc/jdktab ]; then
JDKTAB=/etc/jdktab
elif [ -e /usr/local/etc/jdktab ]; then
JDKTAB=/usr/local/etc/jdktab
else
echo "No /usr/local/etc/jdktab configured." > /dev/stderr
outputDoc
fi
if [ -n "${JDKTAB}" ]; then
#Prompt for the java home if noone selected.
if [ -z "$JDK_NO" ]; then
OPTIONS=""
for line in `cat $JDKTAB |grep -v ^#`
do
echo $line
OPTIONS=$OPTIONS${line%:*},
done;
OPTIONS=${OPTIONS%,*}
read -p "Select the jdk for the env (eg $OPTIONS blank none): " JDK_NO
fi
#Get the path to the new home from the config file.
NEWPATH=$PATH
NEW_JDK_HOME=""
if [ "${JDK_NO:=-}" != "-" ]; then
for line in `cat $JDKTAB |grep -v ^#`
do
if [ "${JDK_NO}" = "${line%:*}" ]; then
NEW_JDK_HOME="${line#*:}"
fi
done;
else
#remove jdk home/bin from path.
NEW_JDK_HOME=""
fi
REPLACED=""
#Replace a jdk home already in the path with the new selection.
#Remove all other jdk entries from the path.
for line in `cat $JDKTAB |grep -v ^#`
do
if [ "$JDK_NO" != "${line%:*}" ]; then
#Replace an old jdk with the new jdk exactly one
#time and remove all other occurances.
OLD_JDK_HOME=${line#*:}
INPATH=`echo $PATH |grep $OLD_JDK_HOME`
if ( [ -z "$REPLACED" ] && [ -n "$INPATH" ] ); then
NEWPATH=`echo $NEWPATH |sed s^${OLD_JDK_HOME}^${NEW_JDK_HOME}^`
REPLACED=y
fi
NEWPATH=`echo $NEWPATH |sed s^:*${OLD_JDK_HOME}[^:]*^^g |sed s^:$^^g`
else
#Be sure we only have one instance of the jdk we want in the PATH.
OLD_JDK_HOME=${line#*:}
NEWPATH=`echo $NEWPATH |sed s^${OLD_JDK_HOME}^XXXMARKERXX^`
NEWPATH=`echo $NEWPATH |sed s^:*${OLD_JDK_HOME}[^:]*^^g |sed s^:$^^g`
NEWPATH=`echo $NEWPATH |sed s^XXXMARKERXX^${NEW_JDK_HOME}^`
fi
done;
if [ -n "${NEW_JDK_HOME}" ]; then
#If the selected jdk is not in the path then add it.
INPATH=`echo $NEWPATH |grep ${NEW_JDK_HOME}`
#Add it to the path start to override invocation of system installation at /usr/bin
if [ -z "${INPATH}" ]; then NEWPATH=${NEW_JDK_HOME}/bin:${NEWPATH}
fi
fi
unset JDK_NO
export JDK_NO
PATH=$NEWPATH
export PATH
JAVA_HOME=$NEW_JDK_HOME
export JAVA_HOME
fi