1

When I log in to the terminal/via SSH, I see this message, where x is a number of updates:

x packages can be updated.
x updates are security updates.

However, if the number to both is 0, I'd prefer not to see the message.

I've tried modifying the MOTD files but from what I can see I can either allow them to show, or hide them, but nothing conditional. The content of the 90-updates-available file is:

#!/bin/sh

stamp="/var/lib/update-notifier/updates-available"

[ ! -r "$stamp" ] || cat "$stamp"

...and the contents of /var/lib/update-notifier/updates-available is:

0 packages can be updated.
0 updates are security updates.

How can I modify the 90-updates-available file to prevent showing the message if both the messages start with 0?

Braiam
  • 67,791
  • 32
  • 179
  • 269
Ben
  • 211
  • 2
  • 10

3 Answers3

5

You can probably do something like:

if [ -r "$stamp" ] 
then
    awk '{c += $1; out = out "\n" $0} END {if (c != 0) print out}' /var/lib/update-notifier/updates-available
fi

This just takes the sum of the first field and prints the file if the sum is non-zero.

muru
  • 197,895
  • 55
  • 485
  • 740
  • This works (that I can see!). Would this only work if the first number is 0, or if both update numbers are 0? – Ben May 23 '16 at 11:05
  • @Ben awk operates line-wise, so it sums the first number on both lines. So, for the sum to be zero, both numbers have to be zero (or x and -x, which I hope won't happen). – muru May 23 '16 at 11:07
  • I see. Is this written in Bash? If so, can you recommend a source to learn it? Thanks! – Ben May 23 '16 at 11:10
  • 1
    The if condition is in shell, yes. The awk part is written in, well, awk. Here's a guide: http://www.grymoire.com/Unix/Awk.html – muru May 23 '16 at 11:11
1

You can change the line:

[ ! -r "$stamp" ] || cat "$stamp"

to

([ ! -r "$stamp" ] || [ -n "$(awk '/^0/{print $1;}' "$stamp")" ]) || cat "$stamp"

This will do it.

Videonauth
  • 33,355
  • 17
  • 105
  • 120
0

Here'a a variant using sed instead of awk that suppresses any line that starts with a count of 0, and joins both regular updates and security updates lines if both have counts:

[ ! -r "$stamp" ] || sed -e '/^0 /d' -e 'N; s/\n0 .*//; s/\n/ /' -- "$stamp"