purge-deinstalled 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/sh
  2. #
  3. # Purge deinstalled packages on Debian
  4. # Copyright (c) 2012 Michael Buesch <m@bues.ch>
  5. #
  6. die()
  7. {
  8. echo "$*"
  9. exit 1
  10. }
  11. usage()
  12. {
  13. echo "Purge deinstalled packages on Debian"
  14. echo
  15. echo "Usage: purge-deinstalled [OPTIONS]"
  16. echo
  17. echo "Options:"
  18. echo " -y|--yes Don't ask for confirmation"
  19. echo " --dry-run Simulate purge"
  20. }
  21. # $1=pkgname
  22. do_purge()
  23. {
  24. local pkg="$1"
  25. [ -n "$opt_dryrun" ] && {
  26. echo "Dryrun: purge $pkg"
  27. return 0
  28. }
  29. dpkg --purge "$pkg" || die "Failed to purge '$pkg'"
  30. echo
  31. }
  32. opt_yes=
  33. opt_dryrun=
  34. while [ $# -ge 1 ]; do
  35. case "$1" in
  36. -h|--help)
  37. usage
  38. exit 0
  39. ;;
  40. -y|--yes)
  41. opt_yes=1
  42. ;;
  43. --dry-run)
  44. opt_dryrun=1
  45. ;;
  46. *)
  47. die "Unknown option: $1"
  48. ;;
  49. esac
  50. shift
  51. done
  52. [ "$(id -u)" = "0" ] || die "Permission denied"
  53. for pkg in $(dpkg --get-selections |\
  54. grep -Ee '[[:space:]]deinstall$' |\
  55. grep -Eoe '^[^[:space:]]+'); do
  56. [ -n "$opt_yes" ] && {
  57. do_purge "$pkg"
  58. continue
  59. }
  60. res=
  61. read -p "Purge package '$pkg'? [y] " res || die "aborted"
  62. [ "x$res" = "x" -o\
  63. "x$res" = "xy" -o "x$res" = "xY" -o\
  64. "x$res" = "xyes" -o "x$res" = "xYES" -o\
  65. "x$res" = "x1" ] && {
  66. do_purge "$pkg"
  67. continue
  68. }
  69. done
  70. exit 0