如何通过精确名称查找已安装的软件包(如果存在)?

如何通过精确名称查找已安装的软件包(如果存在)?

我想检查是否已安装某个软件包。我需要精确的匹配。

会发现以nginx开头的包

$ dpkg -l | grep -w 'nginx'
ii  nginx                                1.18.0-0ubuntu1                   all          small, powerful, scalable web/proxy server
ii  nginx-common                         1.18.0-0ubuntu1                   all          small, powerful, scalable web/proxy server - common files
ii  nginx-core                           1.18.0-0ubuntu1                   amd64        nginx web/proxy server (standard version)

$ dpkg -l | grep -w 'nginx$'

$ dpkg -l | grep -w "nginx"
ii  nginx                                1.18.0-0ubuntu1                   all          small, powerful, scalable web/proxy server
ii  nginx-common                         1.18.0-0ubuntu1                   all          small, powerful, scalable web/proxy server - common files
ii  nginx-core                           1.18.0-0ubuntu1                   amd64        nginx web/proxy server (standard version)

$ dpkg -l | grep -w "\<nginx\>"
ii  nginx                                1.18.0-0ubuntu1                   all          small, powerful, scalable web/proxy server
ii  nginx-common                         1.18.0-0ubuntu1                   all          small, powerful, scalable web/proxy server - common files
ii  nginx-core                           1.18.0-0ubuntu1                   amd64        nginx web/proxy server (standard version)

$ dpkg -l | grep  "\<nginx\>"
ii  nginx                                1.18.0-0ubuntu1                   all          small, powerful, scalable web/proxy server
ii  nginx-common                         1.18.0-0ubuntu1                   all          small, powerful, scalable web/proxy server - common files
ii  nginx-core                           1.18.0-0ubuntu1                   amd64        nginx web/proxy server (standard version)

而我只想要 nginx 。如何修复它?

答案1

正如中所解释的dpkg 中存在一个软件包,但无法通过 apt 安装, 使用dpkg-query

dpkg-query -W -f '${db:Status-Status}' nginx

nginx当且仅当该软件包且仅该软件包完全安装时,才会显示“已安装” 。

如果dpkg-query不适合您,请将包指定为参数dpkg -l

dpkg -l nginx

答案2

通过过滤 的输出dpkg -l

dpkg -l | awk /^ii/'{print $2}' | grep -w nginx$

与 ( ) 的结果相同sudo apt install dctrl-tools

grep-status -FStatus -sPackage -n  "install ok installed" |grep -w  nginx$

相关内容