Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

test so $ANS must start with a numeric or alpha

 
SOLVED
Go to solution
Highlighted
Super Advisor

test so $ANS must start with a numeric or alpha

Hi all,

I want to write a test so that the user must enter a numeric or alpha character:



echo "Please enter your name before you login: \c"

while read ANS
do

if [[ ${ANS} = "^[0-9][aA-Zz]" ]] ; then

do something

fi

done

this doesn't seem to work - I want a value to be entered to ANS

how can I make this loop until ANS = something

Thanks

Chris
hello
11 REPLIES 11
Highlighted
Honored Contributor
Solution

Re: test so $ANS must start with a numeric or alpha

while read ANS
do
ANS2="${ANS#[0-9a-zA-Z]}"
if [ "${ANS}" == "${ANS2}" ]
then
echo not valid try again
continue
fi
break
done
Highlighted
Honored Contributor

Re: test so $ANS must start with a numeric or alpha

Hi,

variable cannot start with numeric..
Best wishes,

Ganesh.
Highlighted
Honored Contributor

Re: test so $ANS must start with a numeric or alpha

read ANS;
test="-1";
while [ "$test" = '-1' ]
do
test="1";
case $ANS in
y | Y ) answer="y";;
n | N ) answer="n";;
*) finish="-1";
echo -n 'Invalid response -- please reenter:';
read answer;;
esac
done
~
Highlighted
Honored Contributor

Re: test so $ANS must start with a numeric or alpha

/*sorry should have been 'test' and not 'finish' with case statement */

read ANS;
test="-1";
while [ "$test" = '-1' ]
do
test="1";
case $ANS in
y | Y ) answer="y";;
n | N ) answer="n";;
*) test="-1";
echo -n 'Invalid response -- please reenter:';
read answer;;
esac
done
Highlighted
Acclaimed Contributor

Re: test so $ANS must start with a numeric or alpha

Hi Chris:

I like to do this like:

#!/usr/bin/sh
#!/usr/bin/sh
typeset -l ANS
while true
do
echo "Enter y|n \c"
read ANS
case ${ANS} in
"y"|"yes" )
echo "OK"
break
;;
"n"|"no" )
echo "Thanks anyway..."
break
;;
* )
echo "please say y|n"
esac
done
echo "...and the answer was '${ANS}'"

...the 'typeset -l' downcases every response making the tests very simple, especially if you demanded that your user reply "yes" or "No" instead of a one-character response. As written, mix-case responses are easily digested too.


Regards!

...JRF...
Highlighted
Acclaimed Contributor

Re: test so $ANS must start with a numeric or alpha

hi (again) Chris:

Sorry, I read your "...how can I make this loop until ANS = something..." and ignored the subject line request for handling numeric or alpha input. Using the orgiginal suggestion of mine, amended:

#!/usr/bin/sh
typeset -l ANS
while true
do
echo "Enter y|n \c"
read ANS
case ${ANS} in
"y"|"yes" )
echo "OK"
break
;;
"n"|"no" )
echo "Thanks anyway..."
break
;;
[0-9]* )
echo "digits ok too"
break
;;
* )
echo "please say y|n|[0-9]*"
esac
done
echo "...and the answer was '${ANS}'"

/* no additional points necessary for this correction */

Regards!

...JRF...
Highlighted
Acclaimed Contributor

Re: test so $ANS must start with a numeric or alpha

>if [[ ${ANS} = "^[0-9][aA-Zz]" ]] ; then

The string compare test takes a pattern:
if [[ ${ANS} = [0-9a-zA-Z]* ]]; then

The "*" says ignore the rest of the string. You could use Laurent's trick for looking at each char. (Note there is no == operator in [ ... ].

Or you could just use grep to test:
echo ${ANS} | grep -q "^[0-9a-zA-Z]*$"
if [ $? -eq 0 ]; then
Highlighted
Acclaimed Contributor

Re: test so $ANS must start with a numeric or alpha

This pattern may work:
if [[ ${ANS} = +([0-9a-zA-Z]) ]]; then

>JRF: [0-9]* )

case takes a pattern not a RE. So that would be: +([0-9])
Highlighted
Honored Contributor

Re: test so $ANS must start with a numeric or alpha

indeed I followed the title constraint. not the text one,
and indeed == is a typo.