Reputation: 45
I tried follow but it can't work:
condition="^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$"
if [[ $1 =~ condition ]]
then
echo fine
else
echo bad
fi
It's used to determine whether the first param is valid.
Upvotes: 1
Views: 1598
Reputation: 627103
You can use
^(https?://)?[[:alnum:]_.-]+(\.[[:alnum:]_.-]+)+[][[:alnum:]_.~:/?#@!$&'*+,;=.-]+$
This means:
(?:
need to be replaced with (
\w
might not be supported in all environments, it can be replaced with [[:alnum:]_]
pattern-
, ]
and ^
need to follow "smart placement" rule (]
should be at the start of the bracket expression and -
must be at the end).See an online demo:
#!/bin/bash
url='http://www.blah.com'
condition='^(https?://)?[[:alnum:]_.-]+(\.[[:alnum:]_.-]+)+[][[:alnum:]._~:/?#@!$&'"'"'*+,;=.-]+$'
if [[ "$url" =~ $condition ]]
then
echo fine
else
echo bad
fi
Output: fine
Note:
$1
in your script should be quoted, "$1"
condition
must be prepended with $
Upvotes: 1