installpkg 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env bash
  2. #
  3. # Identify host OS and execute package manager install command on input args
  4. #
  5. installpkg () {
  6. _getOS () {
  7. # Widely supported method to retrieve host $ID
  8. if [[ -e /etc/os-release ]]; then
  9. source /etc/os-release
  10. else
  11. echo "No /etc/os-release found!"
  12. echo "Your OS is unsupported!"
  13. return 1
  14. fi
  15. }
  16. _setCmdArr () {
  17. declare -ga _CmdArr
  18. # Create OS-specific package install command arrays
  19. if [[ "$ID" == "fedora" ]]; then
  20. _CmdArr=( "dnf" "install" "-y" )
  21. elif [[ "$ID" == "centos" && "$VERSION_ID" -ge 8 ]]; then
  22. _CmdArr=( "dnf" "install" "-y" )
  23. elif [[ "$ID" == "centos" && "$VERSION_ID" -le 7 ]]; then
  24. _CmdArr=( "yum" "install" "-y" )
  25. elif [[ "$ID" == "ubuntu" || "$ID" == "debian" ]]; then
  26. _CmdArr=( "apt-get" "install" "-y" )
  27. elif [[ "$ID" == "arch" ]]; then
  28. _CmdArr=( "pacman" "-Syu" )
  29. else
  30. "Your OS is currently unsupported! You are welcome to add your own and submit a PR."
  31. return 1
  32. fi
  33. # Append sudo if not running as root
  34. [[ "$(whoami)" != "root" ]] && _CmdArr=( "sudo" "${_CmdArr[@]}" )
  35. }
  36. _installPackage () {
  37. # Check for input arguments
  38. if [[ "$#" -ge 1 ]]; then
  39. if ! "${_CmdArr[@]}" "$@"; then
  40. echo "Installation failed!"
  41. return 1
  42. fi
  43. else
  44. echo "You must supply one or more packages to install!"
  45. return 1
  46. fi
  47. }
  48. __main () {
  49. _getOS && \
  50. _setCmdArr && \
  51. _installPackage "$@" && \
  52. unset _CmdArr
  53. }
  54. __main "$@"
  55. }
  56. if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  57. installpkg "$@"
  58. exit $?
  59. fi