-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
路235 lines (202 loc) 路 7.48 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
路235 lines (202 loc) 路 7.48 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
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
#!/bin/sh
###################################################################################################
# File: install.sh
# Author: [Vatsal Gupta (gvatsal60)]
# Description: This script updates shell configuration files based on the
# adjusted Linux distribution ID determined from /etc/os-release.
###################################################################################################
###################################################################################################
# License
###################################################################################################
# This script is licensed under the Apache 2.0 License.
###################################################################################################
# Global Variables & Constants
###################################################################################################
# Exit the script immediately if any command fails
set -e
readonly FILE_NAME=".update.sh"
readonly UPDATE_SCRIPT_SOURCE_URL="https://raw.githubusercontent.com/gvatsal60/Linux-All-In-One-Update-Script/HEAD/${FILE_NAME}"
UPDATE_SOURCE_STR=$(
cat <<EOF
# System Update
update() {
# Check if curl is available
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required but not installed. Please install curl." >&2
return
fi
# Check internet connection by pinging a reliable server
TEST_URL="https://www.google.com"
# Use curl to check the connection
TEST_RESP=\$(curl -Is --connect-timeout 2 --max-time 5 "\${TEST_URL}" 2>/dev/null | head -n 1)
# Check if response is empty
if [ -z "\${TEST_RESP}" ]; then
echo "No Internet Connection!!!" >&2
return
fi
# Check for "200" in the response
if ! printf "%s" "\${TEST_RESP}" | grep -q "200"; then
echo "Internet is not working!!!" >&2
return
fi
curl -fsSL "${UPDATE_SCRIPT_SOURCE_URL}" | sudo \${SHELL}
}
EOF
)
###################################################################################################
# Functions
###################################################################################################
# Function: println
# Description: Prints a message to the console, followed by a newline.
# Usage: println "Your message here"
println() {
printf "%s\n" "$*" 2>/dev/null
}
# Function: print_err
# Description: Prints an error message to the console, followed by a newline.
# Usage: print_err "Your error message here"
print_err() {
printf "%s\n" "$*" >&2
}
# Function: check_cmd
# Description: Checks if a specified command is available in the system.
# Usage: check_cmd "command_name"
check_cmd() {
command_name="$1"
if ! command -v "${command_name}" >/dev/null 2>&1; then
return 1
fi
return 0
}
# Function: check_command
# Description: Checks if a specified command is available in the system.
# Prints a message indicating whether the command is installed.
# Usage: check_command "command_name"
check_command() {
command_name="$1"
if ! command -v "${command_name}" >/dev/null 2>&1; then
print_err "${command_name} is not installed."
return 1
fi
return 0
}
# Function: install_pkg
# Description: Installs a specified package using the appropriate package manager
# if the system's package manager is available.
install_pkg() {
pkg_name="$1"
if ! check_cmd "${pkg_name}"; then
# Setup INSTALL_CMD & PKG_MGR_CMD
if check_cmd apt-get; then
PKG_MGR_CMD=apt-get
INSTALL_CMD="${PKG_MGR_CMD} -y install --no-install-recommends"
elif check_cmd apk; then
PKG_MGR_CMD=apk
INSTALL_CMD="${PKG_MGR_CMD} add --no-cache"
elif check_cmd pacman; then
PKG_MGR_CMD=pacman
INSTALL_CMD="${PKG_MGR_CMD} -S --noconfirm --needed"
elif check_cmd microdnf; then
PKG_MGR_CMD=microdnf
INSTALL_CMD="${PKG_MGR_CMD} -y install --refresh --best --nodocs --noplugins --setopt=install_weak_deps=0"
elif check_cmd dnf; then
PKG_MGR_CMD=dnf
INSTALL_CMD="${PKG_MGR_CMD} -y install"
elif check_cmd yum; then
PKG_MGR_CMD=yum
INSTALL_CMD="${PKG_MGR_CMD} -y install"
else
print_err "Error: Unsupported or unrecognized package manager"
exit 1
fi
case "${ADJUSTED_ID}" in
debian)
"${PKG_MGR_CMD}" update && "${INSTALL_CMD}" "${pkg_name}"
;;
rhel)
"${PKG_MGR_CMD}" update && "${INSTALL_CMD}" "${pkg_name}"
;;
alpine)
"${PKG_MGR_CMD}" update && "${INSTALL_CMD}" "${pkg_name}"
;;
arch)
"${INSTALL_CMD}" "${pkg_name}"
;;
*)
print_err "Error: Unable to install ${pkg_name} for distro ${ID}"
;;
esac
fi
}
# Function: update_rc
# Description: Update shell configuration files
update_rc() {
_rc=""
case "${ADJUSTED_ID}" in
debian | rhel)
_rc="${HOME}/.bashrc"
;;
alpine | arch)
_rc="${HOME}/.profile"
;;
*)
print_err "Error: Unsupported or unrecognized Linux distribution ${ADJUSTED_ID}"
exit 1
;;
esac
# Check if `alias update='sudo sh ${HOME}/.update.sh'` is already defined, if not then append it
if [ -f "${_rc}" ]; then
if ! awk '/^update\(\) {/,/^}/' "${_rc}" | grep -q 'curl'; then
println "=> Updating ${_rc} for ${ADJUSTED_ID}..."
println "${UPDATE_SOURCE_STR}" >>"${_rc}"
fi
else
# Notify if the rc file does not exist
println "=> Profile not found. ${_rc} does not exist."
println "=> Creating the file ${_rc}... Please note that this may not work as expected."
# Create the rc file
touch "${_rc}"
# Append the sourcing block to the newly created rc file
println "${UPDATE_SOURCE_STR}" >>"${_rc}"
fi
println ""
println "=> Close and reopen your terminal to start using 'update' alias"
println " OR"
println "=> Run the following to use it now:"
println ">>> source ${_rc} # This loads update alias"
}
###################################################################################################
# Main Script
###################################################################################################
OS=$(uname)
case "${OS}" in
Linux)
# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME
# shellcheck source=/dev/null
. /etc/os-release
# Get an adjusted ID independent of distro variants
if [ "${ID}" = "debian" ] || [ "${ID_LIKE#*debian}" != "${ID_LIKE}" ]; then
ADJUSTED_ID="debian"
elif [ "${ID}" = "arch" ] || [ "${ID_LIKE#*arch}" != "${ID_LIKE}" ]; then
ADJUSTED_ID="arch"
elif [ "${ID}" = "rhel" ] || [ "${ID}" = "fedora" ] || [ "${ID}" = "mariner" ] || [ "${ID_LIKE#*rhel}" != "${ID_LIKE}" ] || [ "${ID_LIKE#*fedora}" != "${ID_LIKE}" ] || [ "${ID_LIKE#*mariner}" != "${ID_LIKE}" ]; then
ADJUSTED_ID="rhel"
elif [ "${ID}" = "alpine" ]; then
ADJUSTED_ID="alpine"
else
print_err "Error: Linux distro ${ID} not supported."
exit 1
fi
;;
*)
print_err "Error: Unsupported or unrecognized OS distribution ${ADJUSTED_ID}"
exit 1
;;
esac
# Check if curl is available
if ! check_command curl; then
# Install curl
install_pkg curl
fi
# Update the rc (.bashrc, .profile ...) file for `update` alias
update_rc