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
Attachment:
signature.asc
Description: Digital signature