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.

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.

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

${#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".

Thanks, for all the ideas. These are some great examples, I'm sure I can work one of them into my script.

thanks again.