Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm trying to write a script for ubuntu 10.0.4 to check for an application on my system. I want the script to see if the particular named application is installed, and if it is, if it needs updating. I also want the script to automatically install the app if it isn't installed, or update the application if it is installed. Like if i needed to install WINE or GIMP. I'm not sure how to structure the commands but I know I will use either apt-get, or yum to find the application.

share|improve this question
add comment

1 Answer 1

You can combine these two pieces:

dpkg-query -W -f '${status} ${package} ${version}\n' |
sed -n 's/^install ok installed //p'

This will show the names of all installed packages, including their versions. If you don't need version numbers, omit the ${version} part.

Then, run apt-get update to get updated package lists, and:

/usr/lib/update-notifier/apt-check  -p

This will tell you the names of packages for which updates are available.

An idea would be (don't take this verbatim, I'm just providing an idea):

#!/bin/bash

$first_command > installed_packages
$second_command > packages_with_updates

if (grep $YOUR_PACKAGE installed_packages); then
   echo "Package is installed"
   if (grep $YOUR_PACKAGE packages_with_updates); then
      sudo apt-get -q -q update #apt-get runs in quiet mode to avoid lots of output
      echo "Update available for package"
      #Whatever you need to update the package here
   fi
else
   echo "Package not installed, installing..."
   #whatever you need to do to install here
fi
share|improve this answer
    
It's a great start, and thank you... but how would you configure it to run a check like this for a single named package that can changed each time you run the script? If i check for vlc this time, and run it again to check for wine? –  J.R. May 4 '12 at 21:15
    
something like YOUR_PACKAGE=$1 at the beginning. Then call it with update-package wine for wine, and update-package vlc for vlc. $1, $2, ... mean the first, second and following command line parameters. –  roadmr May 4 '12 at 22:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.