Bash regex for strong password

How can I use the following regex in a BASH script?

(?=^.{8,255}$)((?=.*\d)(?!.*\s)(?=.*[A-Z])(?=.*[a-z]))^.*

I need to check the user input (password) for the following:

  • at least one Metropolitan letter.
  • at least one number.
  • at least one small letter.
  • and the password must be between 8 and 255 characters.
+3
source share
5 answers

If your version grephas a parameter -P, it supports PCRE (regular expressions compatible with Perl).

grep -P '(?=^.{8,255}$)(?=^[^\s]*$)(?=.*\d)(?=.*[A-Z])(?=.*[a-z])'

, , . . ^.* , , :

while ! echo "$password" | grep -P ...
do
    read -r -s -p "Please enter a password: " password
done
+2

, - ( ?) (: ), Bash: grep -Eq, :

if echo "$candidate_password" | grep -Eq "$strong_pw_regex"; then
    echo strong
else
    echo weak
fi

Bash 3 =~:

if [[ "$candidate_password" =~ "$strong_pw_regex" ]]; then
fi

regexp grep -E Bash , , . , , , - Ruby Perl Bash.

, Bash (${#candidate_password} candidate_password), . .

+1

grep -E (ERE)
ERE .

, Perl :

perl -ne 'exit 1 if(/(?=^.{8,255}$)((?=.*\\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*/);exit 0;'

0

, , PAM?

.

0

AND, , - . - , - . , - , :

$echo "tEsTstr1ng" | egrep "^.{8,255}"| egrep "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]"| egrep "[abcdefghijklmnopqrstuvwxyz"] | egrep "[0-9]"

"[AZ]" "[az]", [aAbBcC..., , .

script:

#!/bin/sh
a="tEsTstr1ng"
b=`echo $a | egrep "^.{8,255}" | \
             egrep "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]" | \
             egrep "[abcdefghijklmnopqrstuvwxyz"] | \
             egrep "[0-9]"`
# now featuring W in the alphabet string
#if the result string is empty, one of the conditions has failed
if [ -z $b ]
  then
    echo "Conditions do not match"
  else
    echo "Conditions match"
fi
0
source

Source: https://habr.com/ru/post/1775601/


All Articles