How to know if there are updates available?

Solution 1:

Read the man pages for motd(5), pam_motd(8) and update-motd(5). On my system, /etc/update-motd.d/90-updates-available invokes /usr/lib/update-notifier/update-motd-updates-available which displays this when I log in:

19 packages can be updated.
12 updates are security updates.

Delving a little deeper, the "...-updates-available" script invokes /usr/lib/update-notifier/apt-check --human-readable. If you read that (python), you'll see that if you omit the human readable flag, it will output "19;12" to stderr. We can grab that with this:

IFS=';' read updates security_updates < <(/usr/lib/update-notifier/apt-check 2>&1)
echo $updates
echo $security_updates 
19
12

Now you can say:

if (( updates == 0 )); then
    echo "No updates are available"
else
    echo "There are updates available"
fi