Return 1 if command output is empty without capturing it

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I have a shell script which ends with a few pipes grep ... | while read ...| sort | uniq and I want to return 1 if the output is empty, but uniq always returns 0 even if its input is empty.



So far the best solution I've found is adding grep with an empty pattern ... uniq | grep '', this works perfectly but feels more like a hack.



So my question is: Is there any better/canonical way to do this?



Some restrictions:



  • I don't want to capture my output in a variable since I would need to print it again afterwards: a=$(... | uniq); printf '%sn' "$a"; [ -n "$a" ] which doesn't feel right either.

  • If possible I'd also prefer a standard tool (no moreutils) and something portable

Thanks!







share|improve this question




















  • Storing it and printing it is unavoidable, even if you use a program like tee. While some programs make it look like it does not store it and print it later, that is what you have to do to check the output of a command and then running it
    – Stan Strum
    Mar 24 at 18:15










  • In these cases I tend to use wc -l and check the output but this is not better than your approach.
    – Ned64
    Mar 24 at 18:17










  • yeah store and print later is indeed probably what uniq and sort are doing, but the less I do it the better
    – Quentin L'Hours
    Mar 24 at 18:20










  • What about a function like: muniq () uniq ?
    – Jesse_b
    Mar 24 at 18:27










  • @Jesse_b yes it's easier to read but it's still a hack
    – Quentin L'Hours
    Mar 24 at 18:29














up vote
2
down vote

favorite












I have a shell script which ends with a few pipes grep ... | while read ...| sort | uniq and I want to return 1 if the output is empty, but uniq always returns 0 even if its input is empty.



So far the best solution I've found is adding grep with an empty pattern ... uniq | grep '', this works perfectly but feels more like a hack.



So my question is: Is there any better/canonical way to do this?



Some restrictions:



  • I don't want to capture my output in a variable since I would need to print it again afterwards: a=$(... | uniq); printf '%sn' "$a"; [ -n "$a" ] which doesn't feel right either.

  • If possible I'd also prefer a standard tool (no moreutils) and something portable

Thanks!







share|improve this question




















  • Storing it and printing it is unavoidable, even if you use a program like tee. While some programs make it look like it does not store it and print it later, that is what you have to do to check the output of a command and then running it
    – Stan Strum
    Mar 24 at 18:15










  • In these cases I tend to use wc -l and check the output but this is not better than your approach.
    – Ned64
    Mar 24 at 18:17










  • yeah store and print later is indeed probably what uniq and sort are doing, but the less I do it the better
    – Quentin L'Hours
    Mar 24 at 18:20










  • What about a function like: muniq () uniq ?
    – Jesse_b
    Mar 24 at 18:27










  • @Jesse_b yes it's easier to read but it's still a hack
    – Quentin L'Hours
    Mar 24 at 18:29












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a shell script which ends with a few pipes grep ... | while read ...| sort | uniq and I want to return 1 if the output is empty, but uniq always returns 0 even if its input is empty.



So far the best solution I've found is adding grep with an empty pattern ... uniq | grep '', this works perfectly but feels more like a hack.



So my question is: Is there any better/canonical way to do this?



Some restrictions:



  • I don't want to capture my output in a variable since I would need to print it again afterwards: a=$(... | uniq); printf '%sn' "$a"; [ -n "$a" ] which doesn't feel right either.

  • If possible I'd also prefer a standard tool (no moreutils) and something portable

Thanks!







share|improve this question












I have a shell script which ends with a few pipes grep ... | while read ...| sort | uniq and I want to return 1 if the output is empty, but uniq always returns 0 even if its input is empty.



So far the best solution I've found is adding grep with an empty pattern ... uniq | grep '', this works perfectly but feels more like a hack.



So my question is: Is there any better/canonical way to do this?



Some restrictions:



  • I don't want to capture my output in a variable since I would need to print it again afterwards: a=$(... | uniq); printf '%sn' "$a"; [ -n "$a" ] which doesn't feel right either.

  • If possible I'd also prefer a standard tool (no moreutils) and something portable

Thanks!









share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 18:10









Quentin L'Hours

132




132











  • Storing it and printing it is unavoidable, even if you use a program like tee. While some programs make it look like it does not store it and print it later, that is what you have to do to check the output of a command and then running it
    – Stan Strum
    Mar 24 at 18:15










  • In these cases I tend to use wc -l and check the output but this is not better than your approach.
    – Ned64
    Mar 24 at 18:17










  • yeah store and print later is indeed probably what uniq and sort are doing, but the less I do it the better
    – Quentin L'Hours
    Mar 24 at 18:20










  • What about a function like: muniq () uniq ?
    – Jesse_b
    Mar 24 at 18:27










  • @Jesse_b yes it's easier to read but it's still a hack
    – Quentin L'Hours
    Mar 24 at 18:29
















  • Storing it and printing it is unavoidable, even if you use a program like tee. While some programs make it look like it does not store it and print it later, that is what you have to do to check the output of a command and then running it
    – Stan Strum
    Mar 24 at 18:15










  • In these cases I tend to use wc -l and check the output but this is not better than your approach.
    – Ned64
    Mar 24 at 18:17










  • yeah store and print later is indeed probably what uniq and sort are doing, but the less I do it the better
    – Quentin L'Hours
    Mar 24 at 18:20










  • What about a function like: muniq () uniq ?
    – Jesse_b
    Mar 24 at 18:27










  • @Jesse_b yes it's easier to read but it's still a hack
    – Quentin L'Hours
    Mar 24 at 18:29















