Skip to content

Instantly share code, notes, and snippets.

@der-hugo
Created November 10, 2017 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save der-hugo/3b7496c14a77c6915b92fc9ca780023c to your computer and use it in GitHub Desktop.
Save der-hugo/3b7496c14a77c6915b92fc9ca780023c to your computer and use it in GitHub Desktop.
Script for automated Ubuntu apt updates
#!/bin/bash
#########################
## returns:
# 0 - success
# 1 - success : Requires reboot
# 2 - updated failed
# 3 - upgrade failed
# 4 - dist-upgrade failed
# 5 - autoremove failed
# 6 - unknown parameter
UPDATE='sudo apt update'
#########################
## Check Parameter
if [ $# -eq 0 ]; then
# no parameter
UPGRADE='sudo apt upgrade'
DISTUPGRADE='sudo apt dist-upgrade'
AUTOREMOVE='sudo apt autoremove'
else
if [[ $1 == '-y' || $1 == '-Y' ]]; then
UPGRADE='sudo apt upgrade -y'
DISTUPGRADE='sudo apt dist-upgrade -y'
AUTOREMOVE='sudo apt autoremove -y'
else
echo -e "\e[91m RECEIVED UNKNOWN PARAMETER \e[39m"
exit 6
fi
fi
#########################
## First get sudo rights
sudo echo ""
#########################
## Update Server
echo -e "\e[92m UPDATE PACKAGE SOURCES \e[39m"
$UPDATE
if [ $? -eq 0 ]; then
echo -e "\e[92m OK \e[39m"
echo ""
echo -e "\e[92m UPGRADE TO NEW PACKAGES \e[39m"
echo $UPGRADE
echo ""
$UPGRADE
if [ $? -eq 0 ]; then
echo -e "\e[92m OK \e[39m"
echo ""
echo -e "\e[92m UPGRADE DISTRIBUTION-PACKAGES \e[39m"
echo $DISTUPGRADE
echo ""
$DISTUPGRADE
if [ $? -eq 0 ]; then
echo -e "\e[92m OK \e[39m"
echo ""
echo -e "\e[92m AUTOREMOVE OLD PACKAGES \e[39m"
echo $AUTOREMOVE
echo ""
$AUTOREMOVE
if [ $? -eq 0 ]; then
echo -e "\e[92m OK \e[39m"
clear
if [ -f /var/run/reboot-required ]; then
echo -e "\e[92m#####################################\e[39m"
echo -e "\e[92m# #\e[39m"
echo -e "\e[92m# DONE #\e[39m"
echo -e "\e[92m# #\e[39m"
echo -e "\e[92m# \e[91mNOTE: A reboot of the System is\e[92m #\e[39m"
echo -e "\e[92m# \e[91mrequired in order to\e[92m #\e[39m"
echo -e "\e[92m# \e[91mcomplete the Update!\e[92m #\e[39m"
echo -e "\e[92m# #\e[39m"
echo -e "\e[92m#####################################\e[39m"
exit 1
else
echo -e "\e[92m#####################################\e[39m"
echo -e "\e[92m# #\e[39m"
echo -e "\e[92m# DONE #\e[39m"
echo -e "\e[92m# #\e[39m"
echo -e "\e[92m#####################################\e[39m"
exit 0
fi
else
echo -e "\e[91m#####################################\e[39m"
echo -e "\e[91m# #\e[39m"
echo -e "\e[91m# AUTOREMOVE FAILED! #\e[39m"
echo -e "\e[91m# #\e[39m"
echo -e "\e[91m#####################################\e[39m"
exit 5
fi
else
echo -e "\e[91m#####################################\e[39m"
echo -e "\e[91m# #\e[39m"
echo -e "\e[91m# DIST-UPGRADE FAILED! #\e[39m"
echo -e "\e[91m# #\e[39m"
echo -e "\e[91m#####################################\e[39m"
exit 4
fi
else
echo -e "\e[91m#####################################\e[39m"
echo -e "\e[91m# #\e[39m"
echo -e "\e[91m# UPGRADE FAILED! #\e[39m"
echo -e "\e[91m# #\e[39m"
echo -e "\e[91m#####################################\e[39m"
exit 3
fi
else
echo -e "\e[91m#####################################\e[39m"
echo -e "\e[91m# #\e[39m"
echo -e "\e[91m# UPDATE FAILED! #\e[39m"
echo -e "\e[91m# #\e[39m"
echo -e "\e[91m#####################################\e[39m"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment