How to check the exit status using an if statement

后端 未结 7 1613
北荒
北荒 2020-11-29 16:27

I was wondering what would be the best way to check the exit status in an if statement in order to echo a specific output.

I\'m thinking of it being

         


        
相关标签:
7条回答
  • 2020-11-29 16:44

    $? is a parameter like any other. You can save its value to use before ultimately calling exit.

    exit_status=$?
    if [ $exit_status -eq 1 ]; then
        echo "blah blah blah"
    fi
    exit $exit_status
    
    0 讨论(0)
  • 2020-11-29 16:47

    Every command that runs has an exit status.

    That check is looking at the exit status of the command that finished most recently before that line runs.

    If you want your script to exit when that test returns true (the previous command failed) then you put exit 1 (or whatever) inside that if block after the echo.

    That being said if you are running the command and wanting to test its output using the following is often more straight-forward.

    if some_command; then
        echo command returned true
    else
        echo command returned some error
    fi
    

    Or to turn that around use ! for negation

    if ! some_command; then
        echo command returned some error
    else
        echo command returned true
    fi
    

    Note though that neither of those cares what the error code is. If you know you only care about a specific error code then you need to check $? manually.

    0 讨论(0)
  • 2020-11-29 16:50

    For the record, if the script is run with set -e (or #!/bin/bash -e) and you therefore cannot check $? directly (since the script would terminate on any return code other than zero), but want to handle a specific code, @gboffis comment is great:

    /some/command || error_code=$?
    if [ "${error_code}" -eq 2 ]; then
       ...
    
    0 讨论(0)
  • 2020-11-29 16:51

    Note that exit codes != 0 are used to report error. So, it's better to do:

    retVal=$?
    if [ $retVal -ne 0 ]; then
        echo "Error"
    fi
    exit $retVal
    

    instead of

    # will fail for error codes > 1
    retVal=$?
    if [ $retVal -eq 1 ]; then
        echo "Error"
    fi
    exit $retVal
    
    0 讨论(0)
  • 2020-11-29 16:54

    Alternative to explicit if statement

    Minimally:

    test $? -eq 0 || echo "something bad happened"

    Complete:

    EXITCODE=$?
    test $EXITCODE -eq 0 && echo "something good happened" || echo "something bad happened"; 
    exit $EXITCODE
    
    0 讨论(0)
  • 2020-11-29 16:55

    Just to add to the helpful and detailed answer:

    If you have to check the exit code explicitly, it is better to use the arithmetic operator, (( ... )), this way:

    run_some_command
    (($? != 0)) && { printf '%s\n' "Command exited with non-zero"; exit 1; }
    

    Or, use a case statement:

    run_some_command; ec=$?  # grab the exit code into a variable so that it can
                             # be reused later, without the fear of being overwritten
    case $ec in
        0) ;;
        1) printf '%s\n' "Command exited with non-zero"; exit 1;;
        *) do_something_else;;
    esac
    

    Related answer about error handling in Bash:

    • Raise error in a Bash script
    0 讨论(0)
提交回复
热议问题