regex - Bash Shell help regular expressions not working on password check -


i trying make regular expression work password checker script reason not accepting it. trying require following characters, minimum of: 2 upper case letters, 2 lower case letters, minimum 2 numbers , minimum 2 special characters. special characters looking towards "!@" , it. here script,

#!/bin/bash read -s -p "enter password: " password password_length=${#password} if [ $password_length -lt 8 -o $password_length -gt 20 ] ;then         echo -e "invalid password - should between 8 , 20 characters in length.";         echo ;     else         # check invalid characters         case $password in             *[^a-za-z0-9]* )                 echo -e "password contains invalid characters.";                 echo ;                 ;;             * )                 echo "password accepted.";                 echo;;         esac fi 

i don't have answer. @ moment i'm playing around using grep has far more powerful regex engine.

something this:

#!/usr/bin/env bash  while true;     echo "password must contain 2 upper case characters , 2 numbers"     read -s -p "enter password: " password     echo      fail=no      # mininum 8 characters     [[ ${#password} -ge 8 ]] || fail=yes      # must contain 2 upper case letters     echo $password | grep -q "[a-z].*[a-z]" || fail=yes      # must contain 2 digits     echo $password | grep -q "[0-9].*[0-9]" || fail=yes      # must contain 1 non-alphanumeric character (no spaces)     echo $password | grep -q "[^a-za-z0-9 ]" || fail=yes      [[ ${fail} == "no" ]] && break      echo "password invalid" done  echo "password $password valid" 

anyway, test possible password against each requirement separately, it's easy lost in regular expressions, , might miss edge cases.


Comments