[Gllug] Yes or no?

Matthew King matthew.king at monnsta.net
Mon Nov 28 22:20:44 UTC 2005


John Winters <john at sinodun.org.uk> writes:

>> I'd be surprised if someone has written a more efficient program than
>> the following:
>> 
>> read answer
>> [ "$answer" == "yes" ] && do_something_function
>> [ "$answer" != "yes" ] && do_something_else_function
>> 
>> What's wrong with that?
>
> Since you ask, I would expect at the very least for the case-sensitivity
> to be removed and for the code to accept just "y" as well.  The above
> would do only for a question like:

In that case:

[ "${answer:0:1}" = Y -o  "${answer:0:1}" = y ] && true_function || false_function

or

[ "`echo ${answer:0:1} | tr Y y" = y ] && true_function || false_function

Or, to be a bit more verbose (and structured):

if [ "`echo ${answer:0:1} | tr Y y`" = y ];
then
  true_function
else
  false_function
fi

At least I think that's right. I prefer (and use) the first form myself.

And, of course maybe spawning tr is just *too* much extra processing
power...

Matthew

-- 
I must take issue with the term "a mere child," for it has been my
invariable experience that the company of a mere child is infinitely
preferable to that of a mere adult.
                                           --  Fran Lebowitz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 194 bytes
Desc: not available
URL: <http://mailman.lug.org.uk/pipermail/gllug/attachments/20051128/b94c3c46/attachment.pgp>
-------------- next part --------------
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list