One year ago, I asked for a review of Simple Linux upgrade script in Bash.
Conditions are slightly changed, and/or more accurate:
Code readability for the code user, making it simple to adjust the script's behavior
Output readability for the script user, supposing it will run always in interactive form (this is sort of contradicting the original post)
Colored headings describing what exactly is the script doing
Attempt to correct things; well this one is hard to make, but let's suppose the two initial commands will do the trick
Clean-up old packages after successful update
I hereby add:
It must be a portable POSIX shell script
It must have a clear typed-in header for users to distinguish between different computers, if running this script on several machines. It could be a
hostname
, but I decided otherwise for personal purposes (I myself run it on 3 machines in sequence with SSH public key authentication)It must, in the end, check if the Linux it is running on, is Mint. If so, it needs to offer a kernel upgrade (I chose the safest way through MintUpdate) - ask if the user wants to run it
I want it to pass ShellCheck.net without any warning
#!/bin/sh
red="\\033[1;31m"
green="\\033[1;32m"
yellow="\\033[1;33m"
blue="\\033[1;34m"
nocolor="\\033[0m"
print_newline()
{
printf "\\n"
}
show_success()
{
echo "${green}Success.${nocolor}"
print_newline
}
show_error_and_exit()
{
echo "${red}An error occured.${nocolor}"
print_newline
exit "$1"
}
error_handler()
{
if [ "$1" -ne 0 ]
then
show_error_and_exit "$2"
else
show_success
fi
}
ask_for_sudo_password()
{
sudo sh -c ":"
}
is_it_linux_mint()
{
return $(grep -i mint /etc/lsb-release > /dev/null 2>&1)
}
print_newline
echo "${blue}--------------------${nocolor}"
echo "${blue}Vlasta - Laptop Mint${nocolor}"
echo "${blue}--------------------${nocolor}"
print_newline
echo "${green}Step 0: enter password${nocolor}"
# in order to print information AFTER sudo password has been provided
ask_for_sudo_password
error_handler "$?" 1
echo "${green}Step 1: configure packages${nocolor}"
echo "${yellow}dpkg --configure -a${nocolor}"
sudo dpkg --configure -a
error_handler "$?" 2
echo "${green}Step 2: fix broken dependencies${nocolor}"
echo "${yellow}apt-get install --fix-broken${nocolor}"
sudo apt-get install --fix-broken
error_handler "$?" 3
echo "${green}Step 3: update cache${nocolor}"
echo "${yellow}apt-get update${nocolor}"
sudo apt-get update
error_handler "$?" 4
echo "${green}Step 4: upgrade packages${nocolor}"
echo "${yellow}apt-get upgrade${nocolor}"
sudo apt-get upgrade
error_handler "$?" 5
echo "${green}Step 5: upgrade packages with possible removals${nocolor}"
echo "${yellow}apt-get dist-upgrade${nocolor}"
sudo apt-get dist-upgrade
error_handler "$?" 6
echo "${green}Step 6: remove unused packages${nocolor}"
echo "${yellow}apt-get --purge autoremove${nocolor}"
sudo apt-get --purge autoremove
error_handler "$?" 7
echo "${green}Step 7: clean up${nocolor}"
echo "${yellow}apt-get autoclean${nocolor}"
sudo apt-get autoclean
error_handler "$?" 8
if is_it_linux_mint
then
while true
do
echo "Do you want to check for kernel upgrade via MintUpdate?"
read -r answer
case "$answer" in
[Yy]) : ; break;;
[Nn]) exit; break;;
*) echo "Please answer Y or N.";;
esac
done
echo "${green}Step 8: Linux Mint kernel upgrade${nocolor}"
echo "${yellow}mintupdate${nocolor}"
sudo /usr/bin/python3 /usr/lib/linuxmint/mintUpdate/mintUpdate.py show
fi