Debian Developers - test-boolean tool

This is Interesting: Free IT Magazines  
Home > Archive > Debian Developers > July 2004 > test-boolean tool





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author test-boolean tool
ms419@freezone.co.uk

2004-07-30, 5:52 pm

Is there a tool to test strings as boolean values? For instance,
test-booean true, True, TRUE, T, yes, on, 1, succeed, but test-boolean
false, False, FALSE, F, no, off, 0, fail?

I'm writing an init script in which a tool like this would be useful.
I've looked at other init scripts, which simply test if ["$VAR" == true
], but some packages use "true", some use "yes", etc.

If a tool doesn't exist, I think it would be useful in many places and
simple to create. I think there should be a modular tool for this
common task (like the start-stop-daemon or debhelper tools).

One problem I can't resolve is what it should do on test-boolean
notaboolean. Better than resulting in some default behavior (true or
false) on invalid input, would be resulting in an exception. However,
my foreseen application is in a shell script, the if statement of which
(AFAIK) won't result in exceptions (an error is failure is false).

Any suggestions?

Thanks!

Jack


--
To UNSUBSCRIBE, email to debian-devel-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Sergio Talens-Oliag

2004-07-31, 2:47 am

El Fri, Jul 30, 2004 at 03:02:32PM -0700, ms419@freezone.co.uk va escriure:
> Is there a tool to test strings as boolean values? For instance,
> test-booean true, True, TRUE, T, yes, on, 1, succeed, but test-boolean
> false, False, FALSE, F, no, off, 0, fail?
>
> I'm writing an init script in which a tool like this would be useful.
> I've looked at other init scripts, which simply test if ["$VAR" == true
> ], but some packages use "true", some use "yes", etc.
>
> If a tool doesn't exist, I think it would be useful in many places and
> simple to create. I think there should be a modular tool for this
> common task (like the start-stop-daemon or debhelper tools).
>
> One problem I can't resolve is what it should do on test-boolean
> notaboolean. Better than resulting in some default behavior (true or
> false) on invalid input, would be resulting in an exception. However,
> my foreseen application is in a shell script, the if statement of which
> (AFAIK) won't result in exceptions (an error is failure is false).
>
> Any suggestions?


Why do you need a tool? A simple shell function like the following is enough:

test_boolean() {
case $1 in
"true"|"True"|"TRUE"|"T"|"yes"|"Yes"|"YES"|"1") return 0 ;;
"false"|"False"|"FALSE"|"F"|"no"|"No"|"NO"|"0") return 1 ;;
*) return 2 ;;
esac
}

With this function you can test the exit status if you need to distinguish
between "true" ($? after the function call is 0), "false" ($? equals 1) or
"unknown" ($? equals 2) or you can use it as always if you only care about
"true":

if test_boolean $X; then
echo "True"
else
echo "False or Unknown"
fi

Greetings,

Sergio.

--
Sergio Talens-Oliag <sto@uv.es> <http://www.uv.es/~sto/>
Key fingerprint = 29DF 544F 1BD9 548C 8F15 86EF 6770 052B B8C1 FA69

Martin Dickopp

2004-07-31, 2:47 am

ms419@freezone.co.uk writes:

> Is there a tool to test strings as boolean values? For instance,
> test-booean true, True, TRUE, T, yes, on, 1, succeed, but test-boolean
> false, False, FALSE, F, no, off, 0, fail?


I'm not aware of any such tool.

> One problem I can't resolve is what it should do on test-boolean
> notaboolean. Better than resulting in some default behavior (true or
> false) on invalid input, would be resulting in an exception. However, my
> foreseen application is in a shell script, the if statement of which
> (AFAIK) won't result in exceptions (an error is failure is false).


Like other tools which return one of three possible states (e.g. grep:
match, no match, error), your tool could return 0 (true), 1 (false), or
2 (error). It should also print an error message in the latter case.

It might also be a good idea to have command line options to treat
unrecognized input as either true or false, either with or without a
warning message being printed.

Martin


--
To UNSUBSCRIBE, email to debian-devel-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com