File keep3kernels of Package kimi-utils-ubuntu
#!/bin/sh
# Source: https://ubuntuforums.org/showthread.php?t=2474204&p=14094097#post14094097
#
# Hold the third-latest installed kernel version (as determined by debian version number)
# to mark it as not for autoremoval.
# apt already protects the latest and second-latest kernels, this is only needed for third-latest
if test $(id -u) -ne 0;then
echo 'This script must be run as root' >&2
exit 1
fi
runflagfile=/run/update-third-kernel-hold-needed
if test x"$1" != x'--force';then
# Do not run if no kernels were changed
# we must explicitly exit 0 here to avoid false indication of failure
test -f "$runflagfile" || exit 0
fi
set -e
eval $(apt-config shell DPKG Dir::bin::dpkg/f)
test -n "$DPKG" || DPKG="/usr/bin/dpkg"
list="$("${DPKG}" -l | awk '/^[ih][^nc][ ]+linux-image-[0-9]+\./ && $2 !~ /-dbg(:.*)?$/ && $2 !~ /-dbgsym(:.*)?$/ { print $2,$3; }' \
| sed -e 's#^linux-image-##' -e 's#:[^:]\+ # #')"
debverlist="$(echo "$list" | cut -d' ' -f 1 | sort --unique --reverse --version-sort)"
third_version="$(echo "$debverlist" | sed -n 3p)"
# grep exits with status 1 if no matches, don't stop the script for that here
set +e
for ver in $debverlist;do
# only print changes
apt-mark unhold "linux*${ver}*" | grep -F Cancel
done
set -e
if test -n "$third_version";then
apt-mark hold $("${DPKG}" -l | awk '/^[ih][^nc][ ]+linux-[^ ]*'"$(echo "$third_version" | sed 's/\./\\./g')"'/ && $2 !~ /-dbg(:.*)?$/ && $2 !~ /-dbgsym(:.*)?$/ { print $2; }')
fi
# Clean the flag
rm -f "$runflagfile"