package-manager.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. display_help(){
  4. echo "Usage: ${0##*/} [OPTIONS] [PACKAGE(S)]"
  5. echo ""
  6. echo "Works with: pacman, apt, zypper, dnf"
  7. echo ""
  8. echo "OPTIONS:"
  9. echo " -u, --update Update packages"
  10. echo " -i, --install Install packages"
  11. echo " -r, --remove Remove packages"
  12. echo " -c, --clear-cache Clear package cache"
  13. echo " -s, --search Search for packages"
  14. echo " -l, --list List installed packages"
  15. echo " -d, --depends List package dependencies"
  16. echo " -h, --help Display this help message"
  17. echo ""
  18. exit 2
  19. }
  20. [[ $# -lt 1 ]] && display_help
  21. zypper_manager(){
  22. if [ -e /etc/os-release ]; then
  23. . /etc/os-release
  24. else
  25. . /usr/lib/os-release
  26. fi
  27. action="$1"
  28. shift
  29. case "${action,,}" in
  30. u | update) sudo zypper ref
  31. if [[ $ID = "opensuse-tumbleweed" ]]; then
  32. sudo zypper dup
  33. elif [[ $ID = "opensuse-leap" ]]; then
  34. sudo zypper up
  35. fi ;;
  36. i | install) sudo zypper in -- "$@";;
  37. r | remove) sudo zypper re -- "$@";;
  38. c | clear) zypper clean -a;;
  39. s | search) zypper se -- "$@";;
  40. l | list) zypper lp -- "$@";;
  41. d | depends) rpm -qR -- "$@";;
  42. h|\?|:|*) display_help ;;
  43. esac
  44. }
  45. apt_manager(){
  46. action="$1"
  47. shift
  48. case "${action,,}" in
  49. u | update) sudo apt update && sudo apt upgrade;;
  50. i | install) sudo apt install -- "$@";;
  51. r | remove) sudo apt autopurge -- "$@";;
  52. c | clear) sudo apt autoremove && sudo apt clean && sudo apt autoclean;;
  53. s | search) apt search -- "$@";;
  54. l | list) apt list -- "$@";;
  55. d | depends) apt-cache depends -- "$@";;
  56. h|\?|:|*) display_help ;;
  57. esac
  58. }
  59. pacman_manager(){
  60. action="$1"
  61. shift
  62. case "${action,,}" in
  63. u | update) sudo pacman -Syu --needed;;
  64. i | install) sudo pacman -Syu --needed -- "$@";;
  65. r | remove) sudo pacman -Rncssu -- "$@";;
  66. c | clear) sudo pacman -Scc;;
  67. s | search) pacman -Qs -- "$@";;
  68. l | list) pacman -Ss -- "$@";;
  69. d | depends) pacman -Q -- "$@";;
  70. h|\?|:|*) display_help ;;
  71. esac
  72. }
  73. dnf_manager(){
  74. action="$1"
  75. shift
  76. case "${action,,}" in
  77. u | update) sudo dnf upgrade --refresh;;
  78. i | install) sudo dnf install -- "$@";;
  79. r | remove) sudo dnf remove -- "$@";;
  80. c | clear) sudo dnf clean all;;
  81. s | search) dnf search all -- "$@";;
  82. l | list) dnf list -- "$@";;
  83. d | depends) rpm -qR -- "$@";;
  84. h|\?|:|*) display_help ;;
  85. esac
  86. }
  87. if command -v pacman; then
  88. pacman_manager "$@"
  89. elif command -v apt; then
  90. apt_manager "$@"
  91. elif command -v zypper; then
  92. zypper_manager "$@"
  93. elif command -v dnf; then
  94. dnf_manager "$@"
  95. else
  96. echo "package manager not supported!@!@!@"
  97. exit 2
  98. fi