linux Bash script - determine vendor and install system (apt-get, yum etc)

von4xj4u  于 2022-11-28  发布在  Linux
关注(0)|答案(2)|浏览(114)

I'm writing a shell script for use on various linux platforms. Part of the script installs a couple of packages. How can I determine the linux vendor and default system install mechanism, for example Debian/Ubuntu has apt-get/apt, Fedora has yum and so on...
Thanks in advance

m3eecexj

m3eecexj1#

你不需要检查供应商,因为他们 * 可能 * 决定改变打包系统(不太可能,但从概念上讲,你必须确保对于你测试的每个发行版,你都尝试了正确的软件包管理器命令)。

YUM_CMD=$(which yum)
  APT_GET_CMD=$(which apt-get)
  OTHER_CMD=$(which <other installer>)

然后可能按照您的偏好顺序对它们进行排序:

if [[ ! -z $YUM_CMD ]]; then
    yum install $YUM_PACKAGE_NAME
 elif [[ ! -z $APT_GET_CMD ]]; then
    apt-get $DEB_PACKAGE_NAME
 elif [[ ! -z $OTHER_CMD ]]; then
    $OTHER_CMD <proper arguments>
 else
    echo "error can't install package $PACKAGE"
    exit 1;
 fi

您可以看看gentoo(或类似于yocto或openembedded的框架)如何提供获取源代码(使用wget)的方法,如果您想要一个故障保护脚本,则可以从头开始构建。

cygmwpex

cygmwpex2#

#!/bin/sh
set -ex

OS=$(uname -s | tr A-Z a-z)

case $OS in
  linux)
    source /etc/os-release
    case $ID in
      debian|ubuntu|mint)
        apt update
        ;;

      fedora|rhel|centos)
        yum update
        ;;

      *)
        echo -n "unsupported linux distro"
        ;;
    esac
  ;;

  darwin)
    brew update
  ;;

  *)
    echo -n "unsupported OS"
    ;;
esac

相关问题