0

I try to make a script for macOS that informs me about available software updates:

result=$(softwareupdate -l)
if [[ $(echo $result) = *"No new software available."* ]]; then say yes;fi

But the important line isn't included. Result:

result=$(softwareupdate -l)
softwareupdate -l
++ softwareupdate -l

No new software available.
+ result='Software Update Tool
Finding available software'

if [[ $(echo $result) = *"No new software available."* ]]; then say yes;fi
echo $result
++ echo Software Update Tool Finding available software
+ [[ Software Update Tool Finding available software = *\N\o\ \n\e\w\ \s\o\f\t\w\a\r\e\ \a\v\a\i\l\a\b\l\e\.* ]]

The line: "No new software available." is not included in the variable. Why?

1

1 Answer 1

-1

Because the output is on STDERR, the error file descriptor output.

What I would do, to combine STDOUT and STDERR in bash:

if softwareupdate -l 2>&1 | grep -q "No new software available"; then
    echo "Nothing to do"
else
    echo "Update available"
fi

If you want to keep most of your code and use [[ ]] with string match, use:

result=$(softwareupdate -l 2>&1)
if [[ $result == *"No new software available"* ]]; then
    [...]

You can use |& instead of 2>&1 | with bash 4.0+. The default bash on macOS is too old to support |&, so you need to use 2>&1 |, or install a newer bash, e.g. using Homebrew:

brew install bash

This code use boolean logic:

you can test directly the grep command or other commands, because they return 0 for true or > 0 for false.


STDERR:

This is the secondary file descriptor to which commands print to the terminal. It's meant for messages that shouldn't be parsed by default. Redirect to stderr: >&2

STDIN:

Standard Input is the file descriptor from which commands can read input.

STDOUT

This is the primary file descriptor to which commands print to the terminal.

11
  • The result is wrong because the string "No new software available." is not included in the return of "softwareupdate -l" I don't know where this string comes from.
    – Atalantia
    Sep 18 at 4:21
  • Can you test again ? Post edited. Please refresh. Sep 18 at 4:25
  • ...softwareupdate -l |& grep -q ... returns an error and ...softwareupdate -l |>& grep -q ... returns a wrong result again.
    – Atalantia
    Sep 18 at 4:44
  • 1
    Maybe: softwareupdate -l 2>&1 | grep -q "No new software available" Sep 18 at 4:56
  • 1
    The bash version that comes standard with the Mac is a really old 3.2 release. You can install the latest release 5.2 via homebrew
    – rr0ss0rr
    Sep 18 at 13:11

You must log in to answer this question.

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