#!/bin/sh set -euapt_opt=if [ $# -eq 0 ]; then : # interactiveelif [ "${1:-}" = "-y" ]; then apt_opt="-y" shiftfi 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"ficmd="$cmd && $aptget autoremove --purge"cmd="$cmd && $aptget upgrade && $aptget dist-upgrade"set -xexec sudo sh -c "$cmd"
#!/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
}
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"
cmd="$cmd && $aptget autoremove --purge"
cmd="$cmd && $aptget upgrade && $aptget dist-upgrade"
set -x
exec sudo sh -c "$cmd"