The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 10-15-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
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