0

im racking my brain and don't know why i cant compare regex to my string here is what i have so far, am i not doing this right?

#!/bin/bash
menu=0
re='^([a-zA-Z+]+[0-9+]+[&@!#+]+)$'
while [ "$menu" -eq "0" ]; do
read -p "Enter Your Password: " PASS
if [[ ! $PASS =~ $re ]]; then
echo "must contain at least 1 uppercase, one lowercase, one number, and a special character"
else
echo "strong password"
menu=1
fi
done
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I have used all these guides but they arent working in bash for somereason – Eric Bryant Mar 06 '17 at 20:48
  • 1
    I tested your script and the matching `=~` seems to work just fine. Maybe your regex is not what you actually want: `aA9&` is accepted, but `&9Aa` is not accepted (wrong order). It would be easier to make four checks: `... =~ [a-z] ... =~ [A-Z] ... =~ [0-9] ... =~ [&@!]` – Socowi Mar 06 '17 at 21:20
  • Your regex is requiring letters followed by numbers followed by special characters, so it will fail if the user has them in a different order. The question that this is a duplicate of has an answer using readaheads (?=) to make it work properly. Try `^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[&@!#+])[A-Za-z0-9&@!#+]+$` – Roger McCoy Mar 08 '17 at 03:46
  • awesome thanks!, worked flawlessly, yeah that was my problem it only chekced in that order – Eric Bryant Mar 08 '17 at 08:16

0 Answers0