A nightly Bash script runs an extract command, pipes its output through gzip into a file, and then uploads that file to Amazon S3. The script must exit with a nonzero status when any command fails, including the extract command at the start of the pipeline, so a failed extract is never uploaded. Which setting meets this requirement?
Show the answer
Correct answerC. set -eo pipefail
AWhy not this: Exit-on-error alone sees only the pipeline's last command, so a failed extract followed by a successful gzip does not stop the script.
BWhy not this: Tracing prints each command as it runs but does not stop the script on a failure.
CWhy this is right: Exit-on-error with pipefail makes any failing command in the pipeline stop the script before the upload.
DWhy not this: This option treats unset variables as errors; it does not detect a failed command in a pipeline.
By default a pipeline's exit status is that of its last command, so a failed extract followed by a successful gzip looks like success. The pipefail option makes the pipeline fail when any command in it fails, and set -e then stops the script before the upload. set -e alone misses failures earlier in a pipeline.