Return an exit code without closing shell

前端 未结 5 1055
后悔当初
后悔当初 2021-01-01 12:39

I\'d like to return an exit code from a BASH script that is called within another script, but could also be called directly. It roughly looks like this:

#!/b         


        
相关标签:
5条回答
  • 2021-01-01 12:47

    The answer to the question title (not in the body as other answers have addressed) is:

    Return an exit code without closing shell

    (exit 33)
    

    If you need to have -e active and still avoid exiting the shell with a non-zero exit code, then do:

    (exit 33) && true
    

    The true command is never executed but is used to build a compound command that is not exited by the -e shell flag.

    That sets the exit code without exiting the shell (nor a sourced script).

    For the more complex question of exiting (with an specific exit code) either if executed or sourced:

    #!/bin/bash
    [ "$BASH_SOURCE" == "$0" ] &&
        echo "This file is meant to be sourced, not executed" && 
            exit 30
    
    return 88
    

    Will set an exit code of 30 (with an error message) if executed.
    And an exit code of 88 if sourced. Will exit both the execution or the sourcing without affecting the calling shell.

    0 讨论(0)
  • 2021-01-01 12:51

    You can use x"${BASH_SOURCE[0]}" == x"$0" to test if the script was sourced or called (false if sourced, true if called) and return or exit accordingly.

    0 讨论(0)
  • 2021-01-01 12:51

    You can use return if you use set -e in the beginning of the script.

    If you just want to check if the function returned no errors, I'd rather suggest rewriting your code like this:

    #!/bin/bash
    
    set -e # exit program if encountered errors
    
    dq2-get ()
    {
      # define the function here
      # ...
      if [ $1 -eq 0 ]
      then
        return 0
      else
        return 255
      # Note that nothing will execute from this point on,
      # because `return` terminates the function.
    }
    
    # ...
    # lots of code ...
    # ...
    
    # Now, the test:
    # This won't exit the program.
    if $(dq2-get $1); then
      echo "No errors, everything's fine"
    else
      echo "ERROR: ..."
    fi
    # These commands execute anyway, no matter what
    # `dq2-get $1` returns (i.e. {0..255}).
    # extract, do some stuff
    # ...
    

    Now, the code above won't leave the program if the function dq2-get $1 returns errors. But, implementing the function all by itself will exit the program because of the set -e. The code below describes this situation:

    # The function below will stop the program and exit
    # if it returns anything other than `0`
    # since `set -e` means stop if encountered any errors.
    $(dq2-get $1)
    # These commands execute ONLY if `dq2-get $1` returns `0`
    # extract, do some stuff
    # ...
    
    0 讨论(0)
  • 2021-01-01 13:09

    Use this instead of exit or return:

    [ $PS1 ] && return || exit;
    

    Works whether sourced or not.

    0 讨论(0)
  • 2021-01-01 13:12

    Another option is to use a function and put the return values in that and then simply either source the script (source processStatus.sh) or call the script (./processStatus.sh) . For example consider the processStatus.sh script that needs to return a value to the stopProcess.sh script but also needs to be called separately from say the command line without using source (only relevant parts included) Eg:

    check_process ()
    {
      if [ $1 -eq "50" ]
      then
        return 1       
      else
        return 0
      fi       
    }
    

    and

    source processStatus.sh $1
    RET_VALUE=$?
    if [ $RET_VALUE -ne "0" ]
    then
      exit 0
    fi
    
    0 讨论(0)
提交回复
热议问题