Bash 密码验证器
Bash Password validator
我坚持使用我的 bash 代码。需要一些帮助。
我试图编写 bash 代码:
长度——最少 10 个字符。
包含字母和数字。
包括小写字母和大写字母。
如果通过验证则将输出着色为绿色,否则为红色。
Return如果通过验证则退出代码0,否则退出代码1。,
可以为我解决的问题是代码不检查大小写。
并且有 exit 0,这意味着密码在所有情况下都可以。
# Color for Output
Warning='3[0;31m' # Red
Success='3[0;32m' # Green
NC="3[0m" # No Color
password=
len="${#password}"
if test $len -ge 4 ; then
echo "$password" | grep -eq [[:digit:]]
if test $? -eq 1 ; then
echo "$password" | grep -e [[:upper:]]
if test $? -e 1 ; then
echo "$password" | grep -e [[:lower:]]
if test $? -e 1 ; then
printf "${Success}Strong password!\n"
else
echo "weak password include lower case char"
fi
else
echo "weak password include capital char"
fi
else
echo && echo $?
printf "${Success}Strong password!\n"
fi
else
echo $?
printf ${Warning}"Try again... \nPassword must have at least 10 characters.\n"
fi
在 bash 中,在 [[ ... ]]
条件中,==
和 !=
运算符执行 模式匹配 。 bash 模式可以包含 POSIX 个字符 类.
error() {
echo "$*" >&2
exit 1
}
password=
if (( ${#password} < 10 )); then
error "Too short"
elif [[ $password != *[[:digit:]]* ]]; then
error "Does not contain a digit"
elif [[ $password != *[[:lower:]]* ]]; then
error "Does not contain a lower case letter"
elif [[ $password != *[[:upper:]]* ]]; then
error "Does not contain an upper case letter"
fi
echo "OK"
exit 0
POSIX 个字符 类 的列表记录在 3.5.8.1 Pattern Matching
的手册中
那个cascading-if可以写得更简洁:
(( ${#password} >= 10 )) || error "Too short"
[[ $password == *[[:digit:]]* ]] || error "Does not contain a digit"
[[ $password == *[[:lower:]]* ]] || error "Does not contain a lower case letter"
[[ $password == *[[:upper:]]* ]] || error "Does not contain an upper case letter"
echo OK
[[ ... ]]
还有=~
regular-expression匹配运算符(没有!~
否定匹配运算符):
# it contains a digit, or error
[[ $password =~ [[:digit:]] ]] || error "Does not contain a digit"
我坚持使用我的 bash 代码。需要一些帮助。 我试图编写 bash 代码: 长度——最少 10 个字符。 包含字母和数字。 包括小写字母和大写字母。 如果通过验证则将输出着色为绿色,否则为红色。 Return如果通过验证则退出代码0,否则退出代码1。, 可以为我解决的问题是代码不检查大小写。 并且有 exit 0,这意味着密码在所有情况下都可以。
# Color for Output
Warning='3[0;31m' # Red
Success='3[0;32m' # Green
NC="3[0m" # No Color
password=
len="${#password}"
if test $len -ge 4 ; then
echo "$password" | grep -eq [[:digit:]]
if test $? -eq 1 ; then
echo "$password" | grep -e [[:upper:]]
if test $? -e 1 ; then
echo "$password" | grep -e [[:lower:]]
if test $? -e 1 ; then
printf "${Success}Strong password!\n"
else
echo "weak password include lower case char"
fi
else
echo "weak password include capital char"
fi
else
echo && echo $?
printf "${Success}Strong password!\n"
fi
else
echo $?
printf ${Warning}"Try again... \nPassword must have at least 10 characters.\n"
fi
在 bash 中,在 [[ ... ]]
条件中,==
和 !=
运算符执行 模式匹配 。 bash 模式可以包含 POSIX 个字符 类.
error() {
echo "$*" >&2
exit 1
}
password=
if (( ${#password} < 10 )); then
error "Too short"
elif [[ $password != *[[:digit:]]* ]]; then
error "Does not contain a digit"
elif [[ $password != *[[:lower:]]* ]]; then
error "Does not contain a lower case letter"
elif [[ $password != *[[:upper:]]* ]]; then
error "Does not contain an upper case letter"
fi
echo "OK"
exit 0
POSIX 个字符 类 的列表记录在 3.5.8.1 Pattern Matching
的手册中那个cascading-if可以写得更简洁:
(( ${#password} >= 10 )) || error "Too short"
[[ $password == *[[:digit:]]* ]] || error "Does not contain a digit"
[[ $password == *[[:lower:]]* ]] || error "Does not contain a lower case letter"
[[ $password == *[[:upper:]]* ]] || error "Does not contain an upper case letter"
echo OK
[[ ... ]]
还有=~
regular-expression匹配运算符(没有!~
否定匹配运算符):
# it contains a digit, or error
[[ $password =~ [[:digit:]] ]] || error "Does not contain a digit"