Checking for installed packages and if not found install

老子叫甜甜 提交于 2019-12-03 05:49:23

问题


I need to check for installed packages and if not installed install them.

Example for RHEL, CentOS, Fedora:

rpm -qa | grep glibc-static
glibc-static-2.12-1.80.el6_3.5.i686

How do I do a check in BASH?

Do I do something like?

if [ "$(rpm -qa | grep glibc-static)" != "" ] ; then

And what do I need to use for other distributions? apt-get?


回答1:


Try the following code :

if ! rpm -qa | grep -qw glibc-static; then
    yum install glibc-static
fi

or shorter :

rpm -qa | grep -qw glibc-static || yum install glibc-static

For debian likes :

dpkg -l | grep -qw package || apt-get install package

For archlinux :

pacman -Qq | grep -qw package || pacman -S package



回答2:


Based on @GillesQuenot and @Kidbulra answers, here's an example how to loop over multiple packages, and install if missing:

packageList="git gcc python-devel"

for packageName in $packageList; do
  rpm --quiet --query $packageName || sudo yum install -y $packageName
done



回答3:


if [ $(yum list installed | cut -f1 -d" " | grep --extended '^full name of package being checked$' | wc -l) -eq 1 ]; then
  echo "installed";
else
  echo "missing"
fi

I use this because it returns installed / missing without relying on an error state (which can cause problems in scripts taking a "no tolerance" approach to errors via

set -o errexit

for example)




回答4:


If you are doing this against downloaded RPMs. you can do it by.

rpm -Uvh package-name-version-tag.rpm


来源:https://stackoverflow.com/questions/12806176/checking-for-installed-packages-and-if-not-found-install

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!