[Gllug] Yes or no?

Ben Fitzgerald ben_m_f at yahoo.co.uk
Mon Nov 28 12:32:35 UTC 2005


On Mon, Nov 28, 2005 at 12:23:39PM +0000, John Winters wrote:
> On Mon, 2005-11-28 at 11:24 +0000, Ben Fitzgerald wrote:
> > On Mon, Nov 28, 2005 at 09:35:20AM +0000, Jon Dye wrote:
<snip>
> > 
> > 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:
> 
> "This will erase your entire hard disk.  Are you sure you want to
> proceed?  Type "yes" to continue, anything else to abort:"

Okay, case looks good:
[benf at myhost ~]$ cat test.sh
#!/bin/bash

read x

case $x in
[yY][eE][sS]) echo thanks for saying yes;;
[yY]) echo thanks for saying yes;;
*) echo arrrggh;;
esac

[benf at myhost ~]$ ./test.sh 
yes
thanks for saying yes
[benf at myhost ~]$ ./test.sh 
Yes
thanks for saying yes
[benf at myhost ~]$ ./test.sh 
YES
thanks for saying yes
[benf at myhost ~]$ ./test.sh 
Yes-sir
arrrggh

Again, not fiddly, and if you have to reuse this, stick it in a function
and pass the user input as an arg.

Anyway, I'm sure you know all this so I'll just pipe down. You must have
your reasons, I was just suggesting good old read.

Cheers,

Ben.

-- 
Registered Linux user number 339435
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list