![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| testing length | astonmartin | Shell Programming and Scripting | 1 | 02-09-2008 07: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 02: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 09:09 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
Code:
echo $A
123-ABC.def
echo $A | wc -c
12
echo $A | sed 's/[[:alnum:].-]//g'| wc -c
1
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. |
|
|||
|
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
You should note that read will parse backslashes in user input. If you don't want that, see if your shell offers read -r |
| Tags |
| regex, regular expressions |
| Thread Tools | |
| Display Modes | |
|
|