#!/bin/bash if [[ $(which yum) ]]; then OS="CentOS" elif [[ $(which apt) ]]; then OS="Debian" elif [[ $(which apk) ]]; then OS="Alpine" elif [[ $(which zypper) ]]; then OS="OpenSuse" elif [[ $(which pacman) ]]; then OS="Arch" elif [[ $(which dnf) ]]; then OS="Fedora" else IS_UNKNOWN=1 fi echo "$IS_UNKNOWN" suse="OpenSuse" alpine2="Alpine" cent="CentOS" redhat="Fedora" deb="Debian" pacman="Arch" if (( OS==suse )) then export package_manager="zypper install -y" elif (( OS==alpine2 )) then export package_manager="apk --update add" elif (( OS==cent )) then export package_manager="yum install -y" elif (( OS==redhat )) then export package_manager="dnf install -y" elif (( OS==pacman )) then export package_manager="pacman -Sy" elif (( OS==deb )) then export package_manager="apt install -y" else printf "No package manager detected, aborting." exit 1 fi echo "$package_manager"
Trying to detect the package manager of a linux user in bash , but it always seems to output zypper even when the package manager I'm using is not zypper and zypper is not installed.Anyone know what could be causing the problem?
command -v zypper >/dev/null 2>&1 && pkg_mgr=zypper
etc.?[ -f /etc/os-release ] && OS="$(awk -F= '{/^ID=/ {print $2}')"
. Or, since the file contains shell-compatible variable assignments, you can justsource
the file and use the variables defined in it ($ID
contains the distro short name that you're after).ansible
is available, installing a package, e.g.zsh
, could be done on virtually any system withansible -bK -m ansible.builtin.package -a 'name=zsh state=present' localhost
.