Check for special character

Check for special character

Post by Smit » Fri, 29 Oct 2004 16:04:00



hi,
My script accept user input into a variable USRIP.
I only want input to have (aA-zZ or 0-9).
How do I check that the variable USRIP doesn't has special character (e.g

Thanks and regards

 
 
 

Check for special character

Post by Alexis Huxle » Fri, 29 Oct 2004 16:49:05


> My script accept user input into a variable USRIP.
> I only want input to have (aA-zZ or 0-9).
> How do I check that the variable USRIP doesn't has special character (e.g


How about:

        if ! expr "$USRIP" : '[a-zA-Z0-9]*$' > /dev/null; then
                echo "bad input"
                return 1                # or exit or whatever
        fi

Repeat the square-bracketed term to specify at least one
occurence of one of those chars if empty input is not
acceptable.

Alexis

 
 
 

Check for special character

Post by Stephane CHAZELA » Fri, 29 Oct 2004 17:01:11


2004-10-28, 15:04(+08), Smith:
> My script accept user input into a variable USRIP.
> I only want input to have (aA-zZ or 0-9).
> How do I check that the variable USRIP doesn't has special character (e.g


[...]

case $USRIP in
 *[!a-zA-Z0-9]*) echo >&2 invalid characters;;
 *) echo OK;;
esac

--
Stephane

 
 
 

Check for special character

Post by Stephane CHAZELA » Fri, 29 Oct 2004 17:08:23


2004-10-28, 09:49(+02), Alexis Huxley:
[...]
Quote:> How about:

>    if ! expr "$USRIP" : '[a-zA-Z0-9]*$' > /dev/null; then
>            echo "bad input"
>            return 1                # or exit or whatever
>    fi

[...]

Note that the ! operator is not Bourne even if POSIX.

Try the above with $USRIP as "(", "="...

Should be:

! expr "_$USRIP" : '_[a-zA-Z0-9]*$'

or more portably:

expr \( "_$USRIP" : '_[a-zA-Z0-9]*$' \) = 0

--
Stephane

 
 
 

Check for special character

Post by Alan Conno » Fri, 29 Oct 2004 17:21:39




> hi, My script accept user input into a variable USRIP.  I only
> want input to have (aA-zZ or 0-9).  How do I check that the

> etc) and only has aA-zZ and 0-9?

> Thanks and regards

Here's one way, Smith:

(assuming bash or the like)

echo $USRIP | if grep '[^a-zA-Z0-9]' ; then do <whatever>
else <whatever>; fi

AC

 
 
 

Check for special character

Post by dfre.. » Sat, 30 Oct 2004 02:16:53



> 2004-10-28, 15:04(+08), Smith:
> > My script accept user input into a variable USRIP.
> > I only want input to have (aA-zZ or 0-9).
> > How do I check that the variable USRIP doesn't has special
character (e.g

> [...]

> case $USRIP in
>  *[!a-zA-Z0-9]*) echo >&2 invalid characters;;
>  *) echo OK;;
> esac

Same concept, opposite logic, using an "if" statement and pattern
operator:

#!/usr/bin/ksh93
if [[ "_${USRIP}" = _+([A-Za-z0-9]) ]]
then
print "contains only desired chars"
else
print -u 2 "contains invalid chars"
fi

And in a "case" statement:

#!/usr/bin/ksh93
case "${USRIP}" in
+([A-Za-z0-9])) print "contains only desired chars";;
*) print -u 2 "contains invalid chars";;
esac

--

Mt Xia Technical Consulting Group   http://www.mtxia.com
100% Spam Free Email              http://www.ridmail.com
MicroEmacs                      http://uemacs.tripod.com
Korn Shell Web     http://dfrench.tripod.com/kshweb.html

 
 
 

Check for special character

Post by William Par » Sat, 30 Oct 2004 04:17:51



> hi,
> My script accept user input into a variable USRIP.
> I only want input to have (aA-zZ or 0-9).
> How do I check that the variable USRIP doesn't has special character (e.g


Hint:
    glob
        [!A-Za-z0-9]
        [[:alnum:]]

    regex
        [^A-Za-z0-9]
        [[:alnum:]]

--

Open Geometry Consulting, Toronto, Canada

 
 
 

Check for special character

Post by Stephane CHAZELA » Sat, 30 Oct 2004 15:50:29



[...]

Quote:> #!/usr/bin/ksh93
> if [[ "_${USRIP}" = _+([A-Za-z0-9]) ]]

Why do you use the quotes and the "_". The [[ ... ]] was
introduced especially to avoid the need for that. It doesn't
harm, though.

[[ $USRIP = +(A-Za-z0-9) ]]

should be enough.

Quote:> then
> print "contains only desired chars"
> else
> print -u 2 "contains invalid chars"

print -u 2 "contains invalid chars or is empty"

or use [[ $USRIP = *(A-Za-z0-9) ]]

--
Stephane

 
 
 

Check for special character

Post by dfre.. » Sat, 30 Oct 2004 22:28:11




> [...]
> > #!/usr/bin/ksh93
> > if [[ "_${USRIP}" = _+([A-Za-z0-9]) ]]

> Why do you use the quotes and the "_". The [[ ... ]] was
> introduced especially to avoid the need for that. It doesn't
> harm, though.

Years of bourne shell programming, now if I don't include the "_" it
just doesn't look right.

--
Dana French

 
 
 

Check for special character

Post by dfre.. » Sun, 31 Oct 2004 01:18:41




> [...]
> > #!/usr/bin/ksh93
> > if [[ "_${USRIP}" = _+([A-Za-z0-9]) ]]

> Why do you use the quotes and the "_". The [[ ... ]] was
> introduced especially to avoid the need for that. It doesn't
> harm, though.

> [[ $USRIP = +(A-Za-z0-9) ]]

> should be enough.

Actually, it probably should be changed to:

if [[ "${USRIP}" == +([[:alnum:]]) ]]

--

Mt Xia Technical Consulting Group   http://www.mtxia.com
100% Spam Free Email              http://www.ridmail.com
MicroEmacs                      http://uemacs.tripod.com
Korn Shell Web     http://dfrench.tripod.com/kshweb.html

 
 
 

1. Problem with a non-special "special" character

Problem is in bourne shell, under SunOS 4.1.3. a call to a program

every time. If I rearrange the input parameters and remove this one
character, everything is fine. None of my shell documentation lists

an example:



just with "no data". I checked and the only character that I need to

Does any one have a clue

2. QUOTA!

3. \\\ Professional Images Virtual Domains/// Special Special Special Special

4. solaris doors

5. Display of special character

6. XFree86-3.1.2B

7. qt hexviewer and special characters

8. Losing It

9. tcsh files with special characters

10. DOS versus UNIX sed question on special characters

11. parentheses as special characters

12. How to Print Special Character in Unix ???

13. sed with special characters