| In a sh script, I want to allow only the following characters for
| a particular variable (say $VAR)
| [A-Z] [a-z] [0-9] - and _ (These can occur in any combination)
| Can someone show me an easy way of telling whether $VAR has any
| characters other than the above set.
| I tried doing the following, but it doesn't work.
| # Trying to delete all allowable characters.....
| TEST=`echo $VAR | sed -e "s/[a-zA-Z0-9-_]*//"`
| if [ ! -z "$TEST" ]; then
| echo "Test failed"
| This test doesn't quite work right.
Well, your sed command says "search for a sequence of zero or more of
the listed characters and change it to nothing." sed will find zero or
more of them at the beginning of $VAR and put nothing there. Theoretically
that should substitute nothingness up to the first illegal character, so
if you have only legal characters you should get emptiness back. (I note
thought that your comment says you're trying to delete all allowable
characters, not just those that precede the first illegal character.)
Moreover, a hyphen in brackets is taken literally only if it is first,
last, or escaped (are you sure hyphen belongs in that set anyway?), so
0-9-_ is just going to confuse things: try [a-zA-Z0-9_-] instead.
First, try moving the literal hyphen to the end, and if that doesn't help
try what your comment describes: delete all allowed characters, like this:
TEST=`echo $VAR | sed "s/[a-zA-Z0-9_-]//g"`
But all in all, I see you're using an sh-based shell from the format of your
test command, so you could just do this without forking sed if your shell
honors complemented ranges in pattern matches (remember that here we're using
shell pattern matching rather than a regular expression):
case "$VAR" in # the quotes are probably unnecessary
*[!A-Za-z0-9_-]*) echo Test failed ;;
David W. Tamkin Box 59297 Northtown Station, Illinois 60659-0297