centos 帮助使用yum安装软件包的shell脚本

sc4hvdpw  于 2022-11-07  发布在  Shell
关注(0)|答案(1)|浏览(251)

我写了下面的脚本,因为我试图帮助用户安装他们需要从一个yum仓库包。
该脚本的用法是./script PACKAGE VERSION


# !/bin/sh

PAKAGENAME=${1}
VERSION=${2}

if [[ -z ${1} ]]; then
        echo "you should at least specify a component name"
        echo "Usage      : installrpm {COMPONENT} {VERSION}"
    elif [[ -z ${2} ]]; then
        echo "the latest version of the component will be installed"
        sudo yum install -y ${1}
    elif [[ ${1} == all ]]; then
        echo "All component will be installed in latest versions available"
        sudo yum remove -y PAKAGE1 
        sudo yum install -y PAKAGE1 --skip-broken
        sudo yum remove -y PAKAGE2 
        sudo yum install -y PAKAGE2 --skip-broken
    else
        sudo yum remove -y ${1}
        sudo yum install -y ${1}-${2}
fi

当我使用./script packagename version./script packagename时,它可以工作,但当我尝试使用./script packagename all时,我得到以下错误:

the latest deployed version of the component will be installed
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
No package all available.
Error: Nothing to do

看起来脚本开始寻找一个包名all,即使我在这种情况下指定包。
我如何才能修复这个错误,我将感谢任何增强的脚本。

klsxnrf1

klsxnrf11#

我认为问题在于第二个elif。您正在检查第一个opt(${1})是否等于all,但在命令./script packagename all中,opt all是第二个参数。因此,您可以尝试修改脚本,如下所示:


# !/bin/bash

PAKAGENAME=${1}
VERSION=${2}

if [[ -z ${1} ]]; then
  echo "you should at least specify a component name"
  echo "Usage      : installrpm {COMPOSANT} {VERSION}"
elif [[ -z ${2} ]]; then
  echo "the latest version of the component will be installed"
  echo "$PAKAGENAME"
elif [[ ${2} == 'all' ]]; then
  echo "All component will be installed in latest versions available"
  echo "$VERSION"
else
  echo "else"
fi

相关问题