Shell

檢查是否所有列出的軟體包都安裝在 bash 中

  • April 23, 2020

我的二進製文件依賴於這些 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 會自動檢查軟體包是否已經安裝,但我想在使用者安裝之前提示他是否要安裝缺少的庫。

那麼有沒有一種很好的方法來檢查是否安裝了所有列出的庫(如上所述的變數)?

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

引用自:https://unix.stackexchange.com/questions/235605