proper way to detect shell exit code when errexit option set

后端 未结 14 1505
甜味超标
甜味超标 2021-01-30 12:52

I prefer to write solid shell code, so the errexit & nounset is alway set.

The following code will stop at bad_command line

#!/bin/b         


        
14条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 13:46

    A common way to avoid exiting a Bash program when errexit is set and a command that may fail is run is to precede the command with !.

    After the command has run $? does not contain the exit status of the command. (It contains 0 if the command failed and 1 otherwise.) However, the PIPESTATUS array does contain the exit status of the command. A safe way to capture the exit status of a command that may fail, whether or not errexit is set, is:

    ! bad_command
    rc=${PIPESTATUS[0]}
    

    The second line can be simplified to rc=$PIPESTATUS, but Shellcheck will complain about it.

    If (as is often the case) you don't need to know the exit status of the command, just if it succeeded or failed, then the solution by @george is good if the error handler is a one-liner. For multi-line error handlers a good option is:

    if ! bad_command ; then
        # Handle errors here
    fi
    

    Note that (except in very unusual circumstances) it would not be correct to use `bad_command` (as suggested in the question) instead of plain bad_command. You can use ${PIPESTATUS[0]} to get the exit status of the command if it is needed in the error handling code, since $? doesn't contain it in this case either.

提交回复
热议问题