Skip to content

Instantly share code, notes, and snippets.

@rhoconlinux
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save rhoconlinux/a72e7cb33f24cc7ec71c to your computer and use it in GitHub Desktop.

Select an option

Save rhoconlinux/a72e7cb33f24cc7ec71c to your computer and use it in GitHub Desktop.
ninja-update-2
#!/bin/bash
# Author: rhoconlinux, rhoconlinux.wordpress.com
# Credit for the spinner to Tasos Latsas https://github.com/tlatsas/bash-spinner
##########################spinner funcion starts
function _spinner() {
# $1 start/stop
#
# on start: $2 display message
# on stop : $2 process exit status
# $3 spinner function pid (supplied from stop_spinner)
local on_success="DONE"
local on_fail="FAIL"
local white="\e[1;37m"
local green="\e[1;32m"
local red="\e[1;31m"
local nc="\e[0m"
case $1 in
start)
# calculate the column where spinner and status msg will be displayed
let COLS=$(tput cols)
let column=${#2}
# display message and position the cursor in $column column
echo -ne ${2}
printf "\n%${column}s"
#printf "%$1"
# start spinner
i=1
sp='\|/-'
delay=0.15
while :
do
printf "\b${sp:i++%${#sp}:1}"
sleep $delay
done
;;
stop)
if [[ -z ${3} ]]; then
echo "spinner is not running.."
exit 1
fi
kill $3 > /dev/null 2>&1
# inform the user uppon success or failure
echo -en "\b["
if [[ $2 -eq 0 ]]; then
echo -en "${green}${on_success}${nc}"
else
echo -en "${red}${on_fail}${nc}"
fi
echo -e "]"
;;
*)
echo "invalid argument, try {start/stop}"
exit 1
;;
esac
}
function start_spinner {
# $1 : msg to display
_spinner "start" "${1}" &
# set global spinner pid
_sp_pid=$!
disown
}
function stop_spinner {
# $1 : command exit status
_spinner "stop" $1 $_sp_pid
unset _sp_pid
}
##########################spinner funcion ends
clear
echo "Grant root access to update and upgrade your system:"
sudo echo "Your last update was carried at:"
stat -c %y /var/lib/apt/periodic/update-success-stamp
echo ""
# Checking pack health
echo 'Solving eventual integrity faults of the package system...'
echo "Running dpkg integrity check..."
start_spinner
sudo dpkg --configure -a
stop_spinner $?
echo ""
echo 'Running apt-get to fix eventual errors...'
start_spinner
sudo apt-get install -f -y -qq
stop_spinner $?
echo ""
# update
start_spinner 'Updating sources...'
# use sleep to give spinner time to fork and run
# because cp fails instantly
sudo apt-get update -qq
sleep 1
stop_spinner $?
echo ""
# upgrade
start_spinner 'Applying upgrades...'
# use sleep to give spinner time to fork and run
# because cp fails instantly
sudo apt-get dist-upgrade -y -qq; sleep 1; stop_spinner $?
echo ""
# removing garbage
start_spinner 'Cleaning obsolete packages from the system...'
sudo apt-get autoremove -y -qq
sudo apt-get install -f -y -qq; sleep 1; stop_spinner $?
echo ""
echo "Your system is now up-to-date."
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment