我正在使用正则表达式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
BASH 正则表达式引擎不支持正则表达式中的环视。
您可以使用以下 shell glob 检查来确保密码符合您的条件:
[[ ${#s} -ge 6 && "$s" == *[A-Z]* && "$s" == *[a-z]* && "$s" == *[0-9]* ]]
它将确保输入字符串$s
满足所有这些条件:
- 至少 6 个字符长
- 至少有一位数字
- 至少有一个大写字母
- 至少有一个小写字母
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)