display STDOUTs before STDERR?
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
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
bash shell io-redirection stdout stderr
add a comment |Â
up vote
9
down vote
favorite
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
bash shell io-redirection stdout stderr
2
Didcat
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
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
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
bash shell io-redirection stdout stderr
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
bash shell io-redirection stdout stderr
edited Jan 15 at 15:44
asked Jan 14 at 22:10
MeOw
535
535
2
Didcat
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
add a comment |Â
2
Didcat
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
add a comment |Â
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 forfff
).fff <redirs> | sponge <redirs>
,fff
andsponge
started concurrently (with<redirs>
applied independently) withfff
's stdout going to a pipe, andsponge
's stdin being the other end of the pipe.2>&1
: fd 2 offff
(stderr) points to the same thing as on 1: the pipe at this point, sofff
's error go tosponge
via that pipe.>&3
: now stdout points to the original stdout (redirect back to what it was)3>&-
: we close fd 2 whichfff
doesn't needsponge
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 whenfff
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.
Please kindly break it down if you have the time!
â George Udosen
Jan 14 at 22:20
Can we usetee
instead ofsponge
...?
â 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 docat 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 docatâ¯ferr.txtâ¯>&2
to write the stderr information to the stderr.
â G-Man
Jan 15 at 5:04
 |Â
show 6 more comments
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 forfff
).fff <redirs> | sponge <redirs>
,fff
andsponge
started concurrently (with<redirs>
applied independently) withfff
's stdout going to a pipe, andsponge
's stdin being the other end of the pipe.2>&1
: fd 2 offff
(stderr) points to the same thing as on 1: the pipe at this point, sofff
's error go tosponge
via that pipe.>&3
: now stdout points to the original stdout (redirect back to what it was)3>&-
: we close fd 2 whichfff
doesn't needsponge
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 whenfff
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.
Please kindly break it down if you have the time!
â George Udosen
Jan 14 at 22:20
Can we usetee
instead ofsponge
...?
â 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 docat 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 docatâ¯ferr.txtâ¯>&2
to write the stderr information to the stderr.
â G-Man
Jan 15 at 5:04
 |Â
show 6 more comments
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 forfff
).fff <redirs> | sponge <redirs>
,fff
andsponge
started concurrently (with<redirs>
applied independently) withfff
's stdout going to a pipe, andsponge
's stdin being the other end of the pipe.2>&1
: fd 2 offff
(stderr) points to the same thing as on 1: the pipe at this point, sofff
's error go tosponge
via that pipe.>&3
: now stdout points to the original stdout (redirect back to what it was)3>&-
: we close fd 2 whichfff
doesn't needsponge
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 whenfff
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.
Please kindly break it down if you have the time!
â George Udosen
Jan 14 at 22:20
Can we usetee
instead ofsponge
...?
â 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 docat 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 docatâ¯ferr.txtâ¯>&2
to write the stderr information to the stderr.
â G-Man
Jan 15 at 5:04
 |Â
show 6 more comments
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 forfff
).fff <redirs> | sponge <redirs>
,fff
andsponge
started concurrently (with<redirs>
applied independently) withfff
's stdout going to a pipe, andsponge
's stdin being the other end of the pipe.2>&1
: fd 2 offff
(stderr) points to the same thing as on 1: the pipe at this point, sofff
's error go tosponge
via that pipe.>&3
: now stdout points to the original stdout (redirect back to what it was)3>&-
: we close fd 2 whichfff
doesn't needsponge
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 whenfff
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.
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 forfff
).fff <redirs> | sponge <redirs>
,fff
andsponge
started concurrently (with<redirs>
applied independently) withfff
's stdout going to a pipe, andsponge
's stdin being the other end of the pipe.2>&1
: fd 2 offff
(stderr) points to the same thing as on 1: the pipe at this point, sofff
's error go tosponge
via that pipe.>&3
: now stdout points to the original stdout (redirect back to what it was)3>&-
: we close fd 2 whichfff
doesn't needsponge
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 whenfff
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.
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 usetee
instead ofsponge
...?
â 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 docat 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 docatâ¯ferr.txtâ¯>&2
to write the stderr information to the stderr.
â G-Man
Jan 15 at 5:04
 |Â
show 6 more comments
Please kindly break it down if you have the time!
â George Udosen
Jan 14 at 22:20
Can we usetee
instead ofsponge
...?
â 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 docat 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 docatâ¯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
 |Â
show 6 more comments
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
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
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
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
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
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