display STDOUTs before STDERR?

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











up vote
9
down vote

favorite
3












I'm new to bash and I can not for the life of me figure out how to run a certain command, suppose ./fff and print regular stdouts before stderr (I'm confused about the meaning myself)



for example



$ printf "I am andrilln" > fff; 
$ cat fff nofile fff nofile fff

I am a
drill
cat: nofile: No such file or directory
I am a
drill
cat: nofile: No such file or directory
I am a
drill


needs to print like:



I am a
drill
I am a
drill
I am a
drill
cat: nofile: No such file or directory
cat: nofile: No such file or directory


I understand that I need to redirect my output first to a file and then append the error to the same file however this is the output I get for



$ cat ./foo nofile ./foo nofile ./foo <<< $(touch fin) > see 2>> see 

I am a
drill
I am a
drill
I am a
drill
ectory
cat: nofile: No such file or directory






share|improve this question


















  • 2




    Did cat really replace "a" by "some"?
    – Dmitry Grigoryev
    Jan 15 at 13:06










  • oh dang I might have messed up the string. I'll edit it right away @DmitryGrigoryev
    – MeOw
    Jan 15 at 15:44














up vote
9
down vote

favorite
3












I'm new to bash and I can not for the life of me figure out how to run a certain command, suppose ./fff and print regular stdouts before stderr (I'm confused about the meaning myself)



for example



$ printf "I am andrilln" > fff; 
$ cat fff nofile fff nofile fff

I am a
drill
cat: nofile: No such file or directory
I am a
drill
cat: nofile: No such file or directory
I am a
drill


needs to print like:



I am a
drill
I am a
drill
I am a
drill
cat: nofile: No such file or directory
cat: nofile: No such file or directory


I understand that I need to redirect my output first to a file and then append the error to the same file however this is the output I get for



$ cat ./foo nofile ./foo nofile ./foo <<< $(touch fin) > see 2>> see 

I am a
drill
I am a
drill
I am a
drill
ectory
cat: nofile: No such file or directory






share|improve this question


















  • 2




    Did cat really replace "a" by "some"?
    – Dmitry Grigoryev
    Jan 15 at 13:06










  • oh dang I might have messed up the string. I'll edit it right away @DmitryGrigoryev
    – MeOw
    Jan 15 at 15:44












up vote
9
down vote

favorite
3









up vote
9
down vote

favorite
3






3





I'm new to bash and I can not for the life of me figure out how to run a certain command, suppose ./fff and print regular stdouts before stderr (I'm confused about the meaning myself)



for example



$ printf "I am andrilln" > fff; 
$ cat fff nofile fff nofile fff

I am a
drill
cat: nofile: No such file or directory
I am a
drill
cat: nofile: No such file or directory
I am a
drill


needs to print like:



I am a
drill
I am a
drill
I am a
drill
cat: nofile: No such file or directory
cat: nofile: No such file or directory


I understand that I need to redirect my output first to a file and then append the error to the same file however this is the output I get for



$ cat ./foo nofile ./foo nofile ./foo <<< $(touch fin) > see 2>> see 

I am a
drill
I am a
drill
I am a
drill
ectory
cat: nofile: No such file or directory






share|improve this question














I'm new to bash and I can not for the life of me figure out how to run a certain command, suppose ./fff and print regular stdouts before stderr (I'm confused about the meaning myself)



for example



$ printf "I am andrilln" > fff; 
$ cat fff nofile fff nofile fff

I am a
drill
cat: nofile: No such file or directory
I am a
drill
cat: nofile: No such file or directory
I am a
drill


needs to print like:



I am a
drill
I am a
drill
I am a
drill
cat: nofile: No such file or directory
cat: nofile: No such file or directory


I understand that I need to redirect my output first to a file and then append the error to the same file however this is the output I get for



$ cat ./foo nofile ./foo nofile ./foo <<< $(touch fin) > see 2>> see 

I am a
drill
I am a
drill
I am a
drill
ectory
cat: nofile: No such file or directory








share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 15:44

























asked Jan 14 at 22:10









MeOw

535




535







  • 2




    Did cat really replace "a" by "some"?
    – Dmitry Grigoryev
    Jan 15 at 13:06










  • oh dang I might have messed up the string. I'll edit it right away @DmitryGrigoryev
    – MeOw
    Jan 15 at 15:44












  • 2




    Did cat really replace "a" by "some"?
    – Dmitry Grigoryev
    Jan 15 at 13:06










  • oh dang I might have messed up the string. I'll edit it right away @DmitryGrigoryev
    – MeOw
    Jan 15 at 15:44







