From fac01628a07cf8083f91d9924ab8d63a9d4141db Mon Sep 17 00:00:00 2001 From: Florian Beer Date: Wed, 3 Aug 2016 04:51:35 +0200 Subject: [PATCH] Add Debian and make update call more robust - Debian based systems need to update the index before being able to report upgradable packages. - Debian old-stable doesn't have `apt` yet and Ubuntu 14.04 emits the following warning when using `apt` in a script: `WARNING: /usr/bin/apt does not have a stable CLI interface yet. Use with caution in scripts.` By using `apt-get`, issuing a `update` call first and then counting the result of `grep 'Inst'`, this script now works on Debian 7, Debian 8, Ubuntu 14.04 and Ubuntu 16.04. --- snmp/os-updates.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/snmp/os-updates.sh b/snmp/os-updates.sh index 092ccf7..cd8201c 100755 --- a/snmp/os-updates.sh +++ b/snmp/os-updates.sh @@ -12,8 +12,9 @@ BIN_ZYPPER='/usr/bin/zypper' CMD_ZYPPER='lu' BIN_YUM='/usr/bin/yum' CMD_YUM='check-update' -BIN_APT='/usr/bin/apt' -CMD_APT='list --upgradable' +BIN_APT='/usr/bin/apt-get' +CMD_APT='-s upgrade' +CMD_UPDATE='-qq update' #general check for os based on /etc/os-release if [ -f /etc/os-release ]; then @@ -32,10 +33,11 @@ if [ -f /etc/os-release ]; then else echo "0"; fi - elif [ $OS == "ubuntu" ]; then - UPDATES=`$BIN_APT $CMD_APT | $BIN_WC $CMD_WC` + elif [ $OS == "ubuntu" ] || [ $OS == "debian" ]; then + `$BIN_APT $CMD_UPDATE` + UPDATES=`$BIN_APT $CMD_APT | grep 'Inst' | $BIN_WC $CMD_WC` if [ $UPDATES -gt 1 ]; then - echo $(($UPDATES-1)); + echo $UPDATES; else echo "0"; fi