scripts/zypper_autoremove.bash
2024-05-14 10:58:26 +02:00

22 lines
726 B
Bash
Executable file

#!/usr/bin/env bash
# Script that imitates `apt autoremove` for zypper as there is no native solution.
package_list=$(LC_ALL=C sudo zypper packages --unneeded)
table_start=$(echo "$package_list" | grep -n "S | Repository | Name"| gawk '{print $1}' FS=":")
unneeded_packages=()
while IFS= read -r package; do
package="$(echo "$package"| awk -F '|' '{ print $3 }' | xargs -r)"
unneeded_packages+=("$package")
done < <(echo "$package_list" | tail -n+$((table_start+2)))
unneeded_package_list=$(printf '%s ' "${unneeded_packages[@]}")
if [ -n "$(echo "$unneeded_package_list" | tr -d ' ')" ]; then
# shellcheck disable=SC2086
sudo zypper rm -u $unneeded_package_list
else
echo "No unneeded packages."
fi