Storing it and printing it is unavoidable, even if you use a program like tee. While some programs make it look like it does not store it and print it later, that is what you have to do to check the output of a command and then running it
– Stan Strum
Mar 24 at 18:15




Storing it and printing it is unavoidable, even if you use a program like tee. While some programs make it look like it does not store it and print it later, that is what you have to do to check the output of a command and then running it
– Stan Strum
Mar 24 at 18:15












In these cases I tend to use wc -l and check the output but this is not better than your approach.
– Ned64
Mar 24 at 18:17




In these cases I tend to use wc -l and check the output but this is not better than your approach.
– Ned64
Mar 24 at 18:17












yeah store and print later is indeed probably what uniq and sort are doing, but the less I do it the better
– Quentin L'Hours
Mar 24 at 18:20




yeah store and print later is indeed probably what uniq and sort are doing, but the less I do it the better
– Quentin L'Hours
Mar 24 at 18:20












What about a function like: muniq () uniq ?
– Jesse_b
Mar 24 at 18:27




What about a function like: muniq () uniq ?
– Jesse_b
Mar 24 at 18:27












@Jesse_b yes it's easier to read but it's still a hack
– Quentin L'Hours
Mar 24 at 18:29




@Jesse_b yes it's easier to read but it's still a hack
– Quentin L'Hours
Mar 24 at 18:29










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Frankly, I think | grep '' is just fine. You've already used a bunch of programs in the pipeline, so forking out another one isn't going to hurt. And you already know what grep does, so there's no debugging with regard to writing some "smart" shell snippet to do it.



The comments mentioned also | grep ., which might be another possibility. grep . doesn't match on an empty line, grep '' does, so choose depending on which one you want.



Though, using grep like that might not be immediately obvious to a casual reader, so you might want to add a comment about its purpose in any case.






share|improve this answer




















  • Yeah I guess I'll stick with this, too bad there's nothing more obvious than grep.
    – Quentin L'Hours
    Mar 24 at 21:29

















up vote
0
down vote













I think this should do the work:



[ -n "$(command | tee /dev/tty)" ]


tee will send command's output to both stdout (where it's evaluated by the test command) and your terminal (/dev/tty), so you can see the output without capturing it in a variable.



It's a POSIX-compliant solution AFAIK.






share|improve this answer






















  • and that only works if the output from the script is going to a tty, and not redirected somewhere else... also the comsub will eat tailing newlines, making an output that contains just empty lines seem like there was no output
    – ilkkachu
    Mar 24 at 19:39











  • Thanks, it would work in my case but I feel this is too much compared to just using grep, even if that's not grep's primary purpose.
    – Quentin L'Hours
    Mar 24 at 21:27










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433295%2freturn-1-if-command-output-is-empty-without-capturing-it%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Frankly, I think | grep '' is just fine. You've already used a bunch of programs in the pipeline, so forking out another one isn't going to hurt. And you already know what grep does, so there's no debugging with regard to writing some "smart" shell snippet to do it.



The comments mentioned also | grep ., which might be another possibility. grep . doesn't match on an empty line, grep '' does, so choose depending on which one you want.



Though, using grep like that might not be immediately obvious to a casual reader, so you might want to add a comment about its purpose in any case.






share|improve this answer




















  • Yeah I guess I'll stick with this, too bad there's nothing more obvious than grep.
    – Quentin L'Hours
    Mar 24 at 21:29














up vote
0
down vote



accepted










Frankly, I think | grep '' is just fine. You've already used a bunch of programs in the pipeline, so forking out another one isn't going to hurt. And you already know what grep does, so there's no debugging with regard to writing some "smart" shell snippet to do it.



The comments mentioned also | grep ., which might be another possibility. grep . doesn't match on an empty line, grep '' does, so choose depending on which one you want.



Though, using grep like that might not be immediately obvious to a casual reader, so you might want to add a comment about its purpose in any case.






share|improve this answer




















  • Yeah I guess I'll stick with this, too bad there's nothing more obvious than grep.
    – Quentin L'Hours
    Mar 24 at 21:29












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Frankly, I think | grep '' is just fine. You've already used a bunch of programs in the pipeline, so forking out another one isn't going to hurt. And you already know what grep does, so there's no debugging with regard to writing some "smart" shell snippet to do it.



The comments mentioned also | grep ., which might be another possibility. grep . doesn't match on an empty line, grep '' does, so choose depending on which one you want.



