What is the best way to write a wrapper function that runs commands and logs their exit code

前端 未结 2 764
遥遥无期
遥遥无期 2020-12-31 03:13

I currently use this function to wrap executing commands and logging their execution, and return code, and exiting in case of a non-zero return code.

However this is

2条回答
  •  北海茫月
    2020-12-31 03:55

    Additional to "$@" what Douglas says, i would use

    return $?
    

    And not exit. It would exit your shell instead of returning from the function. If in cases you want to exit your shell if something went wrong, you can do that in the caller:

    do_cmd false i will fail executing || exit
    # commands in a row. exit as soon as the first fails
    do_cmd one && do_cmd && two && do_cmd three || exit
    

    (That way, you can handle failures and then exit gracefully).

提交回复
热议问题