检查是否所有列出的软件包都安装在bash中


9

我的二进制文件取决于这些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会自动检查软件包是否已安装,但是我想在提示用户时先提示他是否要安装缺少的库。

那么,有没有一种很好的方法来检查是否已安装所有列出的库(具有上述变量)?


创建一个合适的Debian软件包并让其处理依赖关系
ivanivan '18

@ivanivan这是公司内部的应用程序,当然不是开源的。
托马什Zato -恢复莫妮卡

并不意味着您必须在内部公司之外分发deb文件或其他任何东西...只需使用打包系统依赖关系解决方案即可。哎呀,用这个q / a上的方法创建一个伪造的软件包来安装deps-unix.stackexchange.com/questions/318117/…–
ivanivan

Answers:


13

dpkg -s命令返回已安装软件包的状态。例如,在我的系统上,如果我运行firefox已安装和nedit未安装的系统,则会得到:

$ dpkg -s firefox
Package: firefox
Status: install ok installed
Priority: optional
Section: web
Installed-Size: 94341
Maintainer: Clement Lefebvre <root@linuxmint.com>
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

有一些保存结果的简便方法吗?例如。在C / C ++中,我愿意installed = installed || isInstalled(libArray[i])。同样,如果未安装一个或多个变量,我想在变量中添加false。我想我可以用做到这一点if,但这是很多代码而已。
托马什Zato -恢复莫妮卡

@TomášZato是的,但是您必须更加具体。您要保存缺少的库列表吗?仅够一个吗?就我个人而言,我只是检查是否已全部安装,如果至少没有安装,请在apt-get install所有它们上运行。安装的所有内容都将被忽略。让我知道您想要什么,我将对其进行编辑。基本上,您需要将更|| echo ...改为|| installed=1或。
terdon

我想完全按照自己的意愿去做,这是浪费时间来保存丢失的库的列表。只需检查所有内容,如果缺少任何内容,则提示用户输入apt-get。我已经让您的代码使用丑陋的if语句工作了,但是会使用变量。我打算将其包装成一个函数。
托马什Zato -恢复莫妮卡

@TomášZato好,看看更新后的答案。我简化了一下,因为dpkg -s可以一次检查多个软件包。
terdon

1

我今天为CentOS系统执行了此功能。

#!/bin/bash

# List of the packages that should be present
list=("vim-enhanced" "nano" "expect" "dialog" "epel-release" "yum-utils" "bind-utils")

# Check for the existence of the packages in the system and print to file.txt the packages to be installed
check_list=$(rpm -q "${list[@]}" | grep -e "not installed" | awk 'BEGIN { FS = " " } ; { print $2}' > list.txt)

# Check if the list.txt is empty
grep -q '[^[:space:]]' < list.txt
EMPTY_FILE=$?
# If list.txt is empty there's nothing to do
if [[ $EMPTY_FILE -eq 1 ]]; then

echo "Nothing to do"

else

# If list.txt is not empty it installs the packages in list.txt

for PACKAGES in `cat /your/path/list.txt`; do

  yum install -y $PACKAGES

done

fi

好吧,我稍微修改了功能。更快,它不再需要循环。

#!/bin/bash


list=("vim-enhanced" "nano" "expect" "dialog" "epel-release" "yum-utils" "bind-utils")
check_list=$(rpm -q "${list[@]}" | grep -e "not installed" | awk 'BEGIN { FS = " " } ; { printf $2" "}' > /your/path/to/list.txt)
install=$(cat /your/path/to/list.txt)

grep -q '[^[:space:]]' < /your/path/to/list.txt

EMPTY_FILE=$?

if [[ $EMPTY_FILE -eq 1 ]]; then

echo "Nothing to do"

else

yum install -y $install

fi

Debian / Ubuntu对应项:

apt -qq YOUR_LIST_GOES_HERE | grep -v "installed" | awk -F/ '{print $1}' > /your/path/to/list.txt
packages=$(cat /your/path/to/list.txt)
grep -q '[^[:space:]]' < /your/path/to/list.txt
EMPTY_FILE=$?
if [[ $EMPTY_FILE -eq 1 ]]; then
echo "Nothing to do"
else
apt-get  install -y $packages
fi

0

以下将为您列出所有安装的相关软件包:

dpkg -l $boostlibnames 2>&1 | awk '{if (/^D|^\||^\+/) {next} else if(/^dpkg-query:/) { print $6} else if(!/^[hi]i/) {print $2}}' 

此跳过dpkg -l的标题行,然后打印出线条的dpkg哪里查询抱怨一个未知的包,行开头hiii(按住研究所或安装研究所)。

通常,我会做类似awk 'NR<=5 {next} ; ...'sed -e '1,5d'摆脱dpkg -l的标题行的操作,但是在这种情况下,我们会将stderr重定向到stdout,以便捕获投诉dpkg-query以及dpkg -l的输出,因此我们不能简单地删除第一个5行。

这将显示从未安装,未删除,已清除或由于错误导致安装失败/部分完成的软件包。


0

我在名为Easy Bash的工作中使用以下代码,该代码有助于在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

结果:

在此处输入图片说明


欢迎来到该网站,并感谢您的贡献。不过,请避免发布控制台输出的屏幕截图,而应使用正确的格式对其进行复制和
粘贴

-1
#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

问题在于检查是否已安装单个软件包。
guntbert
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.