It seems odd that the function prints 0
in all cases, including when 1
has been printed.
For a utility like this, it's probably better to communicate via the exit status:
isInstalled() {
! dpkg-query -W -f'${Status}' "$1" 2>/dev/null | grep -q "ok installed" \
|| dpkg-query -W -f'${Status}' "lib$1" 2>/dev/null | grep -q "ok installed"
}
I'd be inclined to remove the !
, returning zero (success) if the package is found.
If you have aptitude
available, it's possible to ask it to search for any installed package of the given name, or providing the name:
aptitude -q2 search "~i~P?exact-name($1)|~i?exact-name($1)" >/dev/null
~i
: installed package
~P
: provides name
|
: alternation
^$1\$
?exact-name
: exactly what it says
This is less brittle, as it doesn't rely on the providing package being formed by prepending lib
to the virtual package name. For example, with www-browser
, it finds all installed web browsers. Also, it avoids matching superstrings of the supplied name.
If you know that $1
will always be the name of a virtual package, you can of course use just the ~P
side of the alternation.
Provides
? So that a different package is installed than what was requested? \$\endgroup\$ – Toby Speight Dec 19 '17 at 14:49apt install gnutls-dev
it's installed but is listed bydpkg-query
aslibgnutls-dev
...gnutls-dev
is a virtual package, I guess. But either way, the code essentially does what it needs to for the sake of my project, just curious if there's a better way to do it. \$\endgroup\$ – user1960364 Dec 19 '17 at 14:55