Objective
Use 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
Difficulty
EASY
Conventions
- # – requires given linux commands to be executed with root privileges either directly as a root user or by use of
sudo
command - $ – requires given linux commands to be executed as a regular non-privileged user
Instructions
Shell Script
The 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 dpkg
to check whether package is installed. Depending whether the dpkg
command 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 $1 is installed!" else echo "Package $1 is NOT installed!" fi
Usage
Save the above script to eg. is_installed.sh
file and make it executable:
$ chmod +x is_installed.sh
Next, 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!