I am writing a script that checks for a very big list of dependencies. So I wrote a function that gets the package name as input and using dpkg
checks if it is installed and prints an appropriate output. Here is one version of this function:
check_dependency() {
dpkg -l $1 > /dev/null
if [ $? -eq 0 ]; then
# print something saying it is installed
else
# print something saying it was not found
fi
}
which works fine when package is installed. But when it is not installed, it somehow doesn't redirect the output to /dev/null
and prompts:
dpkg-query: no packages found matching foo-bar
In a second attempt, I modifed the body of function to use it with grep -q
:
check_dependency() {
dpkg -s $1 | grep -q 'installed'
if [ $? -eq 0 ]; then
# print something saying it is installed
else
# print something saying it was not found
fi
}
When the package is installed, again it seems to work fine, however, when package is not found and unwanted message from dpkg
appears on terminal:
dpkg-query: package 'foo-bar' is not installed and no information is available bla bla...
Using directly dpkg-query
results in similar problem. How to make it silent when it fails to find package?
stderr
) rather than the standard output stream (stdout
) - see Redirect stdout and stderr so that it is displayed or stored nowhere