Search code examples
erlangexitescript

Cannot get erlang escript exit code if the code is not 0


According to the manual of erlang http://erlang.org/doc/man/escript.html:

If the main/1 function in the script returns successfully,the exit status for the script is 0. If an exception is generated during execution, a short message is printed and the script terminates with exit status 127.

To return your own non-zero exit code, call halt(ExitCode), for example:

halt(1).

but, I called halt(1) to return the exit status 1 to caller, the caller cannot get the exit code, and commands below the $ERTS_DIR/bin/escript myscript in my shell script is not running. by the way, if myscript exit normally, the exit code 0 is received, and commands below the $ERTS_DIR/bin/escript myscript is running. what can I do for this?


Solution

  • It sounds like the caller is a shell script using set -e, meaning that it will exit if any command returns a non-zero exit code.

    One thing you could do is wrapping the call in an if:

    if $ERTS_DIR/bin/escript myscript; then
        echo "the escript ran successfully"
    else
        echo "the escript failed with exit code $?"
    fi
    

    If you only want to do something special on failure, put a ! in front:

    if ! $ERTS_DIR/bin/escript myscript; then
        echo "the escript failed"
    fi