#!/bin/sh set -eu apt_opt= if [ $# -eq 0 ]; then : # interactive elif [ "${1:-}" = "-y" ]; then apt_opt="-y" shift fi obsolete_kernels() { local current= latest= local v= p= if [ $# -gt 2 ]; then current="$1" latest="$2" shift 2 for v; do COLUMNS=500 dpkg -l "linux*$v*" | sed -n -e 's|^.. \(linux[^ \]\+\)[ \t].*|\1|p' done | while read p; do case "$p" in *-$current|*-$current-*|*-$latest|*-$latest-*) echo "$p: SKIP" >&2 ;; *) printf "$p " ;; esac done fi } remove=$(obsolete_kernels \ "$(uname -r | sed -e 's|-generic$||')" \ $(ls -1 /boot/vmlinuz-* | \ sed -e 's|^/boot/vmlinuz-||' -e 's|\.efi\.signed$||' -e 's|-generic$||' | \ sort -urV)) aptget="apt-get${apt_opt:+ $apt_opt}" cmd="$aptget update" if [ -n "$remove" ]; then cmd="$cmd && dpkg -P $remove" fi cmd="$cmd && $aptget autoremove --purge" cmd="$cmd && $aptget upgrade && $aptget dist-upgrade" set -x exec sudo sh -c "$cmd"