Though, using grep like that might not be immediately obvious to a casual reader, so you might want to add a comment about its purpose in any case.






share|improve this answer












Frankly, I think | grep '' is just fine. You've already used a bunch of programs in the pipeline, so forking out another one isn't going to hurt. And you already know what grep does, so there's no debugging with regard to writing some "smart" shell snippet to do it.



The comments mentioned also | grep ., which might be another possibility. grep . doesn't match on an empty line, grep '' does, so choose depending on which one you want.



Though, using grep like that might not be immediately obvious to a casual reader, so you might want to add a comment about its purpose in any case.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 19:45









ilkkachu

48.9k671136




48.9k671136











  • Yeah I guess I'll stick with this, too bad there's nothing more obvious than grep.
    – Quentin L'Hours
    Mar 24 at 21:29
















  • Yeah I guess I'll stick with this, too bad there's nothing more obvious than grep.
    – Quentin L'Hours
    Mar 24 at 21:29















Yeah I guess I'll stick with this, too bad there's nothing more obvious than grep.
– Quentin L'Hours
Mar 24 at 21:29




Yeah I guess I'll stick with this, too bad there's nothing more obvious than grep.
– Quentin L'Hours
Mar 24 at 21:29












up vote
0
down vote













I think this should do the work:



[ -n "$(command | tee /dev/tty)" ]


tee will send command's output to both stdout (where it's evaluated by the test command) and your terminal (/dev/tty), so you can see the output without capturing it in a variable.



It's a POSIX-compliant solution AFAIK.






share|improve this answer






















  • and that only works if the output from the script is going to a tty, and not redirected somewhere else... also the comsub will eat tailing newlines, making an output that contains just empty lines seem like there was no output
    – ilkkachu
    Mar 24 at 19:39











  • Thanks, it would work in my case but I feel this is too much compared to just using grep, even if that's not grep's primary purpose.
    – Quentin L'Hours
    Mar 24 at 21:27














up vote
0
down vote













I think this should do the work:



[ -n "$(command | tee /dev/tty)" ]


tee will send command's output to both stdout (where it's evaluated by the test command) and your terminal (/dev/tty), so you can see the output without capturing it in a variable.



It's a POSIX-compliant solution AFAIK.






share|improve this answer






















  • and that only works if the output from the script is going to a tty, and not redirected somewhere else... also the comsub will eat tailing newlines, making an output that contains just empty lines seem like there was no output
    – ilkkachu
    Mar 24 at 19:39











  • Thanks, it would work in my case but I feel this is too much compared to just using grep, even if that's not grep's primary purpose.
    – Quentin L'Hours
    Mar 24 at 21:27












up vote
0
down vote










up vote
0
down vote









I think this should do the work:



[ -n "$(command | tee /dev/tty)" ]


tee will send command's output to both stdout (where it's evaluated by the test command) and your terminal (/dev/tty), so you can see the output without capturing it in a variable.



It's a POSIX-compliant solution AFAIK.






share|improve this answer














I think this should do the work:



[ -n "$(command | tee /dev/tty)" ]


tee will send command's output to both stdout (where it's evaluated by the test command) and your terminal (/dev/tty), so you can see the output without capturing it in a variable.



It's a POSIX-compliant solution AFAIK.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 24 at 19:10

























answered Mar 24 at 18:33









nxnev

2,4522423




2,4522423











  • and that only works if the output from the script is going to a tty, and not redirected somewhere else... also the comsub will eat tailing newlines, making an output that contains just empty lines seem like there was no output
    – ilkkachu
    Mar 24 at 19:39











  • Thanks, it would work in my case but I feel this is too much compared to just using grep, even if that's not grep's primary purpose.
    – Quentin L'Hours
    Mar 24 at 21:27
















  • and that only works if the output from the script is going to a tty, and not redirected somewhere else... also the comsub will eat tailing newlines, making an output that contains just empty lines seem like there was no output
    – ilkkachu
    Mar 24 at 19:39











  • Thanks, it would work in my case but I feel this is too much compared to just using grep, even if that's not grep's primary purpose.
    – Quentin L'Hours
    Mar 24 at 21:27















and that only works if the output from the script is going to a tty, and not redirected somewhere else... also the comsub will eat tailing newlines, making an output that contains just empty lines seem like there was no output
– ilkkachu
Mar 24 at 19:39





and that only works if the output from the script is going to a tty, and not redirected somewhere else... also the comsub will eat tailing newlines, making an output that contains just empty lines seem like there was no output
– ilkkachu
Mar 24 at 19:39













Thanks, it would work in my case but I feel this is too much compared to just using grep, even if that's not grep's primary purpose.
– Quentin L'Hours
Mar 24 at 21:27




Thanks, it would work in my case but I feel this is too much compared to just using grep, even if that's not grep's primary purpose.
– Quentin L'Hours
Mar 24 at 21:27












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433295%2freturn-1-if-command-output-is-empty-without-capturing-it%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay