Blaine Mucklow
Blaine Mucklow

Reputation: 592

Bash Script function verification

I am trying to do validation for every function I call in a script and erroring out the whole script if one function fails.

Looking for the best way to do this. I feel I knew a good way to do it at one time, but can't figure it out again.

I can brute force it, but it's nasty. This is how I can get it to work correctly

    copy_files $1
    if [ "$?" -ne "0" ]; then
            error "Copying files"
            return -1
    fi

This gets pretty ugly since I have a script that goes:

command1 command2 command3

Having to wrap all these commands with returns is not very ideal. :( Please help!

Upvotes: 0

Views: 228

Answers (3)

miedwar
miedwar

Reputation: 7278

You could do something like this:

good() { 
    return 0;
}

bad() {
    return 5;
}

wrapper() { 
    $1  

    rc=$?

    if [ "$rc" -ne "0" ]; then
            echo "error: $1 returned $rc"
            exit -1
    fi  
}


wrapper bad 
wrapper good

or, if you could pass a list of function, like so:

wrapper() {
    for f in $*; do

        $f 
        rc=$?

        if [ "$rc" -ne "0" ]; then
                echo "error: $f returned ${rc}"
                return -1
        fi
    done
    return 0;
}


wrapper good bad

if [ "$?" -ne "0" ]; then
    #error handling here
    exit -1;
fi

Upvotes: 0

David Z
David Z

Reputation: 131550

Since you said you want to do this for every command in a script, you could insert

set -e

at the beginning. That call makes the shell exit immediately if any command returns a status code other than 0. (Exceptions: commands that are part of the test in a conditional statement, and those that are followed by && or || and further commands)

Upvotes: 1

Blue Peppers
Blue Peppers

Reputation: 3808

command1 || (error "Cmd1 fail"; return -1); command2 || (error "Cmd2 fail"; return -1);

etc. etc. The || operator means the next command will only execute if the previous command failed. && is the opposite. The brackets group the commands to run.

Upvotes: 3

Related Questions