Shell – Linux command line that will fail if any standard out is produced

shell-scripttext processing

Is there a simple utility which I can pipe output to on Linux that will:

  • Return a success code if there is no output on standard out (and / or standard error).
  • Return a failure code if output is produced on standard out (and / or standard error).

To provide some context, the command I'm running is:

svn mergeinfo --show-revs eligible
http://mysvnserver.example.com/SVF/repos/common/abc/branches/abc-1.7
http://mysvnserver.example.com/SVF/repos/common/abc/trunk

If there are any unmerged entries on the branch, the command will return a list of revision numbers on standard out. Ideally, the additional command that I'm talking about would:

  • Detect entries on standard out and return an error condition to Linux.
  • Pass on the standard out so that it does end up appearing on the terminal. I'd rather not suppress it.

Best Answer

That's grep you're looking for:

if svn ... 2>&1 | grep '^'; then
  echo "there was some output"
else
  echo "there wasn't"
fi

You can replace grep '^' with grep . or grep '[^[:blank:]]' to check for non-empty or non-blank lines (but that will remove the empty/blank ones from the output).

(note the behaviour will vary across grep implementations if the input contains non-text data like NUL bytes or too long or non-terminated lines (which wouldn't happen for svn though)).

Related Question