4

The situation I have in mind has the following structure:

% some_command | [PRODUCED OUTPUT] || echo 'no output' >&2

Here [PRODUCED OUTPUT] stands for some as-yet-unspecified testing command, whose value should be true (i.e. "success") iff some_command produces any output at all.

Of course, some_command stands for an arbitrarily complex pipeline, and likewise, echo 'no output' >&2 stands for some arbitrary action to perform in case some_command produces no output.

Is there a standard test to do the job represented above by [PRODUCED OUTPUT]? grep -qm1 '.' comes close but also reports false on an input consisting of empty lines.

Unlike Check if pipe is empty and run a command on the data if it isn't I just want to discard the input if it's present, I don't need to preserve it.

kjo
  • 15,339
  • 25
  • 73
  • 114
  • 2
    What's hacky about | grep -q .? O.o – muru Apr 13 '16 at 14:47
  • @muru: hackiness, of course, is in the eye of the beholder, but in my book at least, any use of a tool for a purpose entirely different from what it was intended for, especially when the tool in question is a powerful one, and the purpose it's being used is a trivial one, qualifies as "hacky". And yes, a ton of stuff would qualify as "hacky" by that definition, and a lot of other stuff many people would call hacky does not meet this definition... Nothing wrong with a hack, if that's all one can do... – kjo Apr 13 '16 at 14:59
  • @muru (cont'd) ...or if no one is looking, but for code that will go in a script, I avoid hacks as much as possible, if I want keep the script easy-to-understand by others. YMMV, it's all a matter of opinion. – kjo Apr 13 '16 at 15:00
  • Isn't testing whether input has something the intended purpose of grep? – muru Apr 13 '16 at 15:01
  • I'm assuming you don't care about stderr output? (you'd redirect it after some_command, before the pipeline, if you wanted it to be involved?) – Jeff Schaller Apr 13 '16 at 15:04
  • @JeffSchaller: correct. – kjo Apr 13 '16 at 15:08
  • Your grep command says no if its input consists entirely of newline characters. Is that desired behavior, or do you not care about the case when the input contains only empty lines? – Gilles 'SO- stop being evil' Apr 13 '16 at 22:22
  • @Gilles: that was a mistake on my part; empty lines should be considered output. (In an earlier version of this post I wrote grep -q '', which may have been better.) – kjo Apr 14 '16 at 10:14
  • 1
    ifne (moreutils): runs a given command if and only if the standard input is not empty – HappyFace Sep 20 '20 at 18:51

2 Answers2

3

How about using read?

$ cat /dev/null | read pointless || echo no output
no output
$ echo something | read pointless || echo no output
$ printf "\n" | read pointless || echo no output
$ printf " \n" | read pointless || echo no output
$ false | read pointless || echo no output
no output

According to the Open Group definition:

EXIT STATUS

The following exit values shall be returned:

0

Successful completion.

>0

End-of-file was detected or an error occurred.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
-1

For me worked great | read -n 1.

$ echo 'faw' | read -n 1 || echo 'no output'
$ echo | read -n 1 || echo 'no output'
$ printf '' | read -n 1 || echo 'no output'
no output
$ false | read -n 1 || echo 'no output'
no output
$
keypress
  • 109