25
25
# $ sudo ./installOracleJDK.sh; echo "exit code: $?";
26
26
27
27
28
+ # Print error ${2} and exit with ${1} as an exit code.
29
+ perror_exit () {
30
+ echo -e " \n***ERROR***"
31
+ echo -e " ${2} "
32
+ echo -e " Script will now exit.\n"
33
+ exit ${1}
34
+ }
35
+
36
+ install_set_alternative () {
37
+ sudo update-alternatives --install " /usr/bin/${1} " " ${1} " " /usr/local/java/${JDK_DIR} /bin/${1} " 1
38
+ EC=$?
39
+ if [ ${EC} -ne 0 ]
40
+ then
41
+ echo -e " \n***WARNING***"
42
+ echo -e " Could not install \" ${1} \" alternative. Exit code of 'update-alternatives': ${EC} .\n"
43
+ EFLAG=1
44
+ fi
45
+
46
+ sudo update-alternatives --set " ${1} " " /usr/local/java/${JDK_DIR} /bin/${1} "
47
+ EC=$?
48
+ if [ ${EC} -ne 0 ]
49
+ then
50
+ echo -e " \n***WARNING***"
51
+ echo -e " Could not set \" ${1} \" alternative. Exit code of 'update-alternatives': ${EC} ."
52
+ echo -e " Use \" update-alternatives --config ${1} \" to manually configure alternatives.\n"
53
+ EFLAG=1
54
+ fi
55
+ }
56
+
28
57
# An initial message is printed to console.
29
58
echo " ##################################################################################"
30
59
echo " # *** You are about to install Oracle JDK *** #"
@@ -50,14 +79,13 @@ if [ -z "${1}" ]
50
79
then
51
80
# Prompt user to provide a username.
52
81
# The script is executed as root, so 'whoami' may be invalid.
53
- echo -en " Enter your username(<username>@<host> ) and press [ENTER]:\n > "
82
+ echo -en " Enter your username(<username>@` hostname ` ) and press [ENTER]:\n > "
54
83
read USERNAME
55
84
56
85
# Check if $USERNAME is empty.
57
86
if [ -z " ${USERNAME} " ]
58
87
then
59
- echo -e " \n***ERROR***\nUsername is empty.\nScript will now exit.\n"
60
- exit 1
88
+ perror_exit 1 " Username is empty."
61
89
fi
62
90
63
91
# $DIRPATH gets the absolute path of the user's 'Downloads' directory assigned.
68
96
# Check if $DIRPATH ends with a forward slash.
69
97
if [ " ${DIRPATH: (-1)} " != " /" ]
70
98
then
71
- echo -e " \n***ERROR***\n${DIRPATH} : Path should end with a '/'.\nScript will now exit.\n"
72
- exit 2
99
+ perror_exit 2 " ${DIRPATH} : Path should end with a '/'."
73
100
fi
74
101
fi
75
102
76
103
# Check if $DIRPATH is a valid directory.
77
104
if [ ! -d " ${DIRPATH} " ]
78
105
then
79
- echo -e " \n***ERROR***\n${DIRPATH} : Not a valid directory.\nScript will now exit.\n"
80
- exit 3
106
+ perror_exit 3 " ${DIRPATH} : Not a valid directory."
81
107
fi
82
108
83
109
# $FILES holds all the filenames inside $DIRPATH directory that begin with 'jdk-' and end with '.tar.gz'.
@@ -86,8 +112,7 @@ FILES=$(sudo ls -1 "${DIRPATH}" | grep ^jdk- | grep .tar.gz$ | tr "\n" "\n")
86
112
# Check if there are any filenames complying with the previous checks.
87
113
if [ -z " ${FILES} " ]
88
114
then
89
- echo -e " \n***ERROR***\nThere is no '.tar.gz' file associated with Oracle JDK inside ${DIRPATH} directory.\nScript will now exit.\n"
90
- exit 4
115
+ perror_exit 4 " There is no '.tar.gz' file associated with Oracle JDK inside ${DIRPATH} directory."
91
116
fi
92
117
93
118
# $FILENUM holds the number of files held in $FILES
112
137
# if $CHOICE holds a valid number/index, the related filename is assigned to $FILE.
113
138
if [ ${CHOICE} -lt 0 ] || [ ${CHOICE} -ge ${INDEX} ]
114
139
then
115
- echo -e " \n***ERROR***\nInvalid choice!\nScript will now exit.\n"
116
- exit 5
140
+ perror_exit 5 " Invalid choice!"
117
141
fi
118
142
119
143
INDEX=0
126
150
fi
127
151
INDEX=$(( INDEX+ 1 ))
128
152
done
129
- echo -e " \nChosen file: ${file } \n"
130
- sleep 3
153
+ echo -e " \nChosen file: ${FILE } \n"
154
+ sleep 5
131
155
else
132
156
# If $FILES holds only one filename, it's value is assigned to $FILE.
133
157
FILE=${FILES}
@@ -139,24 +163,26 @@ TYPE="$(file -b ${DIRPATH}${FILE})"
139
163
# Check if the type of $FILE matches "gzip".
140
164
if [ " ${TYPE: 0: 4} " != " gzip" ]
141
165
then
142
- echo -e " \n***ERROR***\nThere is no '.tar.gz.' file associated with Oracle JDK inside ${DIRPATH} directory.\nScript will now exit.\n"
143
- exit 6
166
+ perror_exit 6 " There is no '.tar.gz.' file associated with Oracle JDK inside ${DIRPATH} directory."
144
167
fi
145
168
146
169
# If execution reaches this point of the script, it means that there is a valid JDK '.tar.gz'
147
170
# file inside $DIRPATH. The following part of the script is the one that conducts the installation.
148
171
149
172
# The 'java' directory is created inside /usr/local/ directory
150
- X1=0
151
173
if [ ! -d " /usr/local/java" ]
152
174
then
153
- sudo mkdir /usr/local/java
154
- X1=" $? "
175
+ if ! sudo mkdir /usr/local/java
176
+ then
177
+ perror_exit 7 " There was a problem creating \" /usr/local/java/\" ."
178
+ fi
155
179
fi
156
180
157
181
# Extract the 'tar.gz' file in the current directory.
158
- sudo tar -zxvf ${DIRPATH}${FILE} -C /usr/local/java
159
- X2=" $? "
182
+ if ! sudo tar -zxvf ${DIRPATH}${FILE} -C /usr/local/java
183
+ then
184
+ perror_exit 8 " There was a problem exporting \" ${DIRPATH}${FILE} \" in \" /usr/local/java/\" ."
185
+ fi
160
186
161
187
JDK_VERSION=$( echo ${FILE} | sed ' s/jdk-\(.*\)[-_]linux.*/\1/g' )
162
188
JDK_DIR=" jdk-${JDK_VERSION} "
@@ -178,37 +204,33 @@ PATH=\$PATH:\$JAVA_HOME/bin
178
204
${JRE_PROFILE_LINES}
179
205
export JAVA_HOME
180
206
export PATH" >> /etc/profile
181
- X3=" $? "
207
+
208
+ if [ $? -ne 0 ]
209
+ then
210
+ perror_exit 9 " Could not update \" /etc/profile\" ."
211
+ fi
182
212
183
213
# Updating alternatives
184
- sudo update-alternatives --install " /usr/bin/java" " java" " /usr/local/java/${JDK_DIR} /bin/java" 1
185
- X4=" $? "
186
- sudo update-alternatives --install " /usr/bin/javac" " javac" " /usr/local/java/${JDK_DIR} /bin/javac" 1
187
- X5=" $? "
188
- sudo update-alternatives --install " /usr/bin/javaws" " javaws" " /usr/local/java/${JDK_DIR} /bin/javaws" 1
189
- X6=" $? "
190
- sudo update-alternatives --set java " /usr/local/java/${JDK_DIR} /bin/java"
191
- X7=" $? "
192
- sudo update-alternatives --set javac " /usr/local/java/${JDK_DIR} /bin/javac"
193
- X8=" $? "
194
- sudo update-alternatives --set javaws " /usr/local/java/${JDK_DIR} /bin/javaws"
195
- X9=" $? "
214
+ EFLAG=0
215
+ install_set_alternative " java"
216
+ install_set_alternative " javac"
217
+ install_set_alternative " javaws"
196
218
197
219
# Changing permissions of the /etc/profile
198
- sudo chmod 744 /etc/profile
199
- X10=" $? "
220
+ if ! sudo chmod 744 /etc/profile
221
+ then
222
+ perror_exit 10 " Problem changing mode of \" /etc/profile\" ."
223
+ fi
200
224
201
225
# Executing /etc/profile
202
- sudo /etc/profile
203
- X11=" $? "
204
-
205
- # The exit code of each substantial command is held at the variables from $X1 to $X11.
206
- # If there are no errors, each exit code equals to "0". The sum of all exit codes is held on $SUM.
207
- SUM=$(( X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 ))
226
+ if ! sudo /etc/profile
227
+ then
228
+ perror_exit 11 " Problem executing \" /etc/profile\" ."
229
+ fi
208
230
209
- # Finally, feedback about the installation status is given to the user according to the value of $SUM .
231
+ # Finally, feedback about the installation status is given to the user according to the value of $EFLAG .
210
232
# Note that in UNIX-like systems, the exit code is represented as an 8-bit unsigned(!) char [1-255].
211
- if [ " ${SUM} " -eq " 0 " ]
233
+ if [ ${EFLAG} -eq 0 ]
212
234
then
213
235
echo -e " \n##################################################################################"
214
236
echo " # The installation was successful! #"
217
239
else
218
240
echo -e " \n##################################################################################"
219
241
echo " # The installation was NOT successful! #"
242
+ echo " # One or more alternatives could not be installed and/or set! #"
220
243
echo -e " ##################################################################################\n"
221
- exit 7
244
+ exit 12
222
245
fi
0 commit comments