dpkg-query
does have an option to do this, and its exit codes support your use case:
-s
, --status
package-name...
Report status of specified package. This just displays the entry in the installed package status
database. When multiple package-name
are listed, the requested status entries are separated by an
empty line, with the same order as specified on the argument list.
(note that it looks in the installed package status database, so it can't report on anything not installed — which is what you're after);
and
EXIT STATUS
0: The requested query was successfully performed.
1: The requested query failed either fully or partially, due to no file or package being found (except
for --control-path
, --control-list
and --control-show
were such errors are fatal).
2: Fatal or unrecoverable error due to invalid command-line usage, or interactions with the system, such
as accesses to the database, memory allocations, etc.
(The manpage included in Debian 8 doesn't mention this, but dpkg-query
does behave like that even in Debian 8.)
So something like
#!/bin/sh
dpkg-query -s package > /dev/null 2>&1
case $? in
0)
echo $1 is installed
;;
1)
echo $1 is not installed
;;
2)
echo An error occurred
;;
esac
(turned into a function) would fit the bill as I understand it.