Friday, May 27, 2022

[SOLVED] Verification of http:// or https:// in an argument with grep in BASH

Issue

Input = ./q4.sh https://cdn.eso.org/images/thumb700x/eso1723a.jpg

echo $1 | -Eiq '^https?://' 2> /dev/null || echo 'Ce script supporte seulement les URLs commencant par https:// ou http://' && exit 1

output always jump to the last || echo 'Ce script supporte seulement les URLs commencant par https:// ou http://'

even if my argument 1 have http:// or https://


Solution

As an alternative to grep or even bash's regular expression matching, you can use pattern matching.

if [[ $1 != http?(s)://* ]]; then


Answered By - chepner
Answer Checked By - Katrina (WPSolving Volunteer)