Add (collect) exit codes in bash

前端 未结 7 922
执念已碎
执念已碎 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:47

    As mouviciel mentioned collecting sum of return codes looks rather senseless. Probably, you can use array for accumulating non-zero result codes and check against its length. Example of this approach is below:

    #!/bin/sh
    
    declare RESULT
    declare index=0
    declare ALLOWED_ERROR=1
    
    function write_result {
        if [ $1 -gt 0 ]; then
            RESULT[index++]=$1
        fi
    }
    
    true
    write_result $?
    
    false
    write_result $?
    
    false
    write_result $?
    
    echo ${#RESULT[*]}
    if [ ${#RESULT[*]} -gt $ALLOWEDERROR ] 
       then echo "Too many errors"
    fi
    

提交回复
热议问题