Check whether chars other than those in specified range are in a string

Check whether chars other than those in specified range are in a string

Post by Khanh David To Tu » Thu, 07 Nov 2002 22:28:35



I know this is a very simple task.
And yes, I have used the google search on that topic, but I still dont
get it to work.

I want to check a string by a function, whether that string is a valid
domain adress, assuming, that those consist of 3 parts, which are
divided by dots. The last part may not contain numbers, the other
party may not be empty or have chars other than
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.
I tried

is_domain() {
for i in `echo $* | sed -e 's/\(.\)/\1 /g`
do
case i in [!0-9a-zA-Z]) return 1;;
esac
done
oldifs=$IFS
IFS=.
set "$*"
IFS=$oldifs
for i in `echo $3 | sed -e 's/\(.\)/\1 /g`
do
case i in [a-zA-Z]) return 1;;
esac
done

if is_domain xxx.yyy.xxx
then
...
fi

But it always returns true, even with IPs.
I suppose something with those brackets is wrong.

 
 
 

Check whether chars other than those in specified range are in a string

Post by Stephane Chazela » Thu, 07 Nov 2002 22:59:19




Quote:> I know this is a very simple task.
> And yes, I have used the google search on that topic, but I still dont
> get it to work.

> I want to check a string by a function, whether that string is a valid
> domain adress, assuming, that those consist of 3 parts, which are
> divided by dots. The last part may not contain numbers, the other
> party may not be empty or have chars other than
> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.

[...]

Note that it is not the way we usually define a domain name.
Anyway, what about:

is_domain() {
  (
    IFS=.
    set -- $1
    [ $# -eq 3 ] || return 1
    case $1 in
      ""|*[!a-zA-Z0-9-]*|[0-9-]*|*-) return 1;;
    esac
    case $2 in
      ""|*[!a-zA-Z0-9-]*|[0-9-]*|*-) return 1;;
    esac
    case $3 in
      ""|*[!a-zA-Z]*) return 1;;
    esac
  )

Quote:}

--
Stphane

 
 
 

Check whether chars other than those in specified range are in a string

Post by Robert Kat » Fri, 08 Nov 2002 04:29:58



 > I know this is a very simple task.
 > And yes, I have used the google search on that topic, but I still dont
 > get it to work.
 >
 > I want to check a string by a function, whether that string is a valid
 > domain adress, assuming, that those consist of 3 parts, which are
 > divided by dots. The last part may not contain numbers, the other
 > party may not be empty or have chars other than
 > abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.

[ . . . ]

It's not clear to me what you're requiring of the 3d part.  I think you
want the last part to be just alpha characters.

In the korn shell (and in bash, if you do `shopt -s extglob'), you can do,

function isdomain {
     case $1 in
     +([[:alnum:]]).+([[:alnum:]]).+([[:alpha:]])) return 0;;
     *) return 1;;
     esac

Quote:}

If you want what you've asked for, namely the last part to be anything,
even null, as long as it doesn't contain a *digit* or a *.*, then

function isdomain {
     case  $1 in

     *) return 1;
     esac

Quote:}

--
Regards,

---Robert

 
 
 

Check whether chars other than those in specified range are in a string

Post by Khanh David To Tu » Fri, 08 Nov 2002 21:58:47




>  > I know this is a very simple task.
>  > And yes, I have used the google search on that topic, but I still dont
>  > get it to work.

>  > I want to check a string by a function, whether that string is a valid
>  > domain adress, assuming, that those consist of 3 parts, which are
>  > divided by dots. The last part may not contain numbers, the other
>  > party may not be empty or have chars other than
>  > abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.

> [ . . . ]

> It's not clear to me what you're requiring of the 3d part.  I think you
> want the last part to be just alpha characters.

> In the korn shell (and in bash, if you do `shopt -s extglob'), you can do,

> function isdomain {
>      case $1 in
>      +([[:alnum:]]).+([[:alnum:]]).+([[:alpha:]])) return 0;;
>      *) return 1;;
>      esac
> }

> If you want what you've asked for, namely the last part to be anything,
> even null, as long as it doesn't contain a *digit* or a *.*, then

> function isdomain {
>      case  $1 in

>      *) return 1;
>      esac
> }

Of course I wanted the 3rd part not to be anything, but only letters.
Just a typo with return 1 and return 0.
Is it possible to have that [] check in an if clause?
I tried it, but just messed up my script.
 
 
 

Check whether chars other than those in specified range are in a string

Post by Robert Kat » Sat, 09 Nov 2002 00:58:14



[ . . . ]

> > > I want to check a string by a function, whether that string is a valid
> > > domain adress, assuming, that those consist of 3 parts, which are
> > > divided by dots. The last part may not contain numbers, the other
> > > party may not be empty or have chars other than
> > > abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.

> >[ . . . ]

> >It's not clear to me what you're requiring of the 3d part.  I think you
> >want the last part to be just alpha characters.

