Score:0

How to stop the bash script when a PIPED command meets an error?

us flag

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.

Andrej Podzimek avatar
cn flag
`set -o pipefail` is one option. Also, you have a `PIPESTATUS` automatically generated array in Bash. Try this, for example: `true | false | ( exit 7; ) | ( exit 3; ) | :; s=("${PIPESTATUS[@]}"); for i in "${!s[@]}"; do echo "stage ${i} exited with ${s[i]}"; done;` (Notice how `PIPESTATUS` needs to be copied for further inspection, because each command resets it.)
Score:2
hr flag

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

  pipefail
          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.

Score:0
st flag

You need to check the value of the $? variable provided by the shell. If you ran the failed command without the pipe it would give error code 1, if you check it after the pipe it will probably give error code 127. If it's not zero the operation has failed. (The short and sweet of it.)

Piping w/o error checking isn't really a good solution. Write this into a small shell script that checks the value of the $? variable:

#!/bin/bash

# run first command here...
mysqldump > output.dump

if [ "$?" = 0 ] ; then
   # do the second thing here
   cat output.dump | mysql
fi

if [ "$?" = 0 ] ; then
   # this is checking now that the mysql program didn't throw an error
   echo "Operation Successful!"
else
   echo "Operation failed!"
fi

We're still using the pipe here, but validating the dump was successful before we use the data.

Just checking whether the "output" exists isn't valid because depending on the fates it may or may not create a filename once it begins the dump process. You should presume that any command would fail. The pipe mechanism is mostly for using for one-off commands and doesn't really have any error detection. If this process is something you do more than once then you should have a script that is checking the error codes.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.