Created
May 17, 2025 22:00
-
-
Save EarMaster/9541b1033e0c4a8c85b350cf4b8ab953 to your computer and use it in GitHub Desktop.
Automates APT updates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Exit immediately if a command exits with a non-zero status | |
set -e | |
# Function to handle errors | |
error_handler() { | |
echo "Error occurred at line $1" | |
exit 1 | |
} | |
# Set up error handling | |
trap 'error_handler $LINENO' ERR | |
# Add some color | |
GREEN="\033[0;32m" | |
RED="\033[0;31m" | |
YELLOW="\033[0;33m" | |
NC="\033[0m" # No Color | |
echo -e "${YELLOW}Checking for updates...${NC}" | |
# Check for updates with error redirection | |
aptOutput=$(apt-get update 2>&1) | |
echo "$aptOutput" | |
echo | |
# Use apt-get to check if upgrades are available | |
upgradeCheck=$(apt-get --simulate upgrade 2>&1 | grep -c "^Inst" || echo "0") | |
# Ensure we have a clean integer value | |
upgradeCheck=$(echo "$upgradeCheck" | tr -d '[:space:]') | |
if [[ "$upgradeCheck" -gt 0 ]]; then | |
echo -e "${YELLOW}$upgradeCheck packages can be upgraded.${NC}" | |
# Display available upgrades (using apt-get) | |
apt-get --simulate upgrade 2>&1 | grep "^Inst" | awk '{print $2}' | sort | |
echo | |
# Ask to upgrade | |
read -p "Do you want to upgrade? [y/N] " -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
echo -e "${YELLOW}Upgrading packages...${NC}" | |
apt-get upgrade -y | |
echo -e "${YELLOW}Removing unused packages...${NC}" | |
apt-get autoremove -y | |
echo -e "${GREEN}Upgrade completed successfully.${NC}" | |
else | |
echo -e "${YELLOW}Upgrade skipped.${NC}" | |
fi | |
else | |
echo -e "${GREEN}System is up to date!${NC}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment