rubo77 / remove-old-kernels

20 stars 5 forks source link

Don't remove newer kernels #3

Open melroy89 opened 1 month ago

melroy89 commented 1 month ago

After I installed a newer kernel, the script will directly remove that new kernel as well.

Could you improve the script even further, so it will not remove the current kernel + NEWER.

Workaround: I need to reboot and use that kernel.

rubo77 commented 1 month ago

Maybe this function will help?

#!/bin/bash

is_newer_kernel_installed() {
    # Get the currently running kernel
    current_kernel=$(uname -r)

    # Get the latest installed kernel version
    latest_installed_kernel=$(dpkg -l | grep 'linux-image-[0-9]' | awk '{print $2}' | sed 's/linux-image-//' | sort -V | tail -n 1)

    # Compare the kernel versions
    if [ "$(printf '%s\n' "$current_kernel" "$latest_installed_kernel" | sort -V | head -n 1)" != "$current_kernel" ]; then
        return 0  # True: The latest installed kernel is newer than the running one
    else
        return 1  # False: The running kernel is the latest or no newer version is installed
    fi
}

# Call the function
if is_newer_kernel_installed; then
    echo "A newer kernel is installed than the currently running one."
else
    echo "The currently running kernel is the latest."
fi
rubo77 commented 1 month ago

I have no time to test this solution, PRs are welcome