2




2




Did cat really replace "a" by "some"?
– Dmitry Grigoryev
Jan 15 at 13:06




Did cat really replace "a" by "some"?
– Dmitry Grigoryev
Jan 15 at 13:06












oh dang I might have messed up the string. I'll edit it right away @DmitryGrigoryev
– MeOw
Jan 15 at 15:44




oh dang I might have messed up the string. I'll edit it right away @DmitryGrigoryev
– MeOw
Jan 15 at 15:44










1 Answer
1






active

oldest

votes

















up vote
18
down vote



accepted










You'll need to hold the stderr output somewhere anyway to be able to display it at the end.



A file comes to mind:



fff 2> file; cat file >&2


Or memory (here using sponge from moreutils):



 sponge >&2 3>&-; 3>&1



  • ... 3>&1: within ... file descriptor (fd) 3 points to the same resource as the original stdout (so we can use it to restore stdout for fff).


  • fff <redirs> | sponge <redirs>, fff and sponge started concurrently (with <redirs> applied independently) with fff's stdout going to a pipe, and sponge's stdin being the other end of the pipe.


  • 2>&1: fd 2 of fff (stderr) points to the same thing as on 1: the pipe at this point, so fff's error go to sponge via that pipe.


  • >&3: now stdout points to the original stdout (redirect back to what it was)


  • 3>&-: we close fd 2 which fff doesn't need


  • sponge accumulates its input and only displays it (on its stdout which has been redirected with >&2 to the same resource as stderr) after it has detected eof on its stdin (assumed to be when fff terminates and has already written all its output on its stdout).

If sponge is not installed, you can replace it with perl -0777 -pe ''. With -pe '', perl reads one record at a time from its input and writes it on stdout. -0777 is the slurp mode where the (only one in that case) record is the whole input.






share|improve this answer






















  • Please kindly break it down if you have the time!
    – George Udosen
    Jan 14 at 22:20










  • Can we use tee instead of sponge ...?
    – George Vasiliou
    Jan 14 at 22:44










  • @GeorgeUdosen see edit.
    – Stéphane Chazelas
    Jan 14 at 22:45






  • 1




    @GeorgeVasiliou, tee doesn't hold data, it writes it as soon as it reads it.
    – Stéphane Chazelas
    Jan 14 at 22:46






  • 2




    @MeOw: That’s fine, but, as the third line of Stéphane’s answer shows, you don’t need to save the stdout; just do cat foo nofile foo nofile foo 2> ferr.txt; cat ferr.txt.  (And you probably don t want to use >>.)  Also, Stéphane makes the excellent point that you should probably do cat ferr.txt >&2 to write the stderr information to the stderr.
    – G-Man
    Jan 15 at 5:04










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%2f417124%2fdisplay-stdouts-before-stderr%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
18
down vote



accepted










You'll need to hold the stderr output somewhere anyway to be able to display it at the end.



A file comes to mind:



fff 2> file; cat file >&2


Or memory (here using sponge from moreutils):



 sponge >&2 3>&-; 3>&1



  • ... 3>&1: within ... file descriptor (fd) 3 points to the same resource as the original stdout (so we can use it to restore stdout for fff).


  • fff <redirs> | sponge <redirs>, fff and sponge started concurrently (with <redirs> applied independently) with fff's stdout going to a pipe, and sponge's stdin being the other end of the pipe.


  • 2>&1: fd 2 of fff (stderr) points to the same thing as on 1: the pipe at this point, so fff's error go to sponge via that pipe.


  • >&3: now stdout points to the original stdout (redirect back to what it was)


  • 3>&-: we close fd 2 which fff doesn't need


  • sponge accumulates its input and only displays it (on its stdout which has been redirected with >&2 to the same resource as stderr) after it has detected eof on its stdin (assumed to be when fff terminates and has already written all its output on its stdout).

If sponge is not installed, you can replace it with perl -0777 -pe ''. With -pe '', perl reads one record at a time from its input and writes it on stdout. -0777 is the slurp mode where the (only one in that case) record is the whole input.






