Add (collect) exit codes in bash

前端 未结 7 907
执念已碎
执念已碎 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 02:57

    Here are some ways to perform an addition in bash or sh:

    RESULT=`expr $RESULT + $?`
    RESULT=`dc -e "$RESULT $? + pq"`
    

    And some others in bash only:

    RESULT=$((RESULT + $?))
    RESULT=`bc <<< "$RESULT + $?"` 
    

    Anyway, exit status on error is not always 1 and its value does not depend on error level, so in the general case there is not much sense to check a sum of statuses against a threshold.

提交回复
热议问题