I want to write a bash script allowing me to check, whether a certain package is already installed in arch linux.
How can I do that?
The dpkg-query command can be used to show if a specific package is installed in your system. To do it, run dpkg-query followed by the -l flag and the name of the package you want information about.
You should use Pacman, the package manager of Arch Linux.
You want to use the -Q
operation to query the installed local package database and the -i
option to get information on the package.
This gives you
pacman -Qi <packageName>
You can then use the exit code to determine if the packages existes on the system or not (0 the package exists, 1 it doesn't)
The usage of -i
rather than -s
ensures you will check for exactly that package and not for the presence of a a package containing the package name in its name.
For example if I search for chromium
(the web browser) on a system where only chromium-bsu
(the game) is installed,
# This exits with 1 because chromium is not installed pacman -Qi chromium # This exits with 0 because chromium-bsu is installed pacman -Qs chromium
As Random Citizen pointed out, you certainly want to redirect any output to /dev/null
if you are writing a script and don't want Pacman to pollute your output:
pacman -Qi <packageName> > /dev/null
You can use the arch package management tool pacman.
As you can see in the Arch-Wiki, the -Qs
option can be used to search within the installed packages.
If the package exists, pacman -Qs
will exit with the exit-code 0, otherwise with the exit-code 1
You script might look like:
package=firefox if pacman -Qs $package > /dev/null ; then echo "The package $package is installed" else echo "The package $package is not installed" fi
The > /dev/null
pipe is used to suppress the printed output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With