made the script working

This commit is contained in:
pika 2024-12-29 18:41:56 +01:00
parent 58a037aad6
commit 34abf81fd1

View file

@ -31,6 +31,33 @@ command_exists() {
command -v "$@" >/dev/null 2>&1 command -v "$@" >/dev/null 2>&1
} }
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# ─< Check if the user is root and set sudo variable if necessary >───────────────────────
check_root() {
if [ "$(id -u)" -ne 0 ]; then
if command_exists sudo; then
echo_info "User is not root. Using sudo for privileged operations."
_sudo="sudo"
else
echo_error "No sudo found and you're not root! Can't install packages."
return 1
fi
else
echo_info "Root access confirmed."
_sudo=""
fi
}
run_setup() {
if command_exists unattended-upgrades; then
$_sudo systemctl enable --now unattended-upgrades || echo_error "Something went wrong! Could not setup the service/autostart"
fi
}
run_checks() { run_checks() {
echo_info "Checking if unattended-upgrades is active..." echo_info "Checking if unattended-upgrades is active..."
@ -40,7 +67,7 @@ run_checks() {
else else
echo_warning "unattended-upgrades is not installed. Attempting to install..." echo_warning "unattended-upgrades is not installed. Attempting to install..."
if command_exists apt-get; then if command_exists apt-get; then
if apt-get update && apt-get install --assume-yes unattended-upgrades; then if $_sudo apt-get update && $_sudo apt-get install --assume-yes unattended-upgrades; then
echo_note "unattended-upgrades successfully installed." echo_note "unattended-upgrades successfully installed."
else else
echo_error "Failed to install unattended-upgrades. Exiting." echo_error "Failed to install unattended-upgrades. Exiting."
@ -56,31 +83,20 @@ run_checks() {
UNATTENDED_UPGRADES_FILE="/etc/apt/apt.conf.d/50unattended-upgrades" UNATTENDED_UPGRADES_FILE="/etc/apt/apt.conf.d/50unattended-upgrades"
if [ -f "$UNATTENDED_UPGRADES_FILE" ]; then if [ -f "$UNATTENDED_UPGRADES_FILE" ]; then
echo_info "Configuring unattended upgrades in $UNATTENDED_UPGRADES_FILE" echo_info "Configuring unattended upgrades in $UNATTENDED_UPGRADES_FILE"
# Add or modify configurations as needed run_setup || echo_error "Something went wrong when configuring unattended-upgrades.."
# Example: $_sudo sed -i 's/old_value/new_value/' "$UNATTENDED_UPGRADES_FILE"
else else
echo_error "Unattended upgrades file not found!" echo_error "Unattended upgrades file not found!"
fi fi
} }
run_setup() {
if command_exists unattended-upgrades; then
systemctl enable --now unattended-upgrades || echo_error "Something went wrong! Could not setup the service/autostart"
fi
}
# Main script # Main script
main() { main() {
echo_info "Starting unattended-upgrades check script..." echo_info "Starting unattended-upgrades check script..."
if [ "$(id -u)" -ne 0 ]; then if check_root; then
echo_error "This script must be run as root. Please run with sudo." run_checks
exit 1 # run_setup
fi fi
run_checks
run_setup && echo_note "Script completed successfully!"
} }
main main