How to detect an error using process substitution The Next CEO of Stack Overflowbash: how to propagate errors in process substitution?How can I pipe output to another process, but retain the error state of the first process?using “ifne” in Bash pipeUsing process substitution to trick programs expecting files, with specific extensions as argument?process substitution for opening a list of files with an applicationUnderstanding Bash's Read-a-File Command SubstitutionHow bash treats “> >()”Error exit script from within command substitutionblocking/non-blocking pipes/redirects inside command substitutionBash Process Substitution Hangs When Listing BindingsWhy is bash history substitution still enabled by default?
What can we do to stop prior company from asking us questions?
How does the mv command work with external drives?
Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis
Does it take more energy to get to Venus or to Mars?
Between two walls
Is it possible to search for a directory/file combination?
Why am I allowed to create multiple unique pointers from a single object?
What happens if you roll doubles 3 times then land on "Go to jail?"
calculus parametric curve length
Sending manuscript to multiple publishers
Why didn't Khan get resurrected in the Genesis Explosion?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
How to make a variable always equal to the result of some calculations?
What does convergence in distribution "in the Gromov–Hausdorff" sense mean?
How to transpose the 1st and -1th levels of arbitrarily nested array?
How did people program for Consoles with multiple CPUs?
WOW air has ceased operation, can I get my tickets refunded?
How to solve a differential equation with a term to a power?
Workaholic Formal/Informal
Written every which way
Indicator light circuit
Why does the UK parliament need a vote on the political declaration?
Why has the US not been more assertive in confronting Russia in recent years?
In excess I'm lethal
How to detect an error using process substitution
The Next CEO of Stack Overflowbash: how to propagate errors in process substitution?How can I pipe output to another process, but retain the error state of the first process?using “ifne” in Bash pipeUsing process substitution to trick programs expecting files, with specific extensions as argument?process substitution for opening a list of files with an applicationUnderstanding Bash's Read-a-File Command SubstitutionHow bash treats “> >()”Error exit script from within command substitutionblocking/non-blocking pipes/redirects inside command substitutionBash Process Substitution Hangs When Listing BindingsWhy is bash history substitution still enabled by default?
This question is similar to the following link, but focused on using the command line (bash shell).
Using a simple example, when doing the following command:
$ cat <(date); echo $?
Fri Jul 7 21:04:38 UTC 2017
0
The exit value is 0 as expected.
In the following command there is an error introduced on purpose, but the return value is still 0:
$ cat <(datE); echo $?
bash: datE: command not found...
Similar command is: 'date'
0
Is there a way to catch that there was an error in the process substitution when run on the command line (i.e. without having to put it into a script) ?
The solution in the included link above kills the script that is running the command.
bash pipe process-substitution exit-status
|
show 3 more comments
This question is similar to the following link, but focused on using the command line (bash shell).
Using a simple example, when doing the following command:
$ cat <(date); echo $?
Fri Jul 7 21:04:38 UTC 2017
0
The exit value is 0 as expected.
In the following command there is an error introduced on purpose, but the return value is still 0:
$ cat <(datE); echo $?
bash: datE: command not found...
Similar command is: 'date'
0
Is there a way to catch that there was an error in the process substitution when run on the command line (i.e. without having to put it into a script) ?
The solution in the included link above kills the script that is running the command.
bash pipe process-substitution exit-status
1
Not sure if this fits your definition of "detect an error" butcat <(datE || echo $? >&2)??
– B Layer
Jul 8 '17 at 1:07
@BlairM. Thanks, that is a good idea and helps. I was thinking more along the lines of how thecatcommand would exit with a non-zero value. I was using the linuxparallelcommand to run a bunch of commands in a file and want it to return a non-zero value if one using "process substitution" fails. That was a detail not really needed for this question.
– steveb
Jul 8 '17 at 1:20
Cool. I'll add as an answer then.
– B Layer
Jul 8 '17 at 1:21
The answer I am looking for should address the propagation of the error though. The above suggestion doesn't address that in its current form.
– steveb
Jul 8 '17 at 1:23
Hehe. Okay. I'll think about a more elaborate approach.
– B Layer
Jul 8 '17 at 1:24
|
show 3 more comments
This question is similar to the following link, but focused on using the command line (bash shell).
Using a simple example, when doing the following command:
$ cat <(date); echo $?
Fri Jul 7 21:04:38 UTC 2017
0
The exit value is 0 as expected.
In the following command there is an error introduced on purpose, but the return value is still 0:
$ cat <(datE); echo $?
bash: datE: command not found...
Similar command is: 'date'
0
Is there a way to catch that there was an error in the process substitution when run on the command line (i.e. without having to put it into a script) ?
The solution in the included link above kills the script that is running the command.
bash pipe process-substitution exit-status
This question is similar to the following link, but focused on using the command line (bash shell).
Using a simple example, when doing the following command:
$ cat <(date); echo $?
Fri Jul 7 21:04:38 UTC 2017
0
The exit value is 0 as expected.
In the following command there is an error introduced on purpose, but the return value is still 0:
$ cat <(datE); echo $?
bash: datE: command not found...
Similar command is: 'date'
0
Is there a way to catch that there was an error in the process substitution when run on the command line (i.e. without having to put it into a script) ?
The solution in the included link above kills the script that is running the command.
bash pipe process-substitution exit-status
bash pipe process-substitution exit-status
edited Jul 9 '17 at 23:21
Gilles
545k12811071622
545k12811071622
asked Jul 8 '17 at 0:54
stevebsteveb
1112
1112
1
Not sure if this fits your definition of "detect an error" butcat <(datE || echo $? >&2)??
– B Layer
Jul 8 '17 at 1:07
@BlairM. Thanks, that is a good idea and helps. I was thinking more along the lines of how thecatcommand would exit with a non-zero value. I was using the linuxparallelcommand to run a bunch of commands in a file and want it to return a non-zero value if one using "process substitution" fails. That was a detail not really needed for this question.
– steveb
Jul 8 '17 at 1:20
Cool. I'll add as an answer then.
– B Layer
Jul 8 '17 at 1:21
The answer I am looking for should address the propagation of the error though. The above suggestion doesn't address that in its current form.
– steveb
Jul 8 '17 at 1:23
Hehe. Okay. I'll think about a more elaborate approach.
– B Layer
Jul 8 '17 at 1:24
|
show 3 more comments
1
Not sure if this fits your definition of "detect an error" butcat <(datE || echo $? >&2)??
– B Layer
Jul 8 '17 at 1:07
@BlairM. Thanks, that is a good idea and helps. I was thinking more along the lines of how thecatcommand would exit with a non-zero value. I was using the linuxparallelcommand to run a bunch of commands in a file and want it to return a non-zero value if one using "process substitution" fails. That was a detail not really needed for this question.
– steveb
Jul 8 '17 at 1:20
Cool. I'll add as an answer then.
– B Layer
Jul 8 '17 at 1:21
The answer I am looking for should address the propagation of the error though. The above suggestion doesn't address that in its current form.
– steveb
Jul 8 '17 at 1:23
Hehe. Okay. I'll think about a more elaborate approach.
– B Layer
Jul 8 '17 at 1:24
1
1
Not sure if this fits your definition of "detect an error" but
cat <(datE || echo $? >&2) ??– B Layer
Jul 8 '17 at 1:07
Not sure if this fits your definition of "detect an error" but
cat <(datE || echo $? >&2) ??– B Layer
Jul 8 '17 at 1:07
@BlairM. Thanks, that is a good idea and helps. I was thinking more along the lines of how the
cat command would exit with a non-zero value. I was using the linux parallel command to run a bunch of commands in a file and want it to return a non-zero value if one using "process substitution" fails. That was a detail not really needed for this question.– steveb
Jul 8 '17 at 1:20
@BlairM. Thanks, that is a good idea and helps. I was thinking more along the lines of how the
cat command would exit with a non-zero value. I was using the linux parallel command to run a bunch of commands in a file and want it to return a non-zero value if one using "process substitution" fails. That was a detail not really needed for this question.– steveb
Jul 8 '17 at 1:20
Cool. I'll add as an answer then.
– B Layer
Jul 8 '17 at 1:21
Cool. I'll add as an answer then.
– B Layer
Jul 8 '17 at 1:21
The answer I am looking for should address the propagation of the error though. The above suggestion doesn't address that in its current form.
– steveb
Jul 8 '17 at 1:23
The answer I am looking for should address the propagation of the error though. The above suggestion doesn't address that in its current form.
– steveb
Jul 8 '17 at 1:23
Hehe. Okay. I'll think about a more elaborate approach.
– B Layer
Jul 8 '17 at 1:24
Hehe. Okay. I'll think about a more elaborate approach.
– B Layer
Jul 8 '17 at 1:24
|
show 3 more comments
2 Answers
2
active
oldest
votes
In your example:
cat <(datE); echo $?
What happens is that datE throws the error and generates no output. It then throws an error code. However, the (null) input is then presented to cat which happily chews on nothing, and now your exit code is zero.
If you take out the intermediary step, it works as you expect:
$ datE; echo $?
datE: command not found
127
If you want bash to to abort on any failures in a pipeline and any uncaught error, run the following two commands:
set -e
set -o pipefail
Other shells may provide similar settings.
I do understand that removing thecatfrom the command line will then cause the error to be caught ($?is 127). My example is simple to keep the question clear. Usingset -eandset -o pipefailwith thecat...version won't cause$?to be 127. This problem came up when using gnu parallel to run commands in a file, some of which use process substitution.
– steveb
Jul 9 '17 at 1:32
add a comment |
An alternative to using process substitution is to use /dev/stdin as the file arg so that pipes work as expected:
set -o pipefail
datE | cat /dev/stdin
The above example is a bit contrived since cat will read from stdin if not given a file arg. It's useful when a command must be given a file.
New contributor
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
);
);
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%2f376114%2fhow-to-detect-an-error-using-process-substitution%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your example:
cat <(datE); echo $?
What happens is that datE throws the error and generates no output. It then throws an error code. However, the (null) input is then presented to cat which happily chews on nothing, and now your exit code is zero.
If you take out the intermediary step, it works as you expect:
$ datE; echo $?
datE: command not found
127
If you want bash to to abort on any failures in a pipeline and any uncaught error, run the following two commands:
set -e
set -o pipefail
Other shells may provide similar settings.
I do understand that removing thecatfrom the command line will then cause the error to be caught ($?is 127). My example is simple to keep the question clear. Usingset -eandset -o pipefailwith thecat...version won't cause$?to be 127. This problem came up when using gnu parallel to run commands in a file, some of which use process substitution.
– steveb
Jul 9 '17 at 1:32
add a comment |
In your example:
cat <(datE); echo $?
What happens is that datE throws the error and generates no output. It then throws an error code. However, the (null) input is then presented to cat which happily chews on nothing, and now your exit code is zero.
If you take out the intermediary step, it works as you expect:
$ datE; echo $?
datE: command not found
127
If you want bash to to abort on any failures in a pipeline and any uncaught error, run the following two commands:
set -e
set -o pipefail
Other shells may provide similar settings.
I do understand that removing thecatfrom the command line will then cause the error to be caught ($?is 127). My example is simple to keep the question clear. Usingset -eandset -o pipefailwith thecat...version won't cause$?to be 127. This problem came up when using gnu parallel to run commands in a file, some of which use process substitution.
– steveb
Jul 9 '17 at 1:32
add a comment |
In your example:
cat <(datE); echo $?
What happens is that datE throws the error and generates no output. It then throws an error code. However, the (null) input is then presented to cat which happily chews on nothing, and now your exit code is zero.
If you take out the intermediary step, it works as you expect:
$ datE; echo $?
datE: command not found
127
If you want bash to to abort on any failures in a pipeline and any uncaught error, run the following two commands:
set -e
set -o pipefail
Other shells may provide similar settings.
In your example:
cat <(datE); echo $?
What happens is that datE throws the error and generates no output. It then throws an error code. However, the (null) input is then presented to cat which happily chews on nothing, and now your exit code is zero.
If you take out the intermediary step, it works as you expect:
$ datE; echo $?
datE: command not found
127
If you want bash to to abort on any failures in a pipeline and any uncaught error, run the following two commands:
set -e
set -o pipefail
Other shells may provide similar settings.
answered Jul 8 '17 at 2:33
DopeGhotiDopeGhoti
46.7k56190
46.7k56190
I do understand that removing thecatfrom the command line will then cause the error to be caught ($?is 127). My example is simple to keep the question clear. Usingset -eandset -o pipefailwith thecat...version won't cause$?to be 127. This problem came up when using gnu parallel to run commands in a file, some of which use process substitution.
– steveb
Jul 9 '17 at 1:32
add a comment |
I do understand that removing thecatfrom the command line will then cause the error to be caught ($?is 127). My example is simple to keep the question clear. Usingset -eandset -o pipefailwith thecat...version won't cause$?to be 127. This problem came up when using gnu parallel to run commands in a file, some of which use process substitution.
– steveb
Jul 9 '17 at 1:32
I do understand that removing the
cat from the command line will then cause the error to be caught ($? is 127). My example is simple to keep the question clear. Using set -e and set -o pipefail with the cat... version won't cause $? to be 127. This problem came up when using gnu parallel to run commands in a file, some of which use process substitution.– steveb
Jul 9 '17 at 1:32
I do understand that removing the
cat from the command line will then cause the error to be caught ($? is 127). My example is simple to keep the question clear. Using set -e and set -o pipefail with the cat... version won't cause $? to be 127. This problem came up when using gnu parallel to run commands in a file, some of which use process substitution.– steveb
Jul 9 '17 at 1:32
add a comment |
An alternative to using process substitution is to use /dev/stdin as the file arg so that pipes work as expected:
set -o pipefail
datE | cat /dev/stdin
The above example is a bit contrived since cat will read from stdin if not given a file arg. It's useful when a command must be given a file.
New contributor
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
An alternative to using process substitution is to use /dev/stdin as the file arg so that pipes work as expected:
set -o pipefail
datE | cat /dev/stdin
The above example is a bit contrived since cat will read from stdin if not given a file arg. It's useful when a command must be given a file.
New contributor
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
An alternative to using process substitution is to use /dev/stdin as the file arg so that pipes work as expected:
set -o pipefail
datE | cat /dev/stdin
The above example is a bit contrived since cat will read from stdin if not given a file arg. It's useful when a command must be given a file.
New contributor
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
An alternative to using process substitution is to use /dev/stdin as the file arg so that pipes work as expected:
set -o pipefail
datE | cat /dev/stdin
The above example is a bit contrived since cat will read from stdin if not given a file arg. It's useful when a command must be given a file.
New contributor
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited yesterday
New contributor
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
NathanNathan
1012
1012
New contributor
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Nathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
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%2f376114%2fhow-to-detect-an-error-using-process-substitution%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
1
Not sure if this fits your definition of "detect an error" but
cat <(datE || echo $? >&2)??– B Layer
Jul 8 '17 at 1:07
@BlairM. Thanks, that is a good idea and helps. I was thinking more along the lines of how the
catcommand would exit with a non-zero value. I was using the linuxparallelcommand to run a bunch of commands in a file and want it to return a non-zero value if one using "process substitution" fails. That was a detail not really needed for this question.– steveb
Jul 8 '17 at 1:20
Cool. I'll add as an answer then.
– B Layer
Jul 8 '17 at 1:21
The answer I am looking for should address the propagation of the error though. The above suggestion doesn't address that in its current form.
– steveb
Jul 8 '17 at 1:23
Hehe. Okay. I'll think about a more elaborate approach.
– B Layer
Jul 8 '17 at 1:24