The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
testing length astonmartin Shell Programming and Scripting 1 02-09-2008 08:58 PM
Length of an indirect variable gone_bush Shell Programming and Scripting 0 03-28-2006 04:17 PM
Length of a variable karyn1617 Shell Programming and Scripting 3 02-08-2005 03:41 PM
Variable Testing for size jango AIX 4 08-19-2004 07:46 AM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 10:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-09-2008
Registered User
 

Join Date: Jun 2008
Posts: 2
need help testing for length of variable

Hello, I'm new to shell scripting and need a little help please.

I'm working on a script that asks the user to input a name that can be 1 to 12 alphanumeric characters and can have dots(.) dashes(-) and spaces. I want to test that the answer is valid and if not make the user try again. I have no problem doing this with y|n questions or numeric only , but I haven't been able to figure this out.

Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 06-09-2008
awk awk is offline
Registered User
 

Join Date: Feb 2007
Posts: 119
Code:
echo $A
123-ABC.def

echo $A | wc -c
      12

echo $A | sed 's/[[:alnum:].-]//g'| wc -c
       1
Notice that the wc command is counting the EOL character, so you need to adjust for that.

Bsically, the sed command is counting the records after getting rid of all alphanums, periods and dashes. So it should be 1, showing that there are no extraneous characters in the record.
Reply With Quote
  #3  
Old 06-10-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Code:
while true; do
  echo -n "Enter your favorite string: "
  read n
  case $n in
    ?????????????*) echo "Must be less than 12 characters";;
    '') echo "Must not be empty";;
    *[!A-Za-z0-9. -]*) echo "Must only contain letters, numbers, periods, spaces, and dashes";;
    *) break;;
  esac
  echo Try again.
done
The break is what breaks out of the while loop.

You should note that read will parse backslashes in user input. If you don't want that, see if your shell offers read -r
Reply With Quote
  #4  
Old 06-10-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
Code:
${#A}
is the simplest way to get the length of a string. The other methods using regular expressions or POSIX character classes are probably the best way to check "alphabet-ness".
Reply With Quote
  #5  
Old 06-10-2008
Registered User
 

Join Date: Jun 2008
Posts: 2
Thanks, for all the ideas. These are some great examples, I'm sure I can work one of them into my script.

thanks again.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0