总是给密码不够强的消息(否则条件正在运行)

时间:2018-04-22 17:56:28

标签: bash

我一直在使用shell脚本编写这个函数来从用户那里获取密码,并检查它是否满足成为有效密码的标准,但它总是说密码不够强。我正在尝试使用的密码是LK@12abc

这是我的代码:

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

4 个答案:

答案 0 :(得分:1)

您的密码似乎必须包含:

  • 3个小写字母
  • 2个大写字母
  • 2位数
  • 1个标点字符
  • 正好8个字符

所以,使用bash glob模式:

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

我很惊讶我需要在第4次测试中逃避&

答案 1 :(得分:0)

正如man bash中所述:

  

可以使用另外的二元运算符=〜   优先级为==和!=。使用时,右边的字符串   运算符被视为扩展正则表达式并匹配   因此(如正则表达式(3))。

第一个问题是POSIX正则表达式不允许前瞻 和看起来断言。

第二个问题是您的测试密码不正确 - 您 最后错过8个随机字符。

第三个问题是你没有指定 shebang并没有正确引用变量。

总而言之,您的脚本应该是这样的:

#!/usr/bin/env bash

paq() {

    read -r -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

运行它:

$ ./paq.sh
PASSWORD :- AB!11abc12345678
Valid password

答案 2 :(得分:0)

您可以使用原始正则表达式并将其提供给var input = $("<input type='checkbox' id='" + i + "_" + j + $(this).html() + "' />"); li.append(input); //li is a variable created elsewhere input.isSelected = false; input.select = function () { this.isSelected = true; console.log(this.isSelected); //true } input.deselect = function () { this.isSelected = false; console.log(this.isSelected); //false } input.select(); //logs true input.on("click", function () { console.log(this.isSelected); //undefined if(this.isSelected){ this.deselect(); //does not get called }else{ this.select(); //does not get called } }) (仅当您的系统的grep -P支持Perl正则表达式时)。然后使用脚本:

grep

答案 3 :(得分:0)

使用tr将每个想要的字符类转换为数字,sort该数字字符串并计数与grep的匹配(使用@glennjackman定义的字符类除了确切的长度):

echo LK@12abc | 
tr '[:digit:]' 1 |                         # do this first
tr '[:upper:]' 2 | 
tr '[:lower:]' 3 | 
tr '[:punct:]' 4 | 
tr -c '[:digit:]' 5 |                      # all else to 5
grep -o . |                                # transpose
sort |                                     # sort
tr -d '\n' |                               # transpose 
if 
  grep -q  "1\{2,\}2\{2,\}3\{3,\}4\{1,\}5" # 5 is the trailing newline
then 
  echo yes
else 
  echo no
fi                                         # outputs
yes

(我试图tr '[:digit:][:upper:][:lower:]' '123',但显然不会工作,所以每个类现在都在自己的行上处理。也许你可以松开tr,只需对密码字符串进行排序并使用grep

计算匹配类
相关问题