bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Differentiating false from fatal


From: Dan Stromberg
Subject: Differentiating false from fatal
Date: Tue, 09 Sep 2008 22:32:54 GMT
User-agent: Pan/0.132 (Waxed in Black)

Say you have a shell script, and you need it to be bulletproof.

As you write it, you throw in error checking all over the place.

But say you have a function that needs to return a boolean result in some 
way - call the function "bool_foo" for the sake of discussion.  Further 
assume that bool_foo will sometimes fail to return a result, and it's 
defined with:

function bool_foo
(
xyz
)

...and not function bool_foo
{
xyz
}

...so that bool_foo's variables don't mess with those of other functions, 
but also making it so it cannot just exit 1 to terminate the program 
directly.

How would you go about making bool_foo return a boolean result -and- be 
able to return a fatal?

Do you make bool_foo echo "True" or "False" and use the exit status to 
reflect success/failure?

Or do you make 0 mean True, 1 mean False, and anything else mean fatal in 
$??  And if so, how do you go about differentiating these three things - 
do you use nested if's or a case?  And what if you have set -e in effect?

I'm sure I can come up with A way of handling this, but what have you 
found works well in practice?

I suspect I'm going to end up rewriting my bool_foo functions to echo 
True or False, and then use their exit status to indicated success or 
failure.  So instead of:

if bool_foo
then
        : true
else
        : false
fi

...it seems I'll end up with this mess scattered all over (with set -eu 
in effect, among other things) :

case "$(shell_init; bool_foo)" in
        True)
                : true
                ;;
        False)
                : true
                ;;
        *)
                echo "$0: Unrecognized output from bool_foo" 1>&2
                exit 1
                ;;
esac

I guess what I'm saying is "I miss having an exception mechanism" :)

Followups directed to comp.unix.shell.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]