If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Bourne scripting - checking user input

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-16-04, 09:03
Chewy Chewy is offline
Registered User
 
Join Date: Aug 2004
Posts: 2
Bourne scripting - checking user input

Hi all,
As part of a Bourne shell script I'm writing, I need to check the user's input to make sure that it meets requirements (in this case, only alphabetic characters).
The way I'm doing it at the moment is reading the variable, then using a case statement to print an error if the input is incorrect.

Code:
read word
case $word in
       [a-zA-Z]+) echo "Valid" ;;
       *) echo "Error" ;;
esac
I'm guessing that the case statement doesn't support the regular expression [a-zA-Z]+ , any suggestions on other ways I could go about it?
Thanks.
Reply With Quote
  #2 (permalink)  
Old 08-16-04, 09:34
SiverSloth SiverSloth is offline
Registered User
 
Join Date: Jun 2004
Posts: 29
A bit awkward but...

Code:
read uname
if [ `expr length $uname` -ne `expr "$uname" : [A-Za-z]` ]
then
  echo "Nasty warning message"
  exit
fi
Reply With Quote
  #3 (permalink)  
Old 08-17-04, 04:51
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Apply some reverse logic!

case $word in
*[!a-zA-Z]*) echo contains nonalphas;;
*)echo contains alphas only;;
esac
Reply With Quote
  #4 (permalink)  
Old 08-17-04, 08:32
Chewy Chewy is offline
Registered User
 
Join Date: Aug 2004
Posts: 2
Thanks a lot Damian, seemed to do the trick.
Reply With Quote
  #5 (permalink)  
Old 08-17-04, 14:24
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally Posted by Chewy
Thanks a lot Damian, seemed to do the trick.
Chewy,

The problem with your initial attempt was that your expression was a 'regular expression' - the shell is only understands 'file expressions', which are completely different!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On