Nilsen Chris Nilsen Chris - 15 days ago 7
Bash Question

How do i check if the $# parameter with URL starts with http/https and ends with jpg/jpeg?

i found this earlier but how do i check if my url starts with http/https and ends with jpg/jpeg

i need too check:

starts with: http https

ends with: jpg jpeg JPG JPEG

code:

`if [[ $1 = http?(s)://*.jpg ]];
then
echo "invalid URL"
fi`

Answer

You can use this BASH regex:

[[ ${1,,} =~ ^https?://.+\.jpe?g$ ]]
  • ${1,,} is to convert $1 to all lowercase
  • regex checks if $1 has http:// or https:// at start and it ends with jpg or jpeg
Comments