remove-old-kernels: Change confirmation logic

Handle confirmation dialog in remove-old-kernels deterministically and always
pass `-y' to apt-get. By this we can be sure that confirmation is always asked.
(Because apt-get could skip its confirmation dialog.) Also, `--force' is not
(and never was) contradictory with `--dry-run'.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
This commit is contained in:
Виталий Чикунов 2022-06-26 07:37:42 +03:00
parent 894ef55a81
commit a3524e8207

View File

@ -39,7 +39,7 @@ show_help()
cat <<EOF
Usage: $PROG [options]
Valid options are:
-f, -y, --force non-interactive kernel removal (default is interactive)
-f, -y, --force do not ask for removal confirmation (default is to ask)
-n, --dry-run just simulate removal
-t, --type remove kernels with specified flavour (ovz-smp, std-def, etc)
-a, --all remove kernels with all flavours
@ -71,10 +71,6 @@ while :; do
shift
done
if [ -n "$force" ] && [ -n "$dryrun" ]; then
show_usage '--force and --dry-run are mutually exclusive options.'
fi
if [ -n "$kernel_flavour" ] && [ -n "$all" ]; then
show_usage '--type and --all are mutually exclusive options.'
fi
@ -166,9 +162,13 @@ for kernel_flavour in $flavours; do
fi
done
done
echo "Not removing these kernels (reason why keeping):"
howmuch=$(echo "$keep_kernels" | grep -c .)
[ "$howmuch" -eq 1 ] && phrase="this kernel" || phrase="these $BRIGHT$howmuch$NORM kernels"
echo "Not removing $phrase (reason why keeping):"
echo "$keep_kernels"
echo "Removing these kernels:"
howmuch=$(echo "$remove_kernels" | grep -c .)
[ "$howmuch" -eq 1 ] && phrase="this kernel" || phrase="these $BRIGHT$howmuch$NORM kernels"
echo "Removing $phrase:"
echo "$remove_kernels"
if [ -z "$apt_args_list" ] ; then
@ -180,8 +180,26 @@ if [ "$UID" != "0" ]; then
echo >&2 "${RED}Warning: This program requires root privileges.$NORM"
fi
echo -n "Confirm" ${dryrun:+--dry-run} "uninstall action for $phrase [Y/n]? "
if [ -n "$force" ]; then
echo "yes"
else
while true; do
read -r || { echo "Aborting"; exit 1; }
shopt -s nocasematch
case "$REPLY" in
n|no|q) exit 0 ;;
y|ye|yes|'') break ;;
*) ;;
esac
echo -n "[Y/n] "
shopt -u nocasematch
done
fi
# shellcheck disable=SC2086
V apt-get $force $dryrun remove $apt_args_list
V apt-get -y $dryrun remove $apt_args_list
# Mask non-zero apt exit code on dry run:
if [ -n "$dryrun" ]; then