Code Monkey home page Code Monkey logo

Comments (1)

mgerdts avatar mgerdts commented on August 15, 2024

set -euo pipefail is equivalent to set -e -u -o pipefail. According to bash(1):

       set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
...
              -e      Exit  immediately  if  a  simple  command (see SHELL
                      GRAMMAR above) exits with a  non-zero  status.   The
                      shell  does  not  exit  if the command that fails is
                      part of the command  list  immediately  following  a
                      while  or  until  keyword, part of the test in an if
                      statement, part of a && or || list, or if  the  com-
                      mand's return value is being inverted via !.  A trap
                      on ERR, if set, is executed before the shell  exits.
...
              -o option-name
...
                      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.
...
              -u      Treat unset variables as an  error  when  performing
                      parameter  expansion.   If expansion is attempted on
                      an unset variable, the shell prints  an  error  mes-
                      sage, and, if not interactive, exits with a non-zero
                      status.

In other words, the -e option makes it so that you can be a bit sloppy about checking return values from commands, as the shell will terminate the script if a command fails. This does change the way that you have to do error checking. For instance, this will exit before printing "ouch".

set -e
cp foo bar
if (( $? != 0 )); then
    echo ouch
fi

It would need to be rewritten as

if ! cp foo bar; then
    echo ouch
fi

The -u option guards against typos in variable names and unexpected paths where variables are used before being set.

-o pipefail makes it so that when one command in a pipeline fails, the error doesn't get lost. Consider this command that is wanting to find accounts that may allow passwordless login.

cat /etc/shadow | awk -F: '$2 == "" { print $1 }' > lock_these_accounts

If you can't read /etc/shadow this will always create an empty file lock_these_accounts. If pipefail is not set, the exit code for the pipeline will be 0 (success). If pipefail is set, the exit code will be non-zero (failure). In this case, pipefail allows you to recognize that something in the pipeline failed. If -e is also set, this will cause the script to exit as well.

from mi-centos-7.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.