Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for valid link (URL)

I was reading though this other question which has some really good regex's for the job but as far as I can see non of them work with BASH commands as BASH commands don't support such complex rexeg's.

if echo "http://www.google.com/test/link.php" | grep -q '(https?|ftp|file)://[-A-Z0-9\+&@#/%?=~_|!:,.;]*[-A-Z0-9\+&@#/%=~_|]'; then 
    echo "Link valid"
else
    echo "Link not valid"
fi

But this doesn't work as grep -q doesn't work ...

Edit, ok I just realised that grep had an "extended-regex" (-E) option which seems to make it work. But if anyone has a better/faster way I would still love to here about it.

like image 698
Mint Avatar asked Jul 06 '10 04:07

Mint


People also ask

What is a valid site URL?

A URL is a valid URL if at least one of the following conditions holds: The URL is a valid URI reference [RFC3986]. The URL is a valid IRI reference and it has no query component. [RFC3987] The URL is a valid IRI reference and its query component contains no unescaped non-ASCII characters.


1 Answers

Since I don't have enough rep to comment above, I am going to amend the answer given by Dennis above with this one.

I incorporated Christopher's update to the regex and then added more to it so that the URL has to at least be in this format:

http://w.w (has to have a period in it).

And tweaked output a bit :)

regex='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]\.[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$'

url='http://www.google.com/test/link.php'
if [[ $url =~ $regex ]]
then 
    echo "$url IS valid"
else
    echo "$url IS NOT valid"
fi
like image 176
Patrick Steil Avatar answered Sep 18 '22 14:09

Patrick Steil