On Fri, 21 Apr 2000, rpjday wrote:
> On Thu, 20 Apr 2000, Steven W. Orr wrote:
>
> > Not legal bashsyntax unless you're testing for a filename to be globbed.
sometimes, i can be pretty clueless. since the original poster was asking
how to recognize a variable being equal to either "Y" or "y", it's obvious
he's checking for the response to a yes/no question. enter the case
construct:
#!/bin/bash
echo "you wanna?"
read WORD dummy
case $WORD in
[Yy]*) echo "yes" ;;
[Nn]*) echo "no" ;;
*) echo "huh?"
esac
done
this allows a first letter of y/Y to mean yes, first letter of n/N to
mean no, and anything else to mean ... whatever. customize as you
see fit. or are we getting away from the simplicity of the basic
if construct?
even better, if you plan on asking lots of y/n questions, wrap
all the work in a simple shell function called getyn, as follows:
---------------------
#!/bin/bash
function getyn {
while echo "$1" >&2 ; do
read ANS dummy
case $ANS in
[Yy]*) return 0 ;;
[Nn]*) return 1 ;;
*) echo "Invalid response, try again ..." >&2
esac
done
}
if getyn "Are we having fun yet?" ; then
echo "great"
else
echo "what's your problem?"
fi
---------------------
pass to the "getyn" function the question to ask, and get back
a return code 0 (true) or 1 (false). the getyn function does all
the work of validating the response. just my $.02.
rday
--
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.