Bash 函数条件命令和参数奇怪且错误地将 Ubuntu 解释为 Alpine

Bash 函数条件命令和参数奇怪且错误地将 Ubuntu 解释为 Alpine

我重新调整了代码来检测分发包管理器,并添加了新参数来捕获值:

get_package_manager_install()
{
  if   [ -x "$(command -v $1)" ]; then echo "$1 add --no-cache $2";  # apk add --no-cache
  elif [ -x "$(command -v $1)" ]; then echo "$1 install -y $2";      # apt install -y
  elif [ -x "$(command -v $1)" ]; then echo "$1 install -y $2";      # dnf install -y
  elif [ -x "$(command -v $1)" ]; then echo "$1 -av $2";             # emerge -av
  elif [ -x "$(command -v $1)" ]; then echo "$1 -S --noconfirm $2";  # pacman -S --noconfirm
  elif [ -x "$(command -v $1)" ]; then echo "$1 in $2";              # zypper in
  else        
    echo "Unknown manager manager" &>/dev/null
  fi
}

并通过输入两个参数来执行函数:

get_package_manager_install 'apk'     "musl-locale"
get_package_manager_install 'apt'     "locales locales-all"
get_package_manager_install 'dnf'     "glibc"
get_package_manager_install 'emerge'  "sys-libs/glibc"
get_package_manager_install 'pacman'  "glibc"
get_package_manager_install 'zypper'  "glibc"

我在Ubuntu上运行它,输出显示:

apt add --no-cache locales locales-all

由于 ,该函数似乎认为 Ubuntu 是 Alpine,if [ -x "$(command -v $1)" ]; then echo "$1 add --no-cache $2"但目前应该会elif [ -x "$(command -v $1)" ]; then echo "$1 install -y $2"输出locales locales-all

相关内容