我的二进制文件依赖于这些 boost 库和相应的包:
NAME | PACKAGE:
--------------------------------------+-----------------------------------
libboost_serialization.so.1.55.0 | libboost-serialization1.55.0
libboost_thread.so.1.55.0 | ibboost-thread1.55.0
libboost_date_time.so.1.55.0 | libboost-date-time1.55.0
libboost_signals.so.1.55.0 | libboost-signals1.55.0
所以我想定义一个包列表:
boostlibnames="libboost-serialization1.55.0 libboost-thread1.55.0 libboost-date-time1.55.0 libboost-signals1.55.0"
现在我想检查它们是否全部安装,然后才运行实际的apt-get install
.我知道 apt 会自动检查软件包是否已安装,但我想在这样做之前提示用户是否要安装缺少的库。
那么有没有一种好方法来检查是否安装了所有列出的库(在上面的变量中)?
答案1
该dpkg -s
命令返回已安装软件包的状态。例如,在我的系统上,如果我运行它,firefox
哪个已安装,nedit
哪个未安装,我会得到:
$ dpkg -s firefox
Package: firefox
Status: install ok installed
Priority: optional
Section: web
Installed-Size: 94341
Maintainer: Clement Lefebvre <[email protected]>
Architecture: amd64
Version: 41.0~linuxmint1+betsy
Replaces: firefox-l10n-af, firefox-l10n-ar, firefox-l10n-be, firefox-l10n-bg, firefox-l10n-bn-bd, firefox-l10n-ca, firefox-l10n-cs, firefox-l10n-da, firefox-l10n-de, firefox-l10n-el, firefox-l10n-en-gb, firefox-l10n-en-us, firefox-l10n-eo, firefox-l10n-es, firefox-l10n-et, firefox-l10n-eu, firefox-l10n-fa, firefox-l10n-fi, firefox-l10n-fr, firefox-l10n-fy, firefox-l10n-gl, firefox-l10n-gu, firefox-l10n-he, firefox-l10n-hi, firefox-l10n-hr, firefox-l10n-hu, firefox-l10n-id, firefox-l10n-is, firefox-l10n-it, firefox-l10n-ja, firefox-l10n-kn, firefox-l10n-ko, firefox-l10n-lt, firefox-l10n-lv, firefox-l10n-nb, firefox-l10n-nl, firefox-l10n-nn, firefox-l10n-pl, firefox-l10n-pt, firefox-l10n-pt-br, firefox-l10n-ro, firefox-l10n-ru, firefox-l10n-sk, firefox-l10n-sl, firefox-l10n-sq, firefox-l10n-sr, firefox-l10n-sv, firefox-l10n-th, firefox-l10n-tr, firefox-l10n-uk, firefox-l10n-zh
Provides: gnome-www-browser, www-browser
Breaks: firefox-l10n-en-us
Description: The Firefox web browser
The Mozilla Firefox Web Browser.
$ dpkg -s nedit
dpkg-query: package 'nedit' is not installed and no information is available
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.
因此,您可以使用该命令来检查软件包是否已安装:
#!/usr/bin/env bash
run_install()
{
## Prompt the user
read -p "Do you want to install missing libraries? [Y/n]: " answer
## Set the default value if no answer was given
answer=${answer:Y}
## If the answer matches y or Y, install
[[ $answer =~ [Yy] ]] && apt-get install ${boostlibnames[@]}
}
boostlibnames=("libboost-serialization1.55.0" "libboost-thread1.55.0"
"libboost-date-time1.55.0" "libboost-signals1.55.0" "nedit")
## Run the run_install function if sany of the libraries are missing
dpkg -s "${boostlibnames[@]}" >/dev/null 2>&1 || run_install
答案2
我在工作中使用以下代码,称为轻松猛击,这有助于在 Ubuntu 服务器上快速安装最流行的软件包。此代码将检查列出的软件包是否已安装。
#!/usr/bin/env bash
packages=("curl" "gnupg2" "ca-certificates" "lsb-release")
for pkg in ${packages[@]}; do
is_pkg_installed=$(dpkg-query -W --showformat='${Status}\n' ${pkg} | grep "install ok installed")
if [ "${is_pkg_installed}" == "install ok installed" ]; then
echo ${pkg} is installed.
fi
done
结果:
答案3
下面将为您提供所有相关软件包的列表不是安装:
dpkg -l $boostlibnames 2>&1 | awk '{if (/^D|^\||^\+/) {next} else if(/^dpkg-query:/) { print $6} else if(!/^[hi]i/) {print $2}}'
这会跳过dpkg -l
的标题行,然后打印 dpkg-query 抱怨未知包的行,以及行不是hi
以或开头ii
(Hold Inst 或 Install Inst)。
通常,我会做类似awk 'NR<=5 {next} ; ...'
or 的事情sed -e '1,5d'
来删除 的dpkg -l
标题行,但在这种情况下,我们将 stderr 重定向到 stdout,以便捕获dpkg-query
以及dpkg -l
的输出的投诉,因此我们不能简单地删除第一个5行。
这将显示从未安装、删除、清除的软件包,或者由于错误而安装失败/部分完成的软件包。
答案4
#to check package is installed or not without distribution dependency
#!/bin/bash
read -p "Package Name: " pkg
which $pkg > /dev/null 2>&1
if [ $? == 0 ]
then
echo "$pkg is already installed. "
else
read -p "$pkg is not installed. Answer yes/no if want installation_ " request
if [ $request == "yes" ]
then
yum install $pkg
fi
fi