Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

always giving password not strong enough message (else condition is running)

Tags:

bash

I have been working on this function in shell scripting to take up a password from a user and check if it satisfies the criteria of being an effective password but it always says password not strong enough. The password I'm trying to use is LK@12abc.

Here is my code:

function paq()
{   

     read -p "PASSWORD :-" password

     pasq="^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$"

    if [[ $password =~ $pasq ]]; then
          echo "Valid password"
          echo "The password is:- $password" >> user1.txt
          echo "$password" >>password.txt
    else
          echo "password not strong enough"
    fi
}
paq
like image 906
Somil Rastogi Avatar asked Jan 24 '26 21:01

Somil Rastogi


1 Answers

It appears your password has to have:

  • 3 lower case letters
  • 2 upper case letters
  • 2 digits
  • 1 punctuation character
  • exactly 8 characters long

So, with bash glob patterns:

if [[ $password == *[a-z]*[a-z]*[a-z]* ]] &&
   [[ $password == *[A-Z]*[A-Z]* ]] &&
   [[ $password == *[0-9]*[0-9]* ]] &&
   [[ $password == *[!@#$\&*]* ]] &&
   (( ${#password} == 8 ))
then
    echo password OK
else
    echo password does not satisfy criteria
fi

I'm surprised that I need to escape the & in the 4th test

like image 78
glenn jackman Avatar answered Jan 26 '26 13:01

glenn jackman