How to get exit code from a command block

1

Suppose I have the following commands in a script

susd
systemctl status firewalld

If I look at exit code through echo $? it returns 0 because the last command was executed successfully, but how can I get the exit code of the whole block? in my example it should be an exit code 1 or greater because susd does not exist.

    
asked by anonymous 03.08.2018 / 01:18

2 answers

0
set -e

Force completion of a pipeline (or sequence of commands) when an error occurs, so you can get the exit status through $?

Reference: set

    
03.08.2018 / 01:49
0

Probably not the best option, but you can create a function with $? and call after each command to check exit_code , something like:

#!/bin/bash

captura_erro(){
        if [ $? -eq 0 ]; then
                echo "Sucesso"
        else
                echo "Erro"
        fi
}

susd > /dev/null 2>&1
captura_erro
ls > /dev/null 2>&1
captura_erro
catinho > /dev/null 2>&1
captura_erro
    
03.08.2018 / 01:55