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

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.

That's grep you're looking for:

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

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)).

  • Pipe doesn't pipe stderr, only stdout. You need to add 2>&1 before the | to redirect the stderr to the stdout. – Martin Tournoij Mar 6 '13 at 12:08
  • @Carpetsmoker, alright, I had missed that part of the requirements. Edited now. – Stéphane Chazelas Mar 6 '13 at 12:13
  • I like the idea of grep '^'. However, it returns 0 = success when it does find output and 1 = error when it doesn't :) I need that to be the other way round. Is there a further command which can flip / invert the return code? – David B Mar 8 '13 at 10:47
  • 1
    Use !. ! svn | grep. Or svn | { ! grep; } – Stéphane Chazelas Mar 8 '13 at 12:09
  • Perfect. Marked as the answer with my thanks. – David B Mar 12 '13 at 22:22

I don't know of one existing command... is roll-your-own not acceptable? A wrapper? tee the output to a file and exit based on whether the file is empty of not?

Wrapper approach, assuming you still want to see the output and keep stdout and stderr separate in the output.



cleanup() {
   [ -p $TMPPIPE ] && rm $TMPPIPE
   [ -f $TMPFILE ] && rm $TMPFILE
trap cleanup EXIT

# Set up output/display of std err
[ -p $TMPPIPE ] || mkfifo $TMPPIPE
cat $TMPPIPE | tee -a $TMPFILE1 >&2 &

(eval "$*" 2>$TMPPIPE
echo $? > $SAVERC
) | tee -a $TMPFILE

[ -s $TMPFILE ] && exit 1
exit $(cat $SAVERC)
  • Doesn't clean up after itself, won't work if executed twice (in parallel, or even sequential). – Martin Tournoij Mar 6 '13 at 11:28
  • Indeed, I will update it. – Johan Mar 6 '13 at 11:58
  • The "Wrapper" will save the return code from the command. If that is not the desired effect, you can replace the last command with "exit 0" to indicate "No output detected" – Johan Mar 6 '13 at 12:10

You can use wc to count the characters in the output.

$ [ $(ls 2>&1 | wc -c) = "0" ]
$ echo $?
$ [ $(echo -n '' 2>&1 | wc -c) = "0" ]
$ echo $?

The 2>&1 is required to redirect the stderr to stdout.

  • I don't see that most programs produce a newline character unless they intentionally print something. – Johan Mar 6 '13 at 12:01
  • Thanks, you're right, I'm not sure why I thought that... Probably echo's auto-newline that confused me. – Martin Tournoij Mar 6 '13 at 12:05
  • 1
    So, why exactly is this downvoted? Did I miss something, because it seems like a good solution to me... Please enlighten me :-) – Martin Tournoij Feb 7 '14 at 5:46
  • 1
    Good idea, +1 making justice against non-voters that didn't comment. – jperelli Apr 13 '15 at 13:25
  • I guess one problem is that it eats the output, so if you want to see the output that caused the failure you can't. – Malvineous Dec 4 '18 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.