自动安装pkg的依赖包

    xiaoxiao2021-03-25  71

    当我们要按照一个pkg时,可能有几个依赖的包需要安装,用下面这个脚本可以将pkg要依赖的包自动安装上。 其用法如下: pkgs="parted" install_deps "${pkgs}" 在install_deps 中支持Debian|Ubuntu/CentOS/Fedora 这五个os,我们以centos为例 安装依赖的包,起始就是给yum加上-e 0的参数即可.  install_deps() { //给pkgs 赋值,本例中pkgs=parted      pkgs="$1" // -z表示为null,也就是说pkgs如果为null,则通过error_msg 这个函数输出信息,提示用过正确使用install_deps      [ -z "${pkgs}" ] && error_msg "Usage: install_deps pkgs"  // 调用info_msg 输出需要安装的pkgs      info_msg "Installing ${pkgs}" // 调用dist_name 来判断当前使用的os          dist_name // 通过case 语句针对不同的os,调用不通的安装函数          case "${dist}" in            Debian|Ubuntu)              # Use the default answers for all questions.              DEBIAN_FRONTEND=noninteractive apt-get update -q -y              # shellcheck disable=SC2086              DEBIAN_FRONTEND=noninteractive apt-get install -q -y ${pkgs}              ;;            CentOS)              # shellcheck disable=SC2086              yum -e 0 -y install ${pkgs}              ;;            Fedora)              # shellcheck disable=SC2086              dnf -e 0 -y install ${pkgs}              ;;            Unknown)              warn_msg "Unsupported distro: package install skipped"              ;;          esac      }    error_msg() { //给msg赋值,这里的msg=="Usage: install_deps pkgs"      msg="$1" //如果msg为null,则输出Unknown error      [ -z "${msg}" ] && msg="Unknown error" //如果msg不为null,则通过printf 输出 2表示error console。      printf "ERROR: %s\n" "${msg}" >&2 // 以返回值0退出      exit 0  }  info_msg() {      msg="$1"      [ -z "${msg}" ] && msg="Unknown info"      printf "INFO: %s\n" "${msg}" >&1  }  dist_name() { //根读取不通的路径,获取os信息.-x 表示是否可以执行,-f表示文件      if [ -x /usr/bin/lsb_release ]; then          dist="$(lsb_release -si)"      elif [ -f /etc/lsb-release ]; then          . /etc/lsb-release          dist="${DISTRIB_ID}"      elif [ -f /etc/debian_version ]; then          dist="Debian"      elif [ -f /etc/fedora-release ]; then          dist="Fedora"      elif [ -f /etc/centos-release ]; then          dist="CentOS"      else          dist="Unknown"          warn_msg "Unsupported distro: cannot determine distribution name"      fi  }

    自动安装pkg的依赖包

    转载请注明原文地址: https://ju.6miu.com/read-39997.html

    最新回复(0)