share|improve this answer






















  • Please kindly break it down if you have the time!
    – George Udosen
    Jan 14 at 22:20










  • Can we use tee instead of sponge ...?
    – George Vasiliou
    Jan 14 at 22:44










  • @GeorgeUdosen see edit.
    – Stéphane Chazelas
    Jan 14 at 22:45






  • 1




    @GeorgeVasiliou, tee doesn't hold data, it writes it as soon as it reads it.
    – Stéphane Chazelas
    Jan 14 at 22:46






  • 2




    @MeOw: That’s fine, but, as the third line of Stéphane’s answer shows, you don’t need to save the stdout; just do cat foo nofile foo nofile foo 2> ferr.txt; cat ferr.txt.  (And you probably don t want to use >>.)  Also, Stéphane makes the excellent point that you should probably do cat ferr.txt >&2 to write the stderr information to the stderr.
    – G-Man
    Jan 15 at 5:04














up vote
18
down vote



accepted










You'll need to hold the stderr output somewhere anyway to be able to display it at the end.



A file comes to mind:



fff 2> file; cat file >&2


Or memory (here using sponge from moreutils):



 sponge >&2 3>&-; 3>&1



  • ... 3>&1: within ... file descriptor (fd) 3 points to the same resource as the original stdout (so we can use it to restore stdout for fff).


  • fff <redirs> | sponge <redirs>, fff and sponge started concurrently (with <redirs> applied independently) with fff's stdout going to a pipe, and sponge's stdin being the other end of the pipe.


  • 2>&1: fd 2 of fff (stderr) points to the same thing as on 1: the pipe at this point, so fff's error go to sponge via that pipe.


  • >&3: now stdout points to the original stdout (redirect back to what it was)


  • 3>&-: we close fd 2 which fff doesn't need


  • sponge accumulates its input and only displays it (on its stdout which has been redirected with >&2 to the same resource as stderr) after it has detected eof on its stdin (assumed to be when fff terminates and has already written all its output on its stdout).

If sponge is not installed, you can replace it with perl -0777 -pe ''. With -pe '', perl reads one record at a time from its input and writes it on stdout. -0777 is the slurp mode where the (only one in that case) record is the whole input.






share|improve this answer






















  • Please kindly break it down if you have the time!
    – George Udosen
    Jan 14 at 22:20










  • Can we use tee instead of sponge ...?
    – George Vasiliou
    Jan 14 at 22:44










  • @GeorgeUdosen see edit.
    – Stéphane Chazelas
    Jan 14 at 22:45






  • 1




    @GeorgeVasiliou, tee doesn't hold data, it writes it as soon as it reads it.
    – Stéphane Chazelas
    Jan 14 at 22:46






  • 2




    @MeOw: That’s fine, but, as the third line of Stéphane’s answer shows, you don’t need to save the stdout; just do cat foo nofile foo nofile foo 2> ferr.txt; cat ferr.txt.  (And you probably don t want to use >>.)  Also, Stéphane makes the excellent point that you should probably do cat ferr.txt >&2 to write the stderr information to the stderr.
    – G-Man
    Jan 15 at 5:04












up vote
18
down vote



accepted







up vote
18
down vote



accepted






You'll need to hold the stderr output somewhere anyway to be able to display it at the end.



A file comes to mind:



fff 2> file; cat file >&2


Or memory (here using sponge from moreutils):



 sponge >&2 3>&-; 3>&1



  • ... 3>&1: within ... file descriptor (fd) 3 points to the same resource as the original stdout (so we can use it to restore stdout for fff).


  • fff <redirs> | sponge <redirs>, fff and sponge started concurrently (with <redirs> applied independently) with fff's stdout going to a pipe, and sponge's stdin being the other end of the pipe.


  • 2>&1: fd 2 of fff (stderr) points to the same thing as on 1: the pipe at this point, so fff's error go to sponge via that pipe.


  • >&3: now stdout points to the original stdout (redirect back to what it was)


  • 3>&-: we close fd 2 which fff doesn't need


  • sponge accumulates its input and only displays it (on its stdout which has been redirected with >&2 to the same resource as stderr) after it has detected eof on its stdin (assumed to be when fff terminates and has already written all its output on its stdout).

If sponge is not installed, you can replace it with perl -0777 -pe ''. With -pe '', perl reads one record at a time from its input and writes it on stdout. -0777 is the slurp mode where the (only one in that case) record is the whole input.






share|improve this answer














You'll need to hold the stderr output somewhere anyway to be able to display it at the end.



A file comes to mind:



fff 2> file; cat file >&2


