BASH regex help

Chris F.A. Johnson cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Fri Apr 21 16:11:04 UTC 2006


On Fri, 21 Apr 2006, Neil Watson wrote:

> I am trying to parse a string in BASH. I have two goals. First, exit if the
> string contains anything other than 0-9, a-z or '.'. Second escape all '.' to
> '\.'.
>
>        # Check for a proper url
>        if [[ $DOMAIN = [^a-b0-9\.] ]] ; then
>                echo "Invalid URL  a-b, 0-9 and . allowed only."
>                exit 1
>        fi
>
>        # We must escape special characters
>        $DOMAIN = ${DOMAIN//\./\\./}
>
>
> Alas, my code does not work as I had hoped. The first test always passes even
> if DOMAIN contains other characters. The second test tries to open a file. It
> returns the error "No such file or directory."

    Do you mean a-b or a-z? No capital letters?

LC_COLLATE=C
case $DOMAIN in
      *[!a-z0-9.]*) echo "Invalid URL:  a-z, 0-9 and . allowed only."
                    exit 1 ;;
esac
DOMAIN = ${DOMAIN//./\\.}

-- 
    Chris F.A. Johnson                      <http://cfaj.freeshell.org>
    ===================================================================
    Author:
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list