Linux 检查已安装的软件包,如果未找到则安装
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12806176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking for installed packages and if not found install
提问by amanada.williams
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?
对于其他发行版,我需要使用什么?易于获取?
采纳答案by Gilles Quenot
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 || pacman -S package
回答by Noam Manos
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
回答by Pancho
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)
例如)
回答by diyoda_
If you are doing this against downloaded RPMs. you can do it by.
如果您针对下载的 RPM 执行此操作。你可以做到。
rpm -Uvh package-name-version-tag.rpm