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!


printf '\e[2t'

check=$(softwareupdate -l)

sleep 5

echo $check

if [ fgrep "No new software" <<< $check ]
say "Peter, You are up to date"
say "Peter, you have updates"
  • 1
    Do you need the content of $check later on in the script? – nohillside Mar 28 '20 at 11:36
  • All I want is that if there are any updates at all - it does a say to tell me, and if the software update -l comes back as empty it tells me theres nothing. Thanks :) – Pete Mar 28 '20 at 11:46

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
  • Thanks! Could you confirm what 2>&1 is doing? Does it just mean if it is > 1 character? – Pete Mar 28 '20 at 12:00
  • 1
    @Pete see edited answer – nohillside Mar 28 '20 at 12:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .