Add (collect) exit codes in bash

前端 未结 7 914
执念已碎
执念已碎 2021-02-08 02:28

I need to depend on few separate executions in a script and don\'t want to bundle them all in an ugly \'if\' statement. I would like to take the exit code \'$?\' of each executi

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-08 03:11

    Use the $(( ... )) construct.

    $ cat st.sh
    RESULT=0
    true
    RESULT=$(($RESULT + $?))
    false
    RESULT=$(($RESULT + $?))
    false
    RESULT=$(($RESULT + $?))
    echo $RESULT
    $ sh st.sh
    2
    $
    

提交回复
热议问题