As I will be deploying this script on multiple machines with the very same system Linux Mint 18 with rather same configuration, I would like to be semi-sure I won't screw things up much.
This little script will run every day from the root
's crontab
and will be logged into syslog
or my own log AFTER this manual script passes some quality checks. Logging is not the question now. Automation is also not the question yet. In the meantime it is being executed as normal user and elevated with sudo
internally. No help message needed. And no docs needed either. I ran it through Shellcheck.net. I have also ruled out the use of subjectively nicer apt
, because it is not meant to be used in scripts.
Conditions still are:
- Code readability
- Output readability
- Colored headings
- Attempt to correct things
- Clean-up after update
My current idea is a little more advanced than the first one, having implemented exit code checking was a great idea:
#!/bin/bash
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
NOCOLOR="\033[0m"
function show_success {
echo -e "${GREEN}Success.${NOCOLOR}\n"
}
function show_error_and_exit {
echo -e "${RED}An error occured.${NOCOLOR}\n"
exit "$1"
}
function error_handler {
if [[ $1 -ne 0 ]];
then
show_error_and_exit "$2"
else
show_success
fi
}
echo -e "\n${GREEN}Step 1: configure packages${NOCOLOR}"
echo -e "${YELLOW}dpkg --configure -a${NOCOLOR}"
sudo dpkg --configure -a
error_handler $? 1
echo -e "${GREEN}Step 2: fix broken dependencies${NOCOLOR}"
echo -e "${YELLOW}apt-get install --fix-broken${NOCOLOR}"
sudo apt-get install --fix-broken
error_handler $? 2
echo -e "${GREEN}Step 3: update cache${NOCOLOR}"
echo -e "${YELLOW}apt-get update${NOCOLOR}"
sudo apt-get update
error_handler $? 3
echo -e "${GREEN}Step 4: upgrade packages${NOCOLOR}"
echo -e "${YELLOW}apt-get upgrade${NOCOLOR}"
sudo apt-get upgrade
error_handler $? 4
echo -e "${GREEN}Step 5: upgrade distribution${NOCOLOR}"
echo -e "${YELLOW}apt-get dist-upgrade${NOCOLOR}"
sudo apt-get dist-upgrade
error_handler $? 5
echo -e "${GREEN}Step 6: remove unused packages${NOCOLOR}"
echo -e "${YELLOW}apt-get --purge autoremove${NOCOLOR}"
sudo apt-get --purge autoremove
error_handler $? 6
echo -e "${GREEN}Step 7: clean up${NOCOLOR}"
echo -e "${YELLOW}apt-get autoclean${NOCOLOR}"
sudo apt-get autoclean
error_handler $? 7