softwareupdate grep Script Help

Using High Sierra I have this set as a cronjob to run once a day.

I've been struggling to work out how to get grep to find the string in the output. Am fairly new to bash scripting.

Any advice on where I went wrong, and examples are appreciated!

#!/bin/bash

printf '\e[2t'

check=$(softwareupdate -l)

sleep 5

echo $check


if [ fgrep "No new software" <<< $check ]
then
say "Peter, You are up to date"
else
say "Peter, you have updates"
fi

Solution 1:

softwareupdate prints No new software available. on stderr, not on stdout, so it will never get assigned to check. The quick fix is to run

check=$(softwareupdate -l 2>&1)

This will redirect stderr (file descriptor 2) to stdout (file descriptor 1).

Also your if statement is wrong, the [ is a command, not just syntax. So you can simply run

if fgrep "No new software" <<< $check; then

for this.

But if you don't need the value of $check later on you can put all this in one line and just run

if softwareupdate -l 2>&1 | fgrep -q "No new software"; then