Access standard error on other side of ||?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I want to do something like:
command || log $error_from_last_command
Is there a way to use ||
and still access stderr
like a pipe?
My intention here is to process the error message from command
, using log
, but only if command
fails.
I'm reading through the marked duplicate but I don't see how to apply that to my situation.
bash
add a comment |Â
up vote
2
down vote
favorite
I want to do something like:
command || log $error_from_last_command
Is there a way to use ||
and still access stderr
like a pipe?
My intention here is to process the error message from command
, using log
, but only if command
fails.
I'm reading through the marked duplicate but I don't see how to apply that to my situation.
bash
Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
â ilkkachu
Jan 22 at 15:29
@ilkkachu yes as you describe,log
should only run ifcommand
fails. The input oflog
should be the failure message ofcommand
to be sent to server for reporting.
â Philip Kirkbride
Jan 22 at 15:31
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to do something like:
command || log $error_from_last_command
Is there a way to use ||
and still access stderr
like a pipe?
My intention here is to process the error message from command
, using log
, but only if command
fails.
I'm reading through the marked duplicate but I don't see how to apply that to my situation.
bash
I want to do something like:
command || log $error_from_last_command
Is there a way to use ||
and still access stderr
like a pipe?
My intention here is to process the error message from command
, using log
, but only if command
fails.
I'm reading through the marked duplicate but I don't see how to apply that to my situation.
bash
edited Jan 23 at 2:20
asked Jan 22 at 15:09
Philip Kirkbride
2,2922369
2,2922369
Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
â ilkkachu
Jan 22 at 15:29
@ilkkachu yes as you describe,log
should only run ifcommand
fails. The input oflog
should be the failure message ofcommand
to be sent to server for reporting.
â Philip Kirkbride
Jan 22 at 15:31
add a comment |Â
Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
â ilkkachu
Jan 22 at 15:29
@ilkkachu yes as you describe,log
should only run ifcommand
fails. The input oflog
should be the failure message ofcommand
to be sent to server for reporting.
â Philip Kirkbride
Jan 22 at 15:31
Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
â ilkkachu
Jan 22 at 15:29
Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
â ilkkachu
Jan 22 at 15:29
@ilkkachu yes as you describe,
log
should only run if command
fails. The input of log
should be the failure message of command
to be sent to server for reporting.â Philip Kirkbride
Jan 22 at 15:31
@ilkkachu yes as you describe,
log
should only run if command
fails. The input of log
should be the failure message of command
to be sent to server for reporting.â Philip Kirkbride
Jan 22 at 15:31
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.
errfile=$(mktemp)
if ! somecommand 2> "$errfile" ; then
log < "$errfile" # or log "$(cat "$errfile")" ?
fi
rm "$errfile"
Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.
log < "$errfile"
above would of course direct the error message to stdin of log
(like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")"
(one argument), or log $(cat "$errfile")
(with word splitting, log
sees multiple arguments), or log "$(< "$errfile")"
(non-standard, works at least in Bash).
Ah I see. It works withcat
, iflog
in anSTDIN
-reader. :-)
â Richard Neumann
Jan 22 at 16:07
add a comment |Â
up vote
0
down vote
If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash
variable PIPESTATUS
, which according to the bash
man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".
I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.
errfile=$(mktemp)
if ! somecommand 2> "$errfile" ; then
log < "$errfile" # or log "$(cat "$errfile")" ?
fi
rm "$errfile"
Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.
log < "$errfile"
above would of course direct the error message to stdin of log
(like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")"
(one argument), or log $(cat "$errfile")
(with word splitting, log
sees multiple arguments), or log "$(< "$errfile")"
(non-standard, works at least in Bash).
Ah I see. It works withcat
, iflog
in anSTDIN
-reader. :-)
â Richard Neumann
Jan 22 at 16:07
add a comment |Â
up vote
3
down vote
accepted
If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.
errfile=$(mktemp)
if ! somecommand 2> "$errfile" ; then
log < "$errfile" # or log "$(cat "$errfile")" ?
fi
rm "$errfile"
Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.
log < "$errfile"
above would of course direct the error message to stdin of log
(like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")"
(one argument), or log $(cat "$errfile")
(with word splitting, log
sees multiple arguments), or log "$(< "$errfile")"
(non-standard, works at least in Bash).
Ah I see. It works withcat
, iflog
in anSTDIN
-reader. :-)
â Richard Neumann
Jan 22 at 16:07
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.
errfile=$(mktemp)
if ! somecommand 2> "$errfile" ; then
log < "$errfile" # or log "$(cat "$errfile")" ?
fi
rm "$errfile"
Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.
log < "$errfile"
above would of course direct the error message to stdin of log
(like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")"
(one argument), or log $(cat "$errfile")
(with word splitting, log
sees multiple arguments), or log "$(< "$errfile")"
(non-standard, works at least in Bash).
If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.
errfile=$(mktemp)
if ! somecommand 2> "$errfile" ; then
log < "$errfile" # or log "$(cat "$errfile")" ?
fi
rm "$errfile"
Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.
log < "$errfile"
above would of course direct the error message to stdin of log
(like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")"
(one argument), or log $(cat "$errfile")
(with word splitting, log
sees multiple arguments), or log "$(< "$errfile")"
(non-standard, works at least in Bash).
edited Jan 22 at 15:53
answered Jan 22 at 15:40
ilkkachu
49.8k674137
49.8k674137
Ah I see. It works withcat
, iflog
in anSTDIN
-reader. :-)
â Richard Neumann
Jan 22 at 16:07
add a comment |Â
Ah I see. It works withcat
, iflog
in anSTDIN
-reader. :-)
â Richard Neumann
Jan 22 at 16:07
Ah I see. It works with
cat
, if log
in an STDIN
-reader. :-)â Richard Neumann
Jan 22 at 16:07
Ah I see. It works with
cat
, if log
in an STDIN
-reader. :-)â Richard Neumann
Jan 22 at 16:07
add a comment |Â
up vote
0
down vote
If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash
variable PIPESTATUS
, which according to the bash
man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".
I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.
add a comment |Â
up vote
0
down vote
If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash
variable PIPESTATUS
, which according to the bash
man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".
I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash
variable PIPESTATUS
, which according to the bash
man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".
I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.
If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash
variable PIPESTATUS
, which according to the bash
man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".
I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.
answered Jan 22 at 16:18
user1404316
2,314520
2,314520
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418884%2faccess-standard-error-on-other-side-of%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
Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
â ilkkachu
Jan 22 at 15:29
@ilkkachu yes as you describe,
log
should only run ifcommand
fails. The input oflog
should be the failure message ofcommand
to be sent to server for reporting.â Philip Kirkbride
Jan 22 at 15:31