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
Thanks and regards
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
case $USRIP in
*[!a-zA-Z0-9]*) echo >&2 invalid characters;;
*) echo OK;;
esac
--
Stephane
[...]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
> Thanks and regards
(assuming bash or the like)
echo $USRIP | if grep '[^a-zA-Z0-9]' ; then do <whatever>
else <whatever>; fi
AC
> case $USRIP in
> *[!a-zA-Z0-9]*) echo >&2 invalid characters;;
> *) echo OK;;
> esac
#!/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
regex
[^A-Za-z0-9]
[[:alnum:]]
--
Open Geometry Consulting, Toronto, Canada
Why do you use the quotes and the "_". The [[ ... ]] wasQuote:> #!/usr/bin/ksh93
> if [[ "_${USRIP}" = _+([A-Za-z0-9]) ]]
[[ $USRIP = +(A-Za-z0-9) ]]
should be enough.
print -u 2 "contains invalid chars or is empty"Quote:> then
> print "contains only desired chars"
> else
> print -u 2 "contains invalid chars"
or use [[ $USRIP = *(A-Za-z0-9) ]]
--
Stephane
> Why do you use the quotes and the "_". The [[ ... ]] was
> introduced especially to avoid the need for that. It doesn't
> harm, though.
--
Dana French
> 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.
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
5. Display of special character
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