scripts/zypper_autoremove.bash
caem 7f35cdc86a
Fix xargs misbehaving when there are no packages
Adding the -r argument to xargs makes it not run for no reason when
there are no packages to trim spaces from. Otherwise it'd always run at
least once and sometimes misbehave.
2024-05-10 13:15:31 +02:00

21 lines
677 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 [ ! "$unneeded_package_list" = " " ]; then
sudo zypper rm -u $unneeded_package_list
else
echo "No unneeded packages."
fi