Append string on redirectRedirecting stderr and stdout separately to a function in a bash scriptwhat is meant by connecting STDOUT and STDIN?Shell script doesn't paint last line of stdout to screen without user inputRedirect all subsequent commands' stderr using execWrite to stderrControlling the terminal while in the middle of a pipeRedirecting Standard Output And Standard Error to One FileHow can I prepending a character for every output of a command?Bash redirection: append to a file descriptor (2>>&1)How does this command work? mkfifo /tmp/f; cat /tmp/f | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/fHow to prefix any output in a bash script?
Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?
What is the purpose of a disclaimer like "this is not legal advice"?
What is the orbit and expected lifetime of Crew Dragon trunk?
Short story about cities being connected by a conveyor belt
How does a sound wave propagate?
Why aren't there more Gauls like Obelix?
I am the person who abides by rules but breaks the rules . Who am I
Paper published similar to PhD thesis
How to install "rounded" brake pads
Short story about an infectious indestructible metal bar?
Does an unused member variable take up memory?
Is there a logarithm base for which the logarithm becomes an identity function?
PTIJ: Sport in the Torah
An Undercover Army
How can I portion out frozen cookie dough?
How to recover against Snake as a heavyweight character?
Too soon for a plot twist?
Why do we say 'Pairwise Disjoint', rather than 'Disjoint'?
How can I have x-axis ticks that show ticks scaled in powers of ten?
Are small insurances worth it?
Issue with units for a rocket nozzle throat area problem
How spaceships determine each other's mass in space?
Is it a Cyclops number? "Nobody" knows!
What exactly is the meaning of "fine wine"?
Append string on redirect
Redirecting stderr and stdout separately to a function in a bash scriptwhat is meant by connecting STDOUT and STDIN?Shell script doesn't paint last line of stdout to screen without user inputRedirect all subsequent commands' stderr using execWrite to stderrControlling the terminal while in the middle of a pipeRedirecting Standard Output And Standard Error to One FileHow can I prepending a character for every output of a command?Bash redirection: append to a file descriptor (2>>&1)How does this command work? mkfifo /tmp/f; cat /tmp/f | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/fHow to prefix any output in a bash script?
When redirecting all output to stderr
stat file >&2
If the command throws an error (e.g. file does not exist) how can one prepend some arbitraty text e.g.
<<MY ARBITRARY TEXT>>: stat: cannot stat 'file': No such file or directory
The required logic is:
let res = stat file
if error then output "<<MY ARBITRARY TEXT>>"+error to stderr
else output res to stderr
More context: all command output is being redirected to stderr because stdout and stderr streams do always return in the right order, i.e. if multiple commands are sent and one throw an error, the stdout of the next command may return before the stderr of the original command. To avoid this I am merging all commands onto stderr, but ideally would like to be able to easily identify errors.
bash shell-script shell
New contributor
add a comment |
When redirecting all output to stderr
stat file >&2
If the command throws an error (e.g. file does not exist) how can one prepend some arbitraty text e.g.
<<MY ARBITRARY TEXT>>: stat: cannot stat 'file': No such file or directory
The required logic is:
let res = stat file
if error then output "<<MY ARBITRARY TEXT>>"+error to stderr
else output res to stderr
More context: all command output is being redirected to stderr because stdout and stderr streams do always return in the right order, i.e. if multiple commands are sent and one throw an error, the stdout of the next command may return before the stderr of the original command. To avoid this I am merging all commands onto stderr, but ideally would like to be able to easily identify errors.
bash shell-script shell
New contributor
"I am merging all commands onto stderr, but ideally would like to be able to easily identify errors." Well, there's not much you can do then except rely on exit status and manually parsing the text of the error message (which could be unportable and error-prone by itself). I'd rethink the whole logic behind this endeavor.
– Sergiy Kolodyazhnyy
59 mins ago
Related question: Redirecting stderr and stdout separately to a function in a bash script (probably not a duplicate, this one runs several commands in series)
– Michael Homer
34 mins ago
add a comment |
When redirecting all output to stderr
stat file >&2
If the command throws an error (e.g. file does not exist) how can one prepend some arbitraty text e.g.
<<MY ARBITRARY TEXT>>: stat: cannot stat 'file': No such file or directory
The required logic is:
let res = stat file
if error then output "<<MY ARBITRARY TEXT>>"+error to stderr
else output res to stderr
More context: all command output is being redirected to stderr because stdout and stderr streams do always return in the right order, i.e. if multiple commands are sent and one throw an error, the stdout of the next command may return before the stderr of the original command. To avoid this I am merging all commands onto stderr, but ideally would like to be able to easily identify errors.
bash shell-script shell
New contributor
When redirecting all output to stderr
stat file >&2
If the command throws an error (e.g. file does not exist) how can one prepend some arbitraty text e.g.
<<MY ARBITRARY TEXT>>: stat: cannot stat 'file': No such file or directory
The required logic is:
let res = stat file
if error then output "<<MY ARBITRARY TEXT>>"+error to stderr
else output res to stderr
More context: all command output is being redirected to stderr because stdout and stderr streams do always return in the right order, i.e. if multiple commands are sent and one throw an error, the stdout of the next command may return before the stderr of the original command. To avoid this I am merging all commands onto stderr, but ideally would like to be able to easily identify errors.
bash shell-script shell
bash shell-script shell
New contributor
New contributor
New contributor
asked 2 hours ago
TrevTheDevTrevTheDev
1134
1134
New contributor
New contributor
"I am merging all commands onto stderr, but ideally would like to be able to easily identify errors." Well, there's not much you can do then except rely on exit status and manually parsing the text of the error message (which could be unportable and error-prone by itself). I'd rethink the whole logic behind this endeavor.
– Sergiy Kolodyazhnyy
59 mins ago
Related question: Redirecting stderr and stdout separately to a function in a bash script (probably not a duplicate, this one runs several commands in series)
– Michael Homer
34 mins ago
add a comment |
"I am merging all commands onto stderr, but ideally would like to be able to easily identify errors." Well, there's not much you can do then except rely on exit status and manually parsing the text of the error message (which could be unportable and error-prone by itself). I'd rethink the whole logic behind this endeavor.
– Sergiy Kolodyazhnyy
59 mins ago
Related question: Redirecting stderr and stdout separately to a function in a bash script (probably not a duplicate, this one runs several commands in series)
– Michael Homer
34 mins ago
"I am merging all commands onto stderr, but ideally would like to be able to easily identify errors." Well, there's not much you can do then except rely on exit status and manually parsing the text of the error message (which could be unportable and error-prone by itself). I'd rethink the whole logic behind this endeavor.
– Sergiy Kolodyazhnyy
59 mins ago
"I am merging all commands onto stderr, but ideally would like to be able to easily identify errors." Well, there's not much you can do then except rely on exit status and manually parsing the text of the error message (which could be unportable and error-prone by itself). I'd rethink the whole logic behind this endeavor.
– Sergiy Kolodyazhnyy
59 mins ago
Related question: Redirecting stderr and stdout separately to a function in a bash script (probably not a duplicate, this one runs several commands in series)
– Michael Homer
34 mins ago
Related question: Redirecting stderr and stdout separately to a function in a bash script (probably not a duplicate, this one runs several commands in series)
– Michael Homer
34 mins ago
add a comment |
1 Answer
1
active
oldest
votes
TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.
What you may want to consider doing is capturing stderr
to variable, and afterwards checking if it's not empty. Consider the example below:
$ foo=$(stat /etc/passwd 2>&1 > /dev/tty )
File: /etc/passwd
Size: 1710 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 537594 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-03-08 15:28:10.481688274 -0700
Modify: 2019-03-08 15:28:10.333669255 -0700
Change: 2019-03-08 15:28:10.341670283 -0700
Birth: -
$ test "x$foo" = "x" && echo "empty"
empty
Command substitution $(...)
runs a subshell and captures stdout
only, hence we want to duplicate the file descriptor 1 onto 2 2>&1
so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout
at the controlling terminal /dev/tty
.
Now, what if we actually capture stderr into the variable ?
$ foo=$(stat nonexistent.txt 2>&1 > /dev/tty )
$ test "x$foo" = "x" && echo "empty" || echo "not empty"
not empty
$ printf "<Arbitrary Text>%sn" "$foo"
<Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory
As you can see the because stderr
is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout
file descriptor 1, so there's not a lot we can do to parse stderr
on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo
, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.
In case you want both stdout and stderr in separate variables for processing, there is a good example on Stack Overflow already of that can be achieved via named pipes, along with a few shell-specific approaches/tricks.
Side note: test
and [
are the same command, so you could just as well write [ "x$foo" = "x" ]
and use that in a more elaborate if-else statement
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.
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%2f505260%2fappend-string-on-redirect%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.
What you may want to consider doing is capturing stderr
to variable, and afterwards checking if it's not empty. Consider the example below:
$ foo=$(stat /etc/passwd 2>&1 > /dev/tty )
File: /etc/passwd
Size: 1710 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 537594 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-03-08 15:28:10.481688274 -0700
Modify: 2019-03-08 15:28:10.333669255 -0700
Change: 2019-03-08 15:28:10.341670283 -0700
Birth: -
$ test "x$foo" = "x" && echo "empty"
empty
Command substitution $(...)
runs a subshell and captures stdout
only, hence we want to duplicate the file descriptor 1 onto 2 2>&1
so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout
at the controlling terminal /dev/tty
.
Now, what if we actually capture stderr into the variable ?
$ foo=$(stat nonexistent.txt 2>&1 > /dev/tty )
$ test "x$foo" = "x" && echo "empty" || echo "not empty"
not empty
$ printf "<Arbitrary Text>%sn" "$foo"
<Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory
As you can see the because stderr
is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout
file descriptor 1, so there's not a lot we can do to parse stderr
on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo
, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.
In case you want both stdout and stderr in separate variables for processing, there is a good example on Stack Overflow already of that can be achieved via named pipes, along with a few shell-specific approaches/tricks.
Side note: test
and [
are the same command, so you could just as well write [ "x$foo" = "x" ]
and use that in a more elaborate if-else statement
add a comment |
TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.
What you may want to consider doing is capturing stderr
to variable, and afterwards checking if it's not empty. Consider the example below:
$ foo=$(stat /etc/passwd 2>&1 > /dev/tty )
File: /etc/passwd
Size: 1710 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 537594 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-03-08 15:28:10.481688274 -0700
Modify: 2019-03-08 15:28:10.333669255 -0700
Change: 2019-03-08 15:28:10.341670283 -0700
Birth: -
$ test "x$foo" = "x" && echo "empty"
empty
Command substitution $(...)
runs a subshell and captures stdout
only, hence we want to duplicate the file descriptor 1 onto 2 2>&1
so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout
at the controlling terminal /dev/tty
.
Now, what if we actually capture stderr into the variable ?
$ foo=$(stat nonexistent.txt 2>&1 > /dev/tty )
$ test "x$foo" = "x" && echo "empty" || echo "not empty"
not empty
$ printf "<Arbitrary Text>%sn" "$foo"
<Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory
As you can see the because stderr
is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout
file descriptor 1, so there's not a lot we can do to parse stderr
on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo
, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.
In case you want both stdout and stderr in separate variables for processing, there is a good example on Stack Overflow already of that can be achieved via named pipes, along with a few shell-specific approaches/tricks.
Side note: test
and [
are the same command, so you could just as well write [ "x$foo" = "x" ]
and use that in a more elaborate if-else statement
add a comment |
TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.
What you may want to consider doing is capturing stderr
to variable, and afterwards checking if it's not empty. Consider the example below:
$ foo=$(stat /etc/passwd 2>&1 > /dev/tty )
File: /etc/passwd
Size: 1710 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 537594 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-03-08 15:28:10.481688274 -0700
Modify: 2019-03-08 15:28:10.333669255 -0700
Change: 2019-03-08 15:28:10.341670283 -0700
Birth: -
$ test "x$foo" = "x" && echo "empty"
empty
Command substitution $(...)
runs a subshell and captures stdout
only, hence we want to duplicate the file descriptor 1 onto 2 2>&1
so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout
at the controlling terminal /dev/tty
.
Now, what if we actually capture stderr into the variable ?
$ foo=$(stat nonexistent.txt 2>&1 > /dev/tty )
$ test "x$foo" = "x" && echo "empty" || echo "not empty"
not empty
$ printf "<Arbitrary Text>%sn" "$foo"
<Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory
As you can see the because stderr
is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout
file descriptor 1, so there's not a lot we can do to parse stderr
on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo
, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.
In case you want both stdout and stderr in separate variables for processing, there is a good example on Stack Overflow already of that can be achieved via named pipes, along with a few shell-specific approaches/tricks.
Side note: test
and [
are the same command, so you could just as well write [ "x$foo" = "x" ]
and use that in a more elaborate if-else statement
TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.
What you may want to consider doing is capturing stderr
to variable, and afterwards checking if it's not empty. Consider the example below:
$ foo=$(stat /etc/passwd 2>&1 > /dev/tty )
File: /etc/passwd
Size: 1710 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 537594 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-03-08 15:28:10.481688274 -0700
Modify: 2019-03-08 15:28:10.333669255 -0700
Change: 2019-03-08 15:28:10.341670283 -0700
Birth: -
$ test "x$foo" = "x" && echo "empty"
empty
Command substitution $(...)
runs a subshell and captures stdout
only, hence we want to duplicate the file descriptor 1 onto 2 2>&1
so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout
at the controlling terminal /dev/tty
.
Now, what if we actually capture stderr into the variable ?
$ foo=$(stat nonexistent.txt 2>&1 > /dev/tty )
$ test "x$foo" = "x" && echo "empty" || echo "not empty"
not empty
$ printf "<Arbitrary Text>%sn" "$foo"
<Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory
As you can see the because stderr
is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout
file descriptor 1, so there's not a lot we can do to parse stderr
on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo
, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.
In case you want both stdout and stderr in separate variables for processing, there is a good example on Stack Overflow already of that can be achieved via named pipes, along with a few shell-specific approaches/tricks.
Side note: test
and [
are the same command, so you could just as well write [ "x$foo" = "x" ]
and use that in a more elaborate if-else statement
edited 1 hour ago
answered 1 hour ago
Sergiy KolodyazhnyySergiy Kolodyazhnyy
10.5k42663
10.5k42663
add a comment |
add a comment |
TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.
TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.
TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.
TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f505260%2fappend-string-on-redirect%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
"I am merging all commands onto stderr, but ideally would like to be able to easily identify errors." Well, there's not much you can do then except rely on exit status and manually parsing the text of the error message (which could be unportable and error-prone by itself). I'd rethink the whole logic behind this endeavor.
– Sergiy Kolodyazhnyy
59 mins ago
Related question: Redirecting stderr and stdout separately to a function in a bash script (probably not a duplicate, this one runs several commands in series)
– Michael Homer
34 mins ago