Skip to content

Instantly share code, notes, and snippets.

@colorwebdesigner
Last active February 12, 2022 08:56
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 colorwebdesigner/a8a2d25470bd2cb0c8a9fa847bbd20ef to your computer and use it in GitHub Desktop.
Save colorwebdesigner/a8a2d25470bd2cb0c8a9fa847bbd20ef to your computer and use it in GitHub Desktop.
CLI GUI for linux system update and upgrade
#!/bin/bash
# system-updater
# ---
# CLI GUI for system update and upgrade
# --------------------------------------------------------
# author: Ivan Pro Tools
# github: https://github.com/colorwebdesigner
# date: 17-01-2022
# license: GNU
# --------------------------------------------------------
# Arguments:
#
# [ ] -
# [ ] -
#
# Sample usage in the bottom
# ========================================================
# DEBUGGER On/Off
# set -x
# DEFAULT VARIABLES
# -------------------------
DEVNAME="Ivan Pro Tools"
LICENSE="GNU"
GITURL="https://github.com/colorwebdesigner"
# MAIN FUNCTION
# ---------------------------
system-updater () {
# Local variables
# -------------------------
local status msg lang
# Dependencies
# -------------------------
function banner () {
local banner="
---------------------------------
${1} by ${DEVNAME}
---------------------------------\n"
printf "${banner}"
}
function stepTitle() {
printf " --> ${c[y]}${1} ${c[n]}"
tput sc && printf "\n"
return 0
}
function stepResult () {
local report=${2:-done}
if [ $1 -eq 0 ]; then
tput rc && tput ed && printf "${c[g]}${report}${c[n]}\n"
elif [ $1 -eq 1 ]; then
tput rc && printf "${c[r]}error ${1}${c[n]}\n" && exit 1
else
tput rc && tput ed && tput cnorm
printf "${c[g]}${report}${c[n]}\n\n" && exit 1
fi
unset status
return 0
}
function checkForUpdate() {
local string="apt list --upgradable"
sudo apt update
msg="$(sudo apt update 2>/dev/null | grep "${string}" | cut -d '.' -f 1)"
[[ $msg ]] && return 0 || return 2
}
function listUpdate() {
tput civis && IFS=$'\n'
data=($(sudo apt list --upgradable 2>/dev/null)); unset 'data[0]'
for line in "${data[@]}"; do
printf "%s\n" " --> ${line}"
done
while true; do
tput rc && read -er -n1 -p "${c[y]}Install this updates? ${c[c]}[y/enter] [n]${c[n]}" reply
case ${reply} in
[Yy]|"" ) tput cnorm; msg="${c[y]}Update: ${c[g]}yes${c[n]}"; break;;
[Nn] ) tput cnorm; exit 1;;
* ) continue;;
esac
done
IFS=$' ' && return 0
}
function installUpdate() {
tput civis
sudo apt -y dist-upgrade
sudo apt autoremove --purge
tput cnorm && return 0
}
# DO THE JOB
# -------------------------
banner $FUNCNAME
# -------------------------
# [1] Check for updates
# -------------------------
stepTitle 'Check for updates:'
checkForUpdate; status=$?
stepResult $status "${msg:-все пакеты имеют последние версии}"
# -------------------------
# [2] Manage updates
# -------------------------
printf " --> " && tput sc && printf "\n"
listUpdate; status=$?
stepResult $status "${msg}"
# -------------------------
# [3] Install updates
# -------------------------
stepTitle 'Installation:'
installUpdate; status=$?
stepResult $status
# Exit function
unset status msg
printf "\n" && exit 0
}
# Declare array with color values to colorise output
declare -A c=( ['c']='' ['y']='' ['r']='' ['g']='' ['n']='' )
# Sample usage
system-updater "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment