如何知道是否有可用更新?

Ade*_*ine 8 updates php apt bash

我正在运行 12.04 LTS ubuntu 服务器。而且我认为如果可以在更新可用时通知我会很好。但是我不知道怎么知道...

我试过查看apt-get手册页。从它我能够使用apt-get -s upgrade在脚本中获取 apt-get 输出而不会阻止问题。

现在,我清楚地看到了差异:

更新可用:

apt-get -s upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be upgraded:
  dpkg dpkg-dev libdpkg-perl
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Inst dpkg [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [amd64])
Conf dpkg (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [amd64])
Inst dpkg-dev [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all]) []
Inst libdpkg-perl [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])
Conf libdpkg-perl (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])
Conf dpkg-dev (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])
Run Code Online (Sandbox Code Playgroud)

更新不可用:

apt-get -s upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Run Code Online (Sandbox Code Playgroud)

但我不知道如何从那里开始。如何从 bash 脚本(或 php 脚本)判断是否有可用更新?

编辑 :

这是我当前的 bash 代码。这是行不通的。

updates_available=`/etc/update-motd.d/90-updates-available`

if [ "${updates_available}" = "0 packages can be updated. 0 updates are security updates." ];
then
   echo "No updates are available"
else
   echo "There are updates available"
fi
Run Code Online (Sandbox Code Playgroud)

gle*_*man 18

阅读motd(5), pam_motd(8)和 的手册页 update-motd(5)。在我的系统上,/etc/update-motd.d/90-updates-available调用/usr/lib/update-notifier/update-motd-updates-available它在我登录时显示:

19 packages can be updated.
12 updates are security updates.
Run Code Online (Sandbox Code Playgroud)

再深入一点,“...-updates-available”脚本调用/usr/lib/update-notifier/apt-check --human-readable. 如果你读过那个(python),你会发现如果你省略了人类可读的标志,它会输出“19;12”到标准错误。我们可以用这个来抓住它:

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

现在你可以说:

if (( updates == 0 )); then
    echo "No updates are available"
else
    echo "There are updates available"
fi
Run Code Online (Sandbox Code Playgroud)