[Gllug] Re: Bash Data validation libraries
Tethys
sta296 at astradyne.co.uk
Tue Feb 14 16:45:08 UTC 2006
Richard Jones writes:
>For instance, a naive check of IP addresses might be:
>
>#!/bin/bash -
>
>read ip
>if [[ ! $ip =~ ^[1-9][0-9]?[0-9]?\.[1-9][0-9]?[0-9]?\.[1-9][0-9]?[0-9]?\.[1-9]
>\[0-9]?[0-9]?$ ]]; then
> echo "invalid IP address"
>fi
That's a *very* naive check (it's also a bashism, and won't work in other
shells). BTW, your regexp could be simplified to:
^[1-9][0-9]{,2}(\.[1-9][0-9]{,2}){3}$
but it's still wrong because it allows 555.666.777.888 as an IP address.
More worryingly, it also doesn't accept 192.168.0.1 as being a valid IP.
You *could* do it with just regular expressions, albeit somewhat complex
ones, but it would be better to just verify that each octet is numeric
and is between 0 and 255.
Tet
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list