#!/usr/bin/env bash # Identify host OS and execute package manager install command on input args # installpkg() { getOS() { # Widely supported method to retrieve host $ID if [[ -e /etc/os-release ]]; then source /etc/os-release else echo "No /etc/os-release found!" echo "Your OS is unsupported!" return 1 fi } setCmdArr () { declare -ga CMD_ARR # Create OS-specific package install command arrays if [[ "$ID" == "fedora" ]]; then CMD_ARR=( "dnf" "install" "-y" ) elif [[ "$ID" == "centos" && "$VERSION_ID" -ge 8 ]]; then CMD_ARR=( "dnf" "install" "-y" ) elif [[ "$ID" == "centos" && "$VERSION_ID" -le 7 ]]; then CMD_ARR=( "yum" "install" "-y" ) elif [[ "$ID" == "ubuntu" || "$ID" == "debian" ]]; then CMD_ARR=( "apt-get" "install" "-y" ) elif [[ "$ID" == "arch" ]]; then CMD_ARR=( "pacman" "-Syu" ) else echo "Your OS is currently unsupported! You are welcome to add your own and submit a PR." return 1 fi # Append sudo if not running as root [[ "$(whoami)" != "root" ]] && CMD_ARR=( "sudo" "${CMD_ARR[@]}" ) } installPackage() { # Check for input arguments if [[ "$#" -ge 1 ]]; then if ! "${CMD_ARR[@]}" "$@"; then echo "Installation failed!" return 1 fi else echo "You must supply one or more packages to install!" return 1 fi } main() { getOS && \ setCmdArr && \ installPackage "$@" && \ unset CMD_ARR } main "$@" } if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then installpkg "$@" exit $? fi