-
Notifications
You must be signed in to change notification settings - Fork 1
/
sysUpdate.sh
executable file
·430 lines (361 loc) · 20.8 KB
/
sysUpdate.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/bin/bash
#header----------------------------------------------------------------------------------------
# scriptname: sysUpdate
# scriptversion: v2.2.2
# creator: GitHub/R2Turuk2
# create datetime: 2024.03.10 12:00:00
# permissions: chmod +x sysUpdate.sh
# script description: This Bash script is designed to simplify the process of updating
# Linux systems. It supports various distributions, including Ubuntu,
# Debian, Fedora, openSUSE, CentOS, Kali Linux, and more. The script
# automatically checks compatibility with the operating system and
# then performs the necessary update steps.
# features: System Detection: The script automatically recognizes the operating
# system and its version.
# Package Update: It updates the package list and then carries out
# various update steps, including package upgrades,
# system upgrades, and the removal of unnecessary
# dependencies.
# Snap Package Update: The script also updates Snap packages, if
# installed.
# User-Friendly Options: The script provides optional parameters such
# as reboot, shutdown, and clearing the
# terminal after completing updates.
# Support for Ubuntu.. If desired, the script allows
# System Upgrades: upgrading to a new version of Ubuntu.
# usage: Run the script with ./sysUpdate.sh.
# optional parameters: -h or --help for help and available options.
# -r or --reboot for a reboot after completing the updates.
# -s or --shutdown for a shutdown after completing the updates.
# -c or --clear for clearing the terminal after completing the updates.
# --system-upgrade only for Ubuntu system upgrades (e.g. from 20.04 LTS to 22.04 LTS).
# note: This script requires the availability of the 'lsb_release' command for
# operating system detection and may require additional packages depending
# on the distribution.
#script----------------------------------------------------------------------------------------
# Initialize global variables with default values
#-------------------------------------------------------------------------------------------------------------------------------------------
sysUpgrade=false
sysShutdown=false
sysReboot=false
shellClear=false
shellExit=false
# Check if lsb_release is available
#-------------------------------------------------------------------------------------------------------------------------------------------
while ! command -v lsb_release &> /dev/null; do
echo "-> The command 'lsb_release' is not available on this system. Please install the package to proceed."
# Check the distribution and suggest manual installation commands
if command -v apt &> /dev/null; then
echo "-> Try installing 'lsb-release' with the following command:"
read -p " sudo apt install lsb-release - try running the automatic installation and script again? [Y/n] " lsbAutoInstall
if [ "$lsbAutoInstall" == Y ] || [ "$lsbAutoInstall" == y ]; then
sudo apt install lsb-release
fi
elif command -v yum &> /dev/null; then
echo "-> Try installing 'lsb-release' with the following command:"
read -p " sudo yum install redhat-lsb-core - try running the automatic installation and script again? [Y/n] " lsbAutoInstall
if [ "$lsbAutoInstall" == Y ] || [ "$lsbAutoInstall" == y ]; then
sudo yum install redhat-lsb-core
fi
elif command -v dnf &> /dev/null; then
echo "-> Try installing 'lsb-release' with the following command:"
read -p " sudo dnf install redhat-lsb-core - try running the automatic installation and script again? [Y/n] " lsbAutoInstall
if [ "$lsbAutoInstall" == Y ] || [ "$lsbAutoInstall" == y ]; then
sudo dnf install redhat-lsb-core
fi
elif command -v zypper &> /dev/null; then
echo "-> Try installing 'lsb-release' with the following command:"
read -p " sudo zypper install lsb-release - try running the automatic installation and script again? [Y/n] " lsbAutoInstall
if [ "$lsbAutoInstall" == Y ] || [ "$lsbAutoInstall" == y ]; then
sudo zypper install lsb-release
fi
else
echo "-> Unable to determine the package manager for automatic installation."
echo "-> Please manually install 'lsb-release' using the appropriate package manager for your system."
exit 100
fi
if [ "$lsbAutoInstall" != Y ] && [ "$lsbAutoInstall" != Y ]; then
exit 101
fi
done
# Check if the "snap" command is available in the system's PATH
#-------------------------------------------------------------------------------------------------------------------------------------------
if command -v snap &> /dev/null; then
snapInstall=true
else
snapInstall=false
fi
# Identify the operating system
#-------------------------------------------------------------------------------------------------------------------------------------------
distribution=$(lsb_release -si 2>/dev/null)
release=$(lsb_release -sr 2>/dev/null)
# Check compatibility between OS and script
distribution_lowercase=$(echo "$distribution" | tr '[:upper:]' '[:lower:]')
case $distribution_lowercase in
ubuntu|debian|fedora|opensuse|centos|kali)
systemCompatible="compatible"
;;
*)
systemCompatible="incompatible"
exit 102
;;
esac # end of compatibility check between OS and script
# Display the operating system in the terminal or interrupt the script
if [ ! -n "$distribution" ] && [ ! -n "$release" ]; then
echo "-> Your Linux distribution could not be identified."
exit 103
fi
# Help options
#-------------------------------------------------------------------------------------------------------------------------------------------
if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
echo "-----------------------------------------------------------------------------------------------"
echo "-> Your operating system is $distribution $release and therefore $systemCompatible."
echo " This script runs on Ubuntu, Debian, CentOS, Elementary OS,"
echo " Fedora, Kali Linux, Mageia, Mint, openSUSE, RHEL."
echo ""
#echo "---- essential parameters ---------------------------------------------------------------------"
echo "---- optional parameter -----------------------------------------------------------------------"
echo "-> -h | --help for help"
echo "-> -r | --reboot restart after completing the updates"
echo "-> -s | --shutdown shutdown after completing the updates"
echo "-> -c | --clear clean the terminal after completing the update"
# for distribution-specific information
case $distribution_lowercase in
ubuntu)
echo "-> --system-upgrade only for Ubuntu system upgrade"
echo " use only for system upgrade (for example, from 20.04 LTS to 22.04 LTS)"
;;
*)
;;
esac # end of compatibility check between OS and script
exit 104
fi
# setting parameters in the desired order
#-------------------------------------------------------------------------------------------------------------------------------------------
while [[ $# -gt 0 ]]; do
case "$1" in
--system-upgrade)
sysUpgrade=true
;;
-r|--reboot)
sysReboot=true
;;
-s|--shutdown)
sysShutdown=true
;;
-c|--clear)
shellClear=true
;;
*)
;;
esac
shift # switch to the next arguments
done
# Start update script
#-------------------------------------------------------------------------------------------------------------------------------------------
case $distribution_lowercase in
# for Ubuntu, Mint, Elementary OS
#-------------------------------------------------------------------------------------------------------------------------------------------
ubuntu)
if [ ! -f $HOME/.sysUpdate.do-release.beforeRestart ]; then
echo "----------------------------------------------------------------"
echo "-> The package list is now being updated."
echo "----------------------------------------------------------------"
sudo apt-get update # Updates the package list from all defined package sources.
echo "----------------------------------------------------------------"
echo "-> All packages are now being updated"
echo "----------------------------------------------------------------"
sudo apt-get upgrade -y # Upgrades all installed packages to the latest versions.
echo "----------------------------------------------------------------"
echo "-> System packages and dependencies are now being updated"
echo "----------------------------------------------------------------"
sudo apt-get dist-upgrade -y # Upgrades the system, including system packages and dependencies.
echo "----------------------------------------------------------------"
echo "-> Unnecessary dependencies are now being removed"
echo "----------------------------------------------------------------"
sudo apt-get autoremove -y # Removes unnecessary dependencies and no longer needed packages.
if $snapInstall; then
echo "----------------------------------------------------------------"
echo "-> Snap packages are now being updated"
echo "----------------------------------------------------------------"
sudo snap refresh # Updates Snap packages, if installed.
fi
echo "----------------------------------------------------------------"
echo "-> Your system should now be up to date."
echo "----------------------------------------------------------------"
if [ "$sysUpgrade" = true ]; then
read -p "-> For system upgrade: Was a backup made? If yes, continue? [Y/n] " sysUpgradeContinue
if [ "$sysUpgradeContinue" == "Y" ] || [ "$sysUpgradeContinue" == "y" ]; then
while ! command -v sudo do-release-upgrade &> /dev/null; do
sudo apt install update-manager-core
done
sudo do-release-upgrade # Initiates the upgrade process to a new Ubuntu release if available.
read -p "-> Do you need a system reboot? [Y/n] " sysReboot
if [ "$sysReboot" == "Y" ] || [ "$sysReboot" == "y" ]; then
echo "-> Please run this script again after the reboot. The system will reboot in 15 seconds."
echo "-> To cancel the countdown, press \"ctrl+\"c."
sleep 5
for ((i = 10; i >0; i--))
do
echo "$i sec until reboot."
sleep 1
done
touch $HOME/.sysUpdate.do-release.beforeRestart
sudo reboot
else
exit 200
fi
fi
else
exit 201
fi
fi
if [ -f $HOME/.sysUpdate.do-release.beforeRestart ]; then
rm $HOME/.sysUpdate.do-release.beforeRestart
sudo do-release-upgrade -y # Initiates the upgrade process after reboot to a new Ubuntu release.
fi
;; # end
#-------------------------------------------------------------------------------------------------------------------------------------------
# for Debian
#-------------------------------------------------------------------------------------------------------------------------------------------
debian)
echo "----------------------------------------------------------------"
echo "-> The package list is now being updated."
echo "----------------------------------------------------------------"
sudo apt-get update # Updates the package list from all defined package sources.
echo "----------------------------------------------------------------"
echo "-> All packages are now being updated"
echo "----------------------------------------------------------------"
sudo apt-get upgrade -y # Upgrades all installed packages to the latest versions.
echo "----------------------------------------------------------------"
echo "-> System packages and dependencies are now being updated"
echo "----------------------------------------------------------------"
sudo apt dist-upgrade -y # Upgrades the system, including system packages and dependencies.
echo "----------------------------------------------------------------"
echo "-> Unnecessary dependencies are now being removed"
echo "----------------------------------------------------------------"
sudo apt autoremove -y # Removes unnecessary dependencies and no longer needed packages.
if $snapInstall; then
echo "----------------------------------------------------------------"
echo "-> Snap packages are now being updated"
echo "----------------------------------------------------------------"
sudo snap refresh # Updates Snap packages, if installed.
fi
echo "----------------------------------------------------------------"
echo "-> Your system should now be up to date."
echo "----------------------------------------------------------------"
;; # end
#-------------------------------------------------------------------------------------------------------------------------------------------
# for Fedora
#-------------------------------------------------------------------------------------------------------------------------------------------
fedora)
echo "----------------------------------------------------------------"
echo "-> The package list is now being updated."
echo "----------------------------------------------------------------"
sudo sudo dnf check-update # Check for available updates
echo "----------------------------------------------------------------"
echo "-> All packages are now being updated"
echo "----------------------------------------------------------------"
sudo sudo dnf upgrade -y # Upgrade all installed packages
echo "----------------------------------------------------------------"
echo "-> Unnecessary dependencies are now being removed"
echo "----------------------------------------------------------------"
sudo dnf autoremove -y # Removes unnecessary dependencies and no longer needed packages.
if $snapInstall; then
echo "----------------------------------------------------------------"
echo "-> Snap packages are now being updated"
echo "----------------------------------------------------------------"
sudo snap refresh # Updates Snap packages, if installed.
fi
echo "----------------------------------------------------------------"
echo "-> Your system should now be up to date."
echo "----------------------------------------------------------------"
;; # end
#-------------------------------------------------------------------------------------------------------------------------------------------
# for openSUSE
#-------------------------------------------------------------------------------------------------------------------------------------------
opensuse)
echo "----------------------------------------------------------------"
echo "-> The package list is now being updated."
echo "----------------------------------------------------------------"
sudo zypper refresh # Updates the package list from all defined package sources.
echo "----------------------------------------------------------------"
echo "-> All packages are now being updated"
echo "----------------------------------------------------------------"
sudo sudo zypper update -y # Upgrades all installed packages to the latest versions.
if $snapInstall; then
echo "----------------------------------------------------------------"
echo "-> Snap packages are now being updated"
echo "----------------------------------------------------------------"
sudo snap refresh # Updates Snap packages, if installed.
fi
echo "----------------------------------------------------------------"
echo "-> Your system should now be up to date."
echo "----------------------------------------------------------------"
;; # end
#-------------------------------------------------------------------------------------------------------------------------------------------
# for CentOS, Red Hat Enterprise Linux (RHEL) / Mageia
#-------------------------------------------------------------------------------------------------------------------------------------------
centos)
echo "----------------------------------------------------------------"
echo "-> The package list is now being updated."
echo "----------------------------------------------------------------"
sudo yum check-update # Updates the package list from all defined package sources.
echo "----------------------------------------------------------------"
echo "-> All packages are now being updated"
echo "----------------------------------------------------------------"
sudo yum upgrade -y # Upgrades all installed packages to the latest versions.
echo "----------------------------------------------------------------"
echo "-> Unnecessary dependencies are now being removed"
echo "----------------------------------------------------------------"
sudo yum autoremove -y # Removes unnecessary dependencies and no longer needed packages.
if $snapInstall; then
echo "----------------------------------------------------------------"
echo "-> Snap packages are now being updated"
echo "----------------------------------------------------------------"
sudo snap refresh # Updates Snap packages, if installed.
fi
echo "----------------------------------------------------------------"
echo "-> Your system should now be up to date."
echo "----------------------------------------------------------------"
;; # end
#-------------------------------------------------------------------------------------------------------------------------------------------
# for Kali Linux
#-------------------------------------------------------------------------------------------------------------------------------------------
kali)
echo "----------------------------------------------------------------"
echo "-> The package list is now being updated."
echo "----------------------------------------------------------------"
sudo apt update # Updates the package list from all defined package sources.
echo "----------------------------------------------------------------"
echo "-> All packages are now being updated"
echo "----------------------------------------------------------------"
sudo apt full-upgrade -y # Upgrades all installed packages to the latest versions.
if $snapInstall; then
echo "----------------------------------------------------------------"
echo "-> Snap packages are now being updated"
echo "----------------------------------------------------------------"
sudo snap refresh # Updates Snap packages, if installed.
fi
echo "----------------------------------------------------------------"
echo "-> Your system should now be up to date."
echo "----------------------------------------------------------------"
;; # end
#-------------------------------------------------------------------------------------------------------------------------------------------
*)
;;
esac
# Instructions to end the script
#-------------------------------------------------------------------------------------------------------------------------------------------
# Check for terminal clear condition
if $shellClear; then
clear # Clear the terminal if the condition is true
fi
# Check for system reboot condition
if $sysReboot; then
sudo reboot # Reboot the system if the condition is true
fi
# Check for system shutdown condition
if $sysShutdown; then
sudo shutdown -h now # Shutdown the system if the condition is true
fi