Saturday, February 24, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  2] [ 0]  / answers: 1 / hits: 392  / 3 Years ago, mon, may 24, 2021, 3:00:39

I have some pipes in my bash script, notably mysqldump | mysql

How do I get the script to stop if any of the piped processes return non-zero exit code?

In my tests, if mysqldump fails, the script continues despite the statement set -e at the script start.

More From » bash


You probably want to set the shell's pipefail option:

If set, the return value of a pipeline is the
value of the last (rightmost) command to exit
with a non-zero status, or zero if all commands
in the pipeline exit successfully. This option
is disabled by default.

So, set -eo pipefail in place of just set -e. See also Retrieving status code when using pipelines.

[#93] Tuesday, May 25, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 150
Total Questions: 106
Total Answers: 101

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
lawain questions