> >In the korn shell (and in bash, if you do `shopt -s extglob'), you
> can do,

> >function isdomain {
> >     case $1 in
> >     +([[:alnum:]]).+([[:alnum:]]).+([[:alpha:]])) return 0;;
> >     *) return 1;;
> >     esac
> >}

> >If you want what you've asked for, namely the last part to be anything,
> >even null, as long as it doesn't contain a *digit* or a *.*, then

> >function isdomain {
> >     case  $1 in


Talk about typos, the line above should be


Quote:> >     *) return 1;
> >     esac
> >}

> Of course I wanted the 3rd part not to be anything, but only letters.
> Just a typo with return 1 and return 0.
> Is it possible to have that [] check in an if clause?
> I tried it, but just messed up my script.

Do you mean something like this?

function isdomain {
     if [[ $1 = +([[:alnum:]]).+([[:alnum:]]).+([[:alpha:]]) ]]
     then
         return 0
     else
         return 1
     fi

Quote:}

But why does it have to be an *if* statement?

--
Regards,

---Robert

 
 
 

Check whether chars other than those in specified range are in a string

Post by Stephane Chazela » Sat, 09 Nov 2002 01:06:43


[...]

Quote:> Do you mean something like this?

> function isdomain {
>      if [[ $1 = +([[:alnum:]]).+([[:alnum:]]).+([[:alpha:]]) ]]
>      then
>          return 0
>      else
>          return 1
>      fi
> }

> But why does it have to be an *if* statement?

Yes,

function isdomain {
     [[ $1 = +([[:alnum:]]).+([[:alnum:]]).+([[:alpha:]]) ]]

Quote:}

Should be enough. However note that a domain part normally can't
start with a digit and should only contain ASCII chars (be
carefull to the locale variants for [:al***:])

$ env -i LANG=C ksh -c '[[ = [[:alpha:]] ]]' && echo yes

yes

--
Stphane

 
 
 

Check whether chars other than those in specified range are in a string

Post by Adam Pric » Sat, 09 Nov 2002 02:35:33





> [...]
>> Do you mean something like this?

>> function isdomain {
>>      if [[ $1 = +([[:alnum:]]).+([[:alnum:]]).+([[:alpha:]]) ]]
>>      then
>>          return 0
>>      else
>>          return 1
>>      fi
>> }

>> But why does it have to be an *if* statement?

> Yes,

> function isdomain {
>      [[ $1 = +([[:alnum:]]).+([[:alnum:]]).+([[:alpha:]]) ]]
> }

> Should be enough. However note that a domain part normally can't
> start with a digit

I wish that were true... see www.3com.com for an example (I believe
they lobbied quite hard to ensure this was valid).
Adam
 
 
 

Check whether chars other than those in specified range are in a string

Post by Stephane CHAZELA » Sat, 09 Nov 2002 03:29:59


[...]

Quote:>> However note that a domain part normally can't
>> start with a digit
> I wish that were true... see www.3com.com for an example (I believe
> they lobbied quite hard to ensure this was valid).
> Adam

Yes, RFC 1035 only gives recommandations on the name of domains:

-|The following syntax will result in fewer problems with many
-|applications that use domain names (e.g., mail, TELNET).
-|
-|<domain> ::= <subdomain> | " "
-|
-|<subdomain> ::= <label> | <subdomain> "." <label>
-|
-|<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
-|
-|<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
-|
-|<let-dig-hyp> ::= <let-dig> | "-"
-|
-|<let-dig> ::= <letter> | <digit>
-|
-|<letter> ::= any one of the 52 alphabetic characters A through Z in
-|upper case and a through z in lower case
-|
-|<digit> ::= any one of the ten digits 0 through 9

So, to check a domain name against that recommandation, we could write:

is_domain() {
  (
    IFS=.
    for i in $1; do
      case $i in
        ""|*[!a-zA-Z0-9-]*|[!a-zA-Z]*|*-) return 1;;
      esac
    done
  )

Quote:}

--
Stphane
 
 
 

1. ksh - checking a string for leading # char

Hi,

I'm working on a flexlm license checker to notify people when their
license is about to expire.  I read in a list of license files and I
would like to be able to comment out a file with a # symbol in front
of the file name.  Right now I'm doing it with a temp file and grep
but I would think there is a better way with string operations in
ksh.  What I need to do is check for a leading # character but I'm
not sure how to do so.  

Here is the current code I'd like to fix:

#!/bin/ksh
#
#       Read license files and check dates for expiration
#
input=/system/license.files     # file containing list of license files

exit_stat=0
node=`hostname`
echo "Subject: License Check on Node " $node

# strip out comment lines in license.files
# (bad design as drops any line with a # anywhere on it)

grep -v "#" $input > /tmp/lic_files

for file in $(< /tmp/lic_files)
do
        echo ""
        echo "Testing license file " $file

        if ! nawk -f /system/licdate.awk $file
        then
                exit_stat=1
        fi

done

rm /tmp/lic_files
exit $exit_stat

--------------
Rick Caldwell

2. How do I set up ramdisk

3. checking wether a string has non-whitespace chars

4. HCL for linux 7.x

5. In search of Solaris 2.5 Netscape Navigator 3.0 plug ins

6. modules (net-pf-4 & net-pf-5)

7. Need HELP to Log User Log-ins form the internet

8. X-Server for Avance Logic cards.

9. Suse 7.0; Yast2, kinternet, rc.dialout und ich komme nicht ins Internet

10. Netscape plug-ins on AIX 4

11. what is a INS server?

12. dial ins

13. plug-ins netscape