Standard/canonical way to test whether foregoing pipeline produced output?
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
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.
shell-script zsh pipe
|
show 3 more comments
up vote
4
down vote
favorite
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.
shell-script zsh pipe
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 ofgrep
?
– 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
|
show 3 more comments
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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.
shell-script zsh pipe
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.
shell-script zsh pipe
shell-script zsh pipe
edited Apr 13 '17 at 12:36
Community♦
1
1
asked Apr 13 '16 at 14:31
kjo
3,97993662
3,97993662
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 ofgrep
?
– 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
|
show 3 more comments
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 ofgrep
?
– 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
2
2
What's hacky about
| grep -q .
? O.o– muru
Apr 13 '16 at 14:47
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: 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
@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
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
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
|
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
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.
This also report no output whenread
false.
– cuonglm
Apr 13 '16 at 14:53
How do you trigger that?
– Jeff Schaller
Apr 13 '16 at 14:56
@cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
– Jeff Schaller
Apr 14 '16 at 15:13
Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
– cuonglm
Apr 14 '16 at 15:19
Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe otherread
implementations have other error situations.
– Jeff Schaller
Apr 14 '16 at 15:43
add a comment |
up vote
-1
down vote
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
$
New contributor
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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.
This also report no output whenread
false.
– cuonglm
Apr 13 '16 at 14:53
How do you trigger that?
– Jeff Schaller
Apr 13 '16 at 14:56
@cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
– Jeff Schaller
Apr 14 '16 at 15:13
Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
– cuonglm
Apr 14 '16 at 15:19
Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe otherread
implementations have other error situations.
– Jeff Schaller
Apr 14 '16 at 15:43
add a comment |
up vote
2
down vote
accepted
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.
This also report no output whenread
false.
– cuonglm
Apr 13 '16 at 14:53
How do you trigger that?
– Jeff Schaller
Apr 13 '16 at 14:56
@cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
– Jeff Schaller
Apr 14 '16 at 15:13
Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
– cuonglm
Apr 14 '16 at 15:19
Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe otherread
implementations have other error situations.
– Jeff Schaller
Apr 14 '16 at 15:43
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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.
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.
edited Apr 14 '16 at 15:33
answered Apr 13 '16 at 14:47
Jeff Schaller
36k952119
36k952119
This also report no output whenread
false.
– cuonglm
Apr 13 '16 at 14:53
How do you trigger that?
– Jeff Schaller
Apr 13 '16 at 14:56
@cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
– Jeff Schaller
Apr 14 '16 at 15:13
Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
– cuonglm
Apr 14 '16 at 15:19
Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe otherread
implementations have other error situations.
– Jeff Schaller
Apr 14 '16 at 15:43
add a comment |
This also report no output whenread
false.
– cuonglm
Apr 13 '16 at 14:53
How do you trigger that?
– Jeff Schaller
Apr 13 '16 at 14:56
@cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
– Jeff Schaller
Apr 14 '16 at 15:13
Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
– cuonglm
Apr 14 '16 at 15:19
Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe otherread
implementations have other error situations.
– Jeff Schaller
Apr 14 '16 at 15:43
This also report no output when
read
false.– cuonglm
Apr 13 '16 at 14:53
This also report no output when
read
false.– cuonglm
Apr 13 '16 at 14:53
How do you trigger that?
– Jeff Schaller
Apr 13 '16 at 14:56
How do you trigger that?
– Jeff Schaller
Apr 13 '16 at 14:56
@cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
– Jeff Schaller
Apr 14 '16 at 15:13
@cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
– Jeff Schaller
Apr 14 '16 at 15:13
Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
– cuonglm
Apr 14 '16 at 15:19
Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
– cuonglm
Apr 14 '16 at 15:19
Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other
read
implementations have other error situations.– Jeff Schaller
Apr 14 '16 at 15:43
Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other
read
implementations have other error situations.– Jeff Schaller
Apr 14 '16 at 15:43
add a comment |
up vote
-1
down vote
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
$
New contributor
add a comment |
up vote
-1
down vote
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
$
New contributor
add a comment |
up vote
-1
down vote
up vote
-1
down vote
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
$
New contributor
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
$
New contributor
New contributor
answered 2 days ago
keypress
991
991
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f276199%2fstandard-canonical-way-to-test-whether-foregoing-pipeline-produced-output%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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