Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.
share|improve this question
    
See also Exiting a pipeline if a former command fails , if you want to execute a filter program only if there is non-empty output. –  Gilles Mar 6 '13 at 23:09

3 Answers 3

up vote 4 down vote accepted

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

share|improve this answer
    
Pipe doesn't pipe stderr, only stdout. You need to add 2>&1 before the | to redirect the stderr to the stdout. –  Carpetsmoker 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? –  daveboden 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. –  daveboden 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.

#!/bin/bash

TMPFILE=/tmp/allout.$$
TMPPIPE=/tmp/errout.$$
SAVERC=/tmp/saverc.$$

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)
share|improve this answer
    
Doesn't clean up after itself, won't work if executed twice (in parallel, or even sequential). –  Carpetsmoker 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 $?
1
$ [ $(echo -n '' 2>&1 | wc -c) = "0" ]
$ echo $?
0

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

share|improve this answer
    
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. –  Carpetsmoker Mar 6 '13 at 12:05
    
So, why exactly is this downvoted? Did I miss something, because it seems like a good solution to me... Please enlighten me :-) –  Carpetsmoker Feb 7 at 5:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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