- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: test so $ANS must start with a numeric or alph...
Categories
Company
Local Language
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 07:24 AM
03-31-2009 07:24 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 07:49 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 08:01 AM
03-31-2009 08:01 AM
Re: test so $ANS must start with a numeric or alpha
variable cannot start with numeric..
Ganesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 08:08 AM
03-31-2009 08:08 AM
Re: test so $ANS must start with a numeric or alpha
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
~
- Tags:
- case
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 08:10 AM
03-31-2009 08:10 AM
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";;
*) test="-1";
echo -n 'Invalid response -- please reenter:';
read answer;;
esac
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 08:30 AM
03-31-2009 08:30 AM
Re: test so $ANS must start with a numeric or alpha
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 08:41 AM
03-31-2009 08:41 AM
Re: test so $ANS must start with a numeric or alpha
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 08:42 AM
03-31-2009 08:42 AM
Re: test so $ANS must start with a numeric or alpha
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 08:49 AM
03-31-2009 08:49 AM
Re: test so $ANS must start with a numeric or alpha
if [[ ${ANS} = +([0-9a-zA-Z]) ]]; then
>JRF: [0-9]* )
case takes a pattern not a RE. So that would be: +([0-9])
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 09:20 AM
03-31-2009 09:20 AM
Re: test so $ANS must start with a numeric or alpha
and indeed == is a typo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2009 12:15 PM
03-31-2009 12:15 PM
Re: test so $ANS must start with a numeric or alpha
if [[ ${ANS} = +([0-9a-zA-Z]) ]]; then
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2009 12:39 AM
04-01-2009 12:39 AM
Re: test so $ANS must start with a numeric or alpha
I'll play around and see what I come up with from your suggestions however all good examples!
Chris.