Or memory (here using sponge from moreutils):



 sponge >&2 3>&-; 3>&1



  • ... 3>&1: within ... file descriptor (fd) 3 points to the same resource as the original stdout (so we can use it to restore stdout for fff).


  • fff <redirs> | sponge <redirs>, fff and sponge started concurrently (with <redirs> applied independently) with fff's stdout going to a pipe, and sponge's stdin being the other end of the pipe.


  • 2>&1: fd 2 of fff (stderr) points to the same thing as on 1: the pipe at this point, so fff's error go to sponge via that pipe.


  • >&3: now stdout points to the original stdout (redirect back to what it was)


  • 3>&-: we close fd 2 which fff doesn't need


  • sponge accumulates its input and only displays it (on its stdout which has been redirected with >&2 to the same resource as stderr) after it has detected eof on its stdin (assumed to be when fff terminates and has already written all its output on its stdout).

If sponge is not installed, you can replace it with perl -0777 -pe ''. With -pe '', perl reads one record at a time from its input and writes it on stdout. -0777 is the slurp mode where the (only one in that case) record is the whole input.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 15 at 7:36

























answered Jan 14 at 22:15









Stéphane Chazelas

281k53518849




281k53518849











  • Please kindly break it down if you have the time!
    – George Udosen
    Jan 14 at 22:20










  • Can we use tee instead of sponge ...?
    – George Vasiliou
    Jan 14 at 22:44










  • @GeorgeUdosen see edit.
    – Stéphane Chazelas
    Jan 14 at 22:45






  • 1




    @GeorgeVasiliou, tee doesn't hold data, it writes it as soon as it reads it.
    – Stéphane Chazelas
    Jan 14 at 22:46






  • 2




    @MeOw: That’s fine, but, as the third line of Stéphane’s answer shows, you don’t need to save the stdout; just do cat foo nofile foo nofile foo 2> ferr.txt; cat ferr.txt.  (And you probably don t want to use >>.)  Also, Stéphane makes the excellent point that you should probably do cat ferr.txt >&2 to write the stderr information to the stderr.
    – G-Man
    Jan 15 at 5:04
















  • Please kindly break it down if you have the time!
    – George Udosen
    Jan 14 at 22:20










  • Can we use tee instead of sponge ...?
    – George Vasiliou
    Jan 14 at 22:44










  • @GeorgeUdosen see edit.
    – Stéphane Chazelas
    Jan 14 at 22:45






  • 1




    @GeorgeVasiliou, tee doesn't hold data, it writes it as soon as it reads it.
    – Stéphane Chazelas
    Jan 14 at 22:46






  • 2




    @MeOw: That’s fine, but, as the third line of Stéphane’s answer shows, you don’t need to save the stdout; just do cat foo nofile foo nofile foo 2> ferr.txt; cat ferr.txt.  (And you probably don t want to use >>.)  Also, Stéphane makes the excellent point that you should probably do cat ferr.txt >&2 to write the stderr information to the stderr.
    – G-Man
    Jan 15 at 5:04















Please kindly break it down if you have the time!
– George Udosen
Jan 14 at 22:20




Please kindly break it down if you have the time!
– George Udosen
Jan 14 at 22:20












Can we use tee instead of sponge ...?
– George Vasiliou
Jan 14 at 22:44




Can we use tee instead of sponge ...?
– George Vasiliou
Jan 14 at 22:44












@GeorgeUdosen see edit.
– Stéphane Chazelas
Jan 14 at 22:45




@GeorgeUdosen see edit.
– Stéphane Chazelas
Jan 14 at 22:45




1




1




@GeorgeVasiliou, tee doesn't hold data, it writes it as soon as it reads it.
– Stéphane Chazelas
Jan 14 at 22:46




@GeorgeVasiliou, tee doesn't hold data, it writes it as soon as it reads it.
– Stéphane Chazelas
Jan 14 at 22:46




2




2




@MeOw: That’s fine, but, as the third line of Stéphane’s answer shows, you don’t need to save the stdout; just do cat foo nofile foo nofile foo 2> ferr.txt; cat ferr.txt.  (And you probably don t want to use >>.)  Also, Stéphane makes the excellent point that you should probably do cat ferr.txt >&2 to write the stderr information to the stderr.
– G-Man
Jan 15 at 5:04




@MeOw: That’s fine, but, as the third line of Stéphane’s answer shows, you don’t need to save the stdout; just do cat foo nofile foo nofile foo 2> ferr.txt; cat ferr.txt.  (And you probably don t want to use >>.)  Also, Stéphane makes the excellent point that you should probably do cat ferr.txt >&2 to write the stderr information to the stderr.
– G-Man
Jan 15 at 5:04












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417124%2fdisplay-stdouts-before-stderr%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