Dpkg: check that unix package is installed. Bash script: install package if not present

By neokrates, written on May 21, 2010

howto

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 971 views
Rate it
Ad
Poll
  • Your favorite way to manage packages is?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

Was the package already installed or it still needs to be?

Software:

✔ Ubuntu *

dpkg -s can be used.

Example: is libasound installed?

> dpkg -s libasound2|grep installed

 
Here is some logic in bash script, like installing package if it is not there:

 problem=$(dpkg -s xulrunner-1.9|grep installed)
 echo Checking for libxul: $problem
 if [ "" == "$problem" ]; then
      echo "No libxul. Setting up libxul"
      sudo apt-get --force-yes --yes install xulrunner-1.9
 fi

Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +3 (from 5 votes)
4 votes 'YES'  1 votes 'NO'


TAGS

SOCIAL
Be Sociable, Share!

INCOMING SEARCH TERMS


8 Responses to “Dpkg: check that unix package is installed. Bash script: install package if not present”

  1. david says:

    after some debugging and testing i’ve reached this function
    <code>
    function dpkgstatins {

    echo "checking if $dpkgof is installed" 2>&1
    if [[ $(dpkg-query -f'${Status}' --show $dpkgof 2>/dev/null) = *\ installed ]]; then
    echo "$dpkgof found!"
    else echo "$dpkgof was not found, installing..." 2>&1
    apt-get --force-yes -yqq install $dpkgof 2>/dev/null
    fi
    

    }
    </code>
    this function needs $dpkgof defined as the package

  2. Grant Stone says:

    Isn’t your argument telling it to install the program if the program is already installed…? I think you mixed up the operators and want != instead of ==. To say if it’s NOT equal, to THEN install.
    And why did you put it as a variable? Couldn’t it be better written this way?
    echo “Checking for libxul…”
    if ! dpkg -s xulrunner-1.9 | grep installed
    then
    echo “No libxul. Setting up libxul…”
    sudo apt-get –force-yes –yes install xulrunner-1.9
    fi

    • neokrates says:

      problem=$(dpkg -s xulrunner-1.9|grep installed) will be “” if package is NOT installed, like:

      
      

      Package: xulrunner-1.9
      Essential: yes
      Status: install ok installed
      Priority: required

      Then, if package is NOT installed, we proceed with installation:

      
      

      if [ “” == “$problem” ]; then

      echo "No libxul. Setting up libxul"
      sudo apt-get --force-yes --yes install xulrunner-1.9
      

      fi

      I don’t think that your form is much better, I would rather say it is alternative.
      Both forms miss some readability I think. If it is harder to read, it s harder to maintain, especially by many peers.

      I would prefer to rename $problem to $is_installed and use my form, because it is easy to read then:

      is_installed=$(dpkg -s xulrunner-1.9|grep installed)

      echo Checking for libxul: $is_installed
      if [ "" == "$is_installed" ]; then
      echo "No libxul. Setting up libxul"
      sudo apt-get --force-yes --yes install xulrunner-1.9
      echo "/usr/lib/xulrunner-1.9/">>/etc/ld.so.conf
      ldconfig
      fi</em>
      

  3. zimbatm says:

    I was searching for this today and found the following solution before reaching your blog:

    if ! dpkg-query -l “your_package” | grep -r ‘^ ii’ 2>&1 > /dev/null ; then
    apt-get install -y “your_package”
    fi

    dpgk-query will return 1 if the package is not found, and grep will also return 1 if the package is not installed

  4. Peebrody says:

    must check for less

Leave a Reply