This widget could not be displayed.
Operating System - HP-UX
1845517 Members
3682 Online
110244 Solutions
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
New Discussion
This widget could not be displayed.
This widget could not be displayed.

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

 
SOLVED
Go to solution
lawrenzo_1
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
Laurent Menase
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
Ganesan R
Honored Contributor

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

Hi,

variable cannot start with numeric..
Best wishes,

Ganesh.
T G Manikandan
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
~
T G Manikandan
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
James R. Ferguson
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...
James R. Ferguson
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...
Dennis Handly
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
Dennis Handly
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])
Laurent Menase
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.


Dennis Handly
Acclaimed Contributor

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

Yes, this pattern does work for all alpha or numeric:
if [[ ${ANS} = +([0-9a-zA-Z]) ]]; then
lawrenzo_1
Super Advisor

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

ok thanks everyone ...

I'll play around and see what I come up with from your suggestions however all good examples!

Chris.
hello