bash check if package is installed 1

bash check if package is installed

REQUIRED_PKG="some-package"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
  echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
  sudo apt-get --yes install $REQUIRED_PKG 
fi

Here is what the above code is Doing:
1. It’s checking if the package is installed.
2. If it’s not installed, it’s installing it.
3. If it is installed, it’s doing nothing.

Similar Posts