0

I need a bash line to check if a ubuntu package needs an upgrade or not.

For example, I want to check if the package 'firefox' needs an upgrade using dpkg or apt-get commands.

Hypthetical Example:

# Hypothetical example pseudo-code
if [[ $(firefox_needs_upgrade) ]]; then echo "Firefox needs upgrading";fi
Basil A
  • 1,910
  • 2
  • 17
  • 18

4 Answers4

2
##Simulate an upgrade and grep for the firefox package
##Feel free to replace ' firefox' with something more specific

RET=apt-get -s upgrade | grep firefox
if [ $ret -eq 0 ]
then
    echo "I don't have access to a debian box right now"
fi

From: https://unix.stackexchange.com/questions/19470/list-available-updates-but-do-not-install-them

Daniel Widrick
  • 3,418
  • 2
  • 12
  • 26
  • Doesn't work correctly. What if there is a package named "firefox-utils" – Basil A Apr 07 '16 at 13:02
  • I don't have access to a debian machine... "##Feel free to replace ' firefox' with something more specific" You may have to do some leg work on your own. – Daniel Widrick Apr 08 '16 at 17:54
  • No, I meant if you provide "firefox", it might also match a package named "firefox-utils" (hypothetically). In a case where "firefox" package is up-to-date but firefox-utils needs upgrading, this will give a false positive. – Basil A Apr 08 '16 at 21:49
0

You can check version of programs installed under apt with command:

 dpkg -l

You can check a current package by referencing the Ubuntu Packages Search. Alternatively, for any package under apt (assuming that you have the updates repository enabled), you can use the apt-get command to update your package information and check for upgrades:

 apt-get update
 apt-get dist-upgrade

If you are not logged in as root (which is generally to be avoided, you may have to use sudo to have sufficient privileges):

 sudo apt-get update
 sudo apt-get dist-upgrade

Check the apt-get man page for more options.

Finally, you can search (or subscribe to) Ubuntu security notices to keep yourself notified real-time of needed updates.

Colt
  • 1,939
  • 6
  • 20
  • 25
  • So in bash, how can I check if firefox is up-to-date or not using dpkg? – Basil A Apr 06 '16 at 10:14
  • I'm sorry, I didn't read that exactly right. In any case (whether automatic updating is a good idea or not), here is the [Ubuntu documentation](https://help.ubuntu.com/community/AutomaticSecurityUpdates) for doing this with the GNOME Update Manager, the unattended-upgrades package, by writing your own cron script that calls aptitude, or using cron-apt. – Colt Apr 06 '16 at 10:21
  • I'm looking to write an if condition in a bash script, eg. if [ $(firefox_needs_updating) ]; then echo 'You need to update firefox' – Basil A Apr 06 '16 at 12:24
0

The command your looking for is apt list --upgradable 2>/dev/null.

And using it in your example:

package_name=firefox
if [[ $(apt list --upgradable 2>/dev/null  | grep $package_name) ]]; then echo "${package_name} needs upgrading";fi

Note: 2>/dev/null is used to suppress the warning message from apt

alejdg
  • 176
  • 1
  • 6
-2

Try to use apticron

apt-get install apticron

After installation configure it here:

/etc/apticron/apticron.conf

It will give you a daily notification on mail if anything need to be updated.

Fiil
  • 101
  • 2