Skip to content

Commit 07f4c2a

Browse files
committed
Major script refactoring related to error checking
1 parent 9858e5c commit 07f4c2a

File tree

1 file changed

+67
-44
lines changed

1 file changed

+67
-44
lines changed

installOracleJDK.sh

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@
2525
# $ sudo ./installOracleJDK.sh; echo "exit code: $?";
2626

2727

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+
2857
# An initial message is printed to console.
2958
echo "##################################################################################"
3059
echo "# *** You are about to install Oracle JDK *** #"
@@ -50,14 +79,13 @@ if [ -z "${1}" ]
5079
then
5180
# Prompt user to provide a username.
5281
# 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 > "
5483
read USERNAME
5584

5685
# Check if $USERNAME is empty.
5786
if [ -z "${USERNAME}" ]
5887
then
59-
echo -e "\n***ERROR***\nUsername is empty.\nScript will now exit.\n"
60-
exit 1
88+
perror_exit 1 "Username is empty."
6189
fi
6290

6391
# $DIRPATH gets the absolute path of the user's 'Downloads' directory assigned.
@@ -68,16 +96,14 @@ else
6896
# Check if $DIRPATH ends with a forward slash.
6997
if [ "${DIRPATH:(-1)}" != "/" ]
7098
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 '/'."
73100
fi
74101
fi
75102

76103
# Check if $DIRPATH is a valid directory.
77104
if [ ! -d "${DIRPATH}" ]
78105
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."
81107
fi
82108

83109
# $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")
86112
# Check if there are any filenames complying with the previous checks.
87113
if [ -z "${FILES}" ]
88114
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."
91116
fi
92117

93118
# $FILENUM holds the number of files held in $FILES
@@ -112,8 +137,7 @@ then
112137
# if $CHOICE holds a valid number/index, the related filename is assigned to $FILE.
113138
if [ ${CHOICE} -lt 0 ] || [ ${CHOICE} -ge ${INDEX} ]
114139
then
115-
echo -e "\n***ERROR***\nInvalid choice!\nScript will now exit.\n"
116-
exit 5
140+
perror_exit 5 "Invalid choice!"
117141
fi
118142

119143
INDEX=0
@@ -126,8 +150,8 @@ then
126150
fi
127151
INDEX=$((INDEX+1))
128152
done
129-
echo -e "\nChosen file: ${file}\n"
130-
sleep 3
153+
echo -e "\nChosen file: ${FILE}\n"
154+
sleep 5
131155
else
132156
# If $FILES holds only one filename, it's value is assigned to $FILE.
133157
FILE=${FILES}
@@ -139,24 +163,26 @@ TYPE="$(file -b ${DIRPATH}${FILE})"
139163
# Check if the type of $FILE matches "gzip".
140164
if [ "${TYPE:0:4}" != "gzip" ]
141165
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."
144167
fi
145168

146169
# If execution reaches this point of the script, it means that there is a valid JDK '.tar.gz'
147170
# file inside $DIRPATH. The following part of the script is the one that conducts the installation.
148171

149172
# The 'java' directory is created inside /usr/local/ directory
150-
X1=0
151173
if [ ! -d "/usr/local/java" ]
152174
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
155179
fi
156180

157181
# 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
160186

161187
JDK_VERSION=$(echo ${FILE} | sed 's/jdk-\(.*\)[-_]linux.*/\1/g')
162188
JDK_DIR="jdk-${JDK_VERSION}"
@@ -178,37 +204,33 @@ PATH=\$PATH:\$JAVA_HOME/bin
178204
${JRE_PROFILE_LINES}
179205
export JAVA_HOME
180206
export PATH" >> /etc/profile
181-
X3="$?"
207+
208+
if [ $? -ne 0 ]
209+
then
210+
perror_exit 9 "Could not update \"/etc/profile\"."
211+
fi
182212

183213
# 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"
196218

197219
# 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
200224

201225
# 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
208230

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.
210232
# 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 ]
212234
then
213235
echo -e "\n##################################################################################"
214236
echo "# The installation was successful! #"
@@ -217,6 +239,7 @@ then
217239
else
218240
echo -e "\n##################################################################################"
219241
echo "# The installation was NOT successful! #"
242+
echo "# One or more alternatives could not be installed and/or set! #"
220243
echo -e "##################################################################################\n"
221-
exit 7
244+
exit 12
222245
fi

0 commit comments

Comments
 (0)