meister
meister

Reputation: 47

Bash check for http or https regex

I'm trying to check if a url starts with http|https and ends with jpg|png. I have searched, but the answers don't work for me.

I have this currently:

if [[ $url = ^https?://.*jpg ]]
then
wget -O webcam.jpg $url
fi

But its fails to wget. What am I doing wrong?

Upvotes: 3

Views: 1658

Answers (1)

chepner
chepner

Reputation: 531808

You have to use the =~ operator for regular-expression matching; = only does pattern matching. The equivalent pattern would be http?(s)://*jpg*. (Recent versions of bash always use extended patterns inside [[ ... ]]; older versions may require they be turned on explicitly with shopt -s extglob.)

(I added the trailing * to the pattern because patterns are anchored to both ends of the string by default, while regular expressions require ^ and $ explicitly. Since you did not have $ at the end of your regular expression, I made the pattern open at the end as well.)

Upvotes: 1

Related Questions