ObjectiveUse shell script to check whether a given packages is available/installed on Ubuntu or Debian Linux system.
Operating System and Software Versions
- Operating System: - Ubuntu, Debian
- # - requires given linux commands to be executed with root privileges either directly as a root user or by use of
- $ - requires given linux commands to be executed as a regular non-privileged user
Shell ScriptThe following script will check whether a package supplied to the below script via command line argument is installed on the system.
The script first uses
dpkgto check whether package is installed. Depending whether the
dpkgcommand executes successfully the script will print a package installation status to standard output.
#!/bin/bash dpkg -s $1 &> /dev/null if [ $? -eq 0 ]; then echo "Package is installed!" else echo "Package is NOT installed!" fi
UsageSave the above script to eg.
is_installed.shfile and make it executable:
$ chmod +x is_installed.shNext, use the script to check for installed package while supplying the package name as an argument. For example:
$ ./check_package.sh vim Package vim is installed! $ ./check_package.sh nginx Package nginx is NOT installed!