scripts/zypper_autoremove.bash
caem 45d4b8bad6
Fix detection of having no packages
Sometimes empty elements containing only spaces are pushed in to the
packages array, before the code would only detect that there are no
pacakges to install when only one spaces was inserted, now it should
work for any arbitrary count of elements containing only spaces.
2024-05-10 13:20:41 +02:00

21 lines
692 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
sudo zypper rm -u $unneeded_package_list
else
echo "No unneeded packages."
fi