I have to complete this script for a beginner BASH class but it seems like without the first line, my script fully execute its task but when I added the http / https verification (FIRST LINE) it don't go through the rest of it. Could you explain to me why? When I remove the quiet option of my grep I see that it work perfectly but it won't continue to read the script until the wget.
grep -Eiq "^https?://" <<<$1 || echo 'Ce script supporte seulement les URLs commencant par https:// ou http://' && exit 1
NOMBRE=`echo "$1" | grep -o '/' 2> /dev/null | wc -l`
NOMBRE=$((NOMBRE 1))
ITEM=`echo "$1" | cut -d '/' -f$NOMBRE 2> /dev/null`
wget -q $1 && echo "Votre fichier a ete telecharge ici: "$PWD/$ITEM
CodePudding user response:
The issue is about precedence. You need to put the error message and the exit within braces. If grep fails it prints the error message, if it succeeds, it exits.
Try this instead:
grep -Eiq "^https?://" <<<$1 || { echo 'Ce script supporte seulement les URLs commencant par https:// ou http://' && exit 1 ; }