-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_all.sh
205 lines (173 loc) · 6.09 KB
/
update_all.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
#!/bin/sh
# Text Color Variables
# Check if the 'tput' command is available
# - '/dev/null 2>&1' redirects standard output (stdout) and standard error (stderr) to /dev/null, suppressing output.
# - If 'tput' is found, it likely indicates that color support is available.
if command -v tput >/dev/null 2>&1; then
RED=$(tput setaf 1) # Set text color to red
GREEN=$(tput setaf 2) # Set text color to green
CLEAR=$(tput sgr0) # Reset text formatting
# If 'tput' is not available, check if color support is available by other means:
# - If file descriptor 1 (stdout) is associated with a terminal, then color support is assumed.
# - This check is performed using the '-t' test, which verifies if the file descriptor is connected to a terminal.
elif [ -t 1 ]; then
RED=$(printf '\033[31m') # Set text color to red using ANSI escape code
GREEN=$(printf '\033[32m') # Set text color to green using ANSI escape code
CLEAR=$(printf '\033[0m') # Reset text formatting using ANSI escape code
else
# If neither 'tput' nor the '-t' test is available, default to empty strings for color variables.
RED=''
GREEN=''
CLEAR=''
fi
# OS Update Functions
# Function to update Debian based
update_debian() {
printf "\n%sUpdating Debian based...%s\n" "${GREEN}" "${CLEAR}"
if ! sudo apt-get update -y && sudo apt-get upgrade -y && sudo apt-get autoremove -y; then
printf "\n%sUpdate failed.%s\n" "${RED}" "${CLEAR}"
fi
}
# Function to update RPM based
update_rpm() {
printf "\n%sUpdating RPM based...%s\n" "${GREEN}" "${CLEAR}"
if command -v dnf >/dev/null 2>&1; then
if ! sudo dnf update -y && sudo dnf upgrade -y && sudo dnf autoremove -y; then
printf "\n%sUpdate failed.%s\n" "${RED}" "${CLEAR}"
fi
elif command -v yum >/dev/null 2>&1; then
if ! sudo yum update -y && sudo yum upgrade -y && sudo yum autoremove -y; then
printf "\n%sUpdate failed.%s\n" "${RED}" "${CLEAR}"
fi
else
printf "\n%sUpdate failed unknown package manager%s\n" "${RED}" "${CLEAR}"
fi
}
# Function to update Pacman based
update_pacman() {
printf "\n%sUpdating Pacman based...%s\n" "${GREEN}" "${CLEAR}"
if command -v pacman >/dev/null 2>&1; then
if ! sudo pacman -Syu --noconfirm; then
printf "\n%sUpdate failed.%s\n" "${RED}" "${CLEAR}"
fi
fi
}
# Function to update APK based
update_apk() {
printf "\n%sUpdating APK based...%s\n" "${GREEN}" "${CLEAR}"
if command -v apk >/dev/null 2>&1; then
if ! sudo apk update && sudo apk upgrade; then
printf "\n%sUpdate failed.%s\n" "${RED}" "${CLEAR}"
fi
fi
}
# Function to update os based on their type
update_os() {
printf "\n%sUpdate OS Packages%s\n" "${GREEN}" "${CLEAR}"
# Check if the OS is Debian
if [ -f /etc/lsb-release ] || [ -f /etc/debian_version ]; then
update_debian
# Check if the OS is RPM based
elif [ -f /etc/redhat-release ] || [ -f /etc/centos-release ]; then
update_rpm
# Check if the OS is Pacman based
elif [ -f /etc/arch-release ]; then
update_pacman
# Check if the OS is Alpine based
elif [ -f /etc/alpine-release ]; then
update_apk
else
printf "\n%sUnsupported Linux distribution.%s\n" "${RED}" "${CLEAR}"
fi
}
# Function to update brew
update_brew() {
printf "\n%sUpdate Brew Formula's%s\n" "${GREEN}" "${CLEAR}"
if ! command -v brew >/dev/null 2>&1; then
printf "\n%brew is not installed.%s\n" "${RED}" "${CLEAR}"
return
fi
brew update && brew upgrade && brew cleanup -s
printf "\n%sUpdate Brew Casks%s\n" "${GREEN}" "${CLEAR}"
brew outdated --cask && brew upgrade --cask && brew cleanup -s
printf "\n%sBrew Diagnostics%s\n" "${GREEN}" "${CLEAR}"
brew doctor && brew missing
}
# Function to update vscode extensions
update_vscode_ext() {
printf "\n%sUpdating VSCode Extensions%s\n" "${GREEN}" "${CLEAR}"
if ! command -v code >/dev/null 2>&1; then
printf "\n%sVSCode is not installed.%s\n" "${RED}" "${CLEAR}"
return
fi
code --update-extensions
}
# Function to update gem packages
update_gem() {
printf "\n%sUpdating Gems%s\n" "${GREEN}" "${CLEAR}"
if ! command -v gem >/dev/null 2>&1; then
printf "\n%sGem is not installed.%s\n" "${RED}" "${CLEAR}"
return
fi
gem update --user-install && gem cleanup --user-install
}
# Function to update npm packages
update_npm() {
printf "\n%sUpdating Npm Packages%s\n" "${GREEN}" "${CLEAR}"
if ! command -v npm >/dev/null 2>&1; then
printf "\n%sNpm is not installed.%s\n" "${RED}" "${CLEAR}"
return
fi
npm update -g
}
# Function to update yarn packages
update_yarn() {
printf "\n%sUpdating Yarn Packages%s\n" "${GREEN}" "${CLEAR}"
if ! command -v yarn >/dev/null 2>&1; then
printf "\n%sYarn is not installed.%s\n" "${RED}" "${CLEAR}"
return
fi
yarn upgrade --latest
}
# Function to update pip3 packages
update_pip3() {
printf "\n%sUpdating Python 3.x pips%s\n" "${GREEN}" "${CLEAR}"
if ! command -v python3 >/dev/null 2>&1 || ! command -v pip3 >/dev/null 2>&1; then
printf "\n%sPython 3 or pip3 is not installed.%s\n" "${RED}" "${CLEAR}"
return
fi
# Running with a non-root user
python3 -m pip list --outdated --format=columns | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 python3 -m pip install -U
}
# Function to update all in one shot
update_all() {
update_os
update_brew
update_vscode_ext
update_gem
update_npm
update_yarn
update_pip3
}
# Function to check if ping is supported for non-root users
check_ping_support() {
readonly PING_IP=8.8.8.8
if ping -q -W 1 -c 1 $PING_IP >/dev/null 2>&1; then
return 0
elif sudo ping -q -W 1 -c 1 $PING_IP >/dev/null 2>&1; then
return 0
else
printf "\n%sping is not installed.%s\n" "${RED}" "${CLEAR}"
return 1
fi
}
# Main script
main() {
if check_ping_support; then
update_all
printf "\n"
else
printf "\n%sInternet Disabled!!!%s\n" "${RED}" "${CLEAR}"
fi
}
main