error validating url from youtube in bash
Solution 1:
Inside [[ ]]
=~
is not a negated =
.
=~
is an operator that allows you to match against a regular expression, while =
has nothing to do with regular expressions. See Conditional Constructs in Bash, [[
with its =~
is described there. The most important fragment:
An additional binary operator,
=~
, is available, with the same precedence as==
and!=
. When it is used, the string to the right of the operator is considered a POSIX extended regular expression and matched accordingly […].
The test succeeds iff the regex matches. The flow of your script (echo "not valid"
upon a match) makes me believe you think it's the other way around. You need to negate the test.
To negate your test, place !
before the [[
:
if ! [[ "$URL" =~ $regex ]]; then
Alternatively you can negate the expression inside the [[ ]]
:
if [[ ! "$URL" =~ $regex ]]; then
Additionally please read Understanding IFS= read -r line
and adjust your read
command maybe.