- 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
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- 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.