The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
test a string... Kaminski UNIX for Dummies Questions & Answers 2 02-12-2008 05:37 AM
With Regex Spliting the string into Alphanumeric and Numeric part ozgurgul Shell Programming and Scripting 1 06-30-2007 09:52 AM
AlphaNumeric String Operations lakshmikanth UNIX for Dummies Questions & Answers 3 01-05-2007 06:55 AM
string test? hedrict UNIX for Dummies Questions & Answers 9 03-29-2002 03:57 PM
matching alphanumeric string sskb Shell Programming and Scripting 4 12-12-2001 10:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-15-2007
tugger tugger is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 9
Smile ksh - test if string contains alphanumeric...

Okay I will let users input spaces as well

I am having a mental block. I have done a couple of searches but havent found anything that I understand (the likes of :alpha: and awk).

Basically I want to give the user an option to enter some text which will go down as a field within a flat file ( which is delimeted by : ).

I want to limit this to 50 characters (I believe I can use typeset -z50 for this)

I also just want a-z,A-Z,(spaces) and numbers. Whats the best way to validate the input?
  #2 (permalink)  
Old 10-15-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
one way is to use tr to remove unwanted character types. -dc removes everything except those char types you specify. [:alnum;] is alphanumeric, so if you check the length of the string it should be unchanged if it contains only the char types you want.
Code:
# t is the input string, ck is a variable to check the contents of t
t="thisisa555ctest"

ck=$( echo "$t" | tr -dc '[:alnum:]')
if [[ ${#t} -eq ${#ck} ]]; then
      echo "ok"
else
      echo "not ok"
      exit 1
fi
# t is all good chars at this point
# check length of t

if [[ ${#t} -gt 50 ]]; then
      echo "not ok too long"
fi
  #3 (permalink)  
Old 10-16-2007
tugger tugger is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 9
Jim,

Thanks for the response. Having tested the code thi doesn;t allow me to have spaces within the string. Any ideas?

Also what does the # mean within ${#t}?

Thank you
  #4 (permalink)  
Old 10-16-2007
tugger tugger is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 9
Think I've cracked it

I am using this code. The tests I have done appear to work.

while true
do
clear
echo "Enter Text: \c"
read t
case $t in
+([a-z]|[A-Z]|[0-9]|[' ']))
echo test string is okay
break
;;
*)
echo test string not okay
sleep 2
;;
esac
done
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0