Reg expr question

Reg expr question

Post by Rupen She » Sun, 18 Jul 1993 07:49:46



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"
        fi

This test doesn't quite work right.
Please help.
Thank you.
--

 
 
 

Reg expr question

Post by David W. Tamk » Mon, 19 Jul 1993 06:12:32




| 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"
|       fi
|
| 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 ;;
esac

David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297


 
 
 

Reg expr question

Post by Nancy McGou » Tue, 20 Jul 1993 02:55:31




>| 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.

One possibility is to use:

  grep '[^-_0-9A-Za-z]'

If it finds a match (return code 0) in `echo $VAR` then $VAR contains
something other than the allowable characters.
QUESTION:  How do you test return codes in a sh script?

I actually like David W. Tamkin's answer better though...

Quote:>case "$VAR" in # the quotes are probably unnecessary
>*[!A-Za-z0-9_-]*) echo Test failed ;;
>esac

Thanks,
 Nancy
 
 
 

Reg expr question

Post by David W. Tamk » Tue, 20 Jul 1993 03:13:27




| QUESTION:  How do you test return codes in a sh script?

$? contains the latest return code.  You can save it in a variable if you
will be clobbering it with something that will have its own return code
before you use it.

| I actually like David W. Tamkin's answer better though...

Thank you.

David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297

 
 
 

Reg expr question

Post by Donn Ca » Wed, 21 Jul 1993 02:34:10



| 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 ;;
| esac

The quotes are indeed unnecessary after "case", but the "-" in the pattern
usually does need to be quoted.  Many Bourne shell implementations don't
do anything useful with a trailing "-" in a character set.  Try the above
with AIX 3 sh, or Ultrix sh5, then escape "-" as shown below and try that.
(Don't bother with Ultrix "sh", it doesn't handle complements.)  Without
the quote, AIX sh and Ultrix sh5 have opposite results, but neither do the
desired thing.

case $VAR in
*[!A-Za-z0-9_\-]*) echo Test failed ;;
esac

        Donn Cave, University Computing Services, University of Washington

 
 
 

1. Help with Reg Expr

Hi all,

I'm a novice with Reg Exprs: please help with the following:

I want all the lines not starting with /home to be changed to /go-to-error-page
using sed:

sed -e 's/<reg expr?>/<...>/g'

Thank you very much.

Sara

2. I need a cheap unix machine

3. expr question

4. Penetration Testing

5. bash: "expr index" question

6. Racoon ---> NetScreen (Help PLZ).

7. rc zerofill function (was Re: rc and expr question)

8. PPP upload/download speeds

9. Substitute/reg-exp question

10. a simple question reg drivers

11. Question reg. Solaris threads

12. Awk with reg. exp. question

13. Questions reg. SETUP and GCC 2.7.0