检查已安装的软件包,如果未找到则安装

时间:2021-10-09 08:53:50

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

我需要检查已安装的软件包,如果没有安装,请安装它们。

Example for RHEL, CentOS, Fedora:

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?

我如何检查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?

我还需要为其他发行版使用什么? apt-get的?

3 个解决方案

#1


36  

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 :

对于debian喜欢:

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

For archlinux :

对于archlinux:

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

#2


1  

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

基于@GillesQuenot和@Kidbulra答案,这里是一个如何循环多个包并在缺少时安装的示例:

packageList="git gcc python-devel"

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

#3


0  

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)

例如)

#1


36  

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 :

对于debian喜欢:

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

For archlinux :

对于archlinux:

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

#2


1  

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

基于@GillesQuenot和@Kidbulra答案,这里是一个如何循环多个包并在缺少时安装的示例:

packageList="git gcc python-devel"

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

#3


0  

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)

例如)