Bash shell 中的正则表达式密码验证

Aru*_*kar 4 bash

我在Bash Shell 脚本中使用正则表达式。我使用下面的正则表达式代码来检查密码标准:密码长度应至少为 6 个字符,其中至少有一位数字和至少一个大写字母。我在正则表达式验证工具中进行了验证,我形成的正则表达式工作正常。但是,它在 Bash Shell 脚本中失败。请提供您的想法。

echo "Please enter password for User to be created in OIM: "
echo "******Please Note: Password should be at least 6 characters long with one digit and one Upper case Alphabet******"
read user_passwd
regex="^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)\S{6,}$"
echo $user_passwd
echo $regex
if [[ $user_passwd =~ $regex ]]; then
    echolog "Password Matches the criteria"
else
    echo "Password criteria: Password should be at least 6 characters long with one digit and one Upper case Alphabet"
    echo "Password does not Match the criteria, exiting..."
    exit
fi
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 5

BASH 正则表达式引擎不支持正则表达式中的环视。

您可以使用以下 shell glob 检查来确保密码符合您的条件:

[[ ${#s} -ge 6 && "$s" == *[A-Z]* && "$s" == *[a-z]* && "$s" == *[0-9]* ]]
Run Code Online (Sandbox Code Playgroud)

它将确保输入字符串$s满足所有这些条件:

  • 至少 6 个字符长
  • 至少有一位数字
  • 至少有一个大写字母
  • 至少有